222d0b02508be5a4fff327bb049203592be03ab3 — Robert A. Uhl 9 months ago 9330861 master
Added days 12, 13 & 14

Was too busy to commit them earlier.

Sadly, with an end-of-year project at work, I believe that these are
the last ones I’m likely to get to work on this year, at least in
time.  I’m pretty bummed — it’s been great fun so far.
3 files changed, 443 insertions(+), 0 deletions(-)

A 2018-12-12.lisp
A 2018-12-13.lisp
A 2018-12-14.lisp
A 2018-12-12.lisp => 2018-12-12.lisp +108 -0
@@ 0,0 1,108 @@
+ (defpackage "AOC/12" (:use "CL"))
+ (in-package "AOC/12")
+ 
+ (defvar *test-input* "initial state: #..#.#..##......###...###
+ 
+ ...## => #
+ ..#.. => #
+ .#... => #
+ .#.#. => #
+ .#.## => #
+ .##.. => #
+ .#### => #
+ #.#.# => #
+ #.### => #
+ ##.#. => #
+ ##.## => #
+ ###.. => #
+ ###.# => #
+ ####. => #")
+ 
+ (defvar *input* "initial state: #..######..#....#####..###.##..#######.####...####.##..#....#.##.....########.#...#.####........#.#.
+ 
+ #...# => .
+ ##.## => .
+ ###.. => .
+ .#### => .
+ #.#.# => .
+ ##..# => #
+ ..#.# => #
+ .##.. => #
+ ##... => #
+ #..## => #
+ #..#. => .
+ .###. => #
+ #.##. => .
+ ..### => .
+ .##.# => #
+ ....# => .
+ ##### => .
+ #.### => .
+ .#..# => .
+ #.... => .
+ ...## => .
+ .#.## => .
+ ##.#. => #
+ #.#.. => #
+ ..... => .
+ .#... => #
+ ...#. => #
+ ..#.. => .
+ ..##. => .
+ ###.# => .
+ ####. => .
+ .#.#. => .
+ ")
+ 
+ (defun parse-input (stream)
+   (let* ((line (read-line stream))
+          (state (make-string (- (length line) 15)))
+          (rules (make-hash-table :test 'equal)))
+     (assert (string= (subseq line 0 15) "initial state: "))
+     (replace state (subseq line 15))
+     (read-line stream)
+     (loop for line = (read-line stream nil)
+           while line
+           do (setf (gethash (subseq line 0 5) rules) (aref line 9)))
+     (cons (cons 0 state)
+           rules)))
+ 
+ (defun trim (state)
+   (let ((offset (caar state))
+         (pos (position #\# (cdar state) :test #'eql)))
+     (cons (cons (+ offset pos) (subseq (cdar state) pos (1+ (position #\# (cdar state) :test #'eql :from-end t))))
+           (cdr state))))
+ 
+ (defun turn (state)
+   ;; This conses more than it should, but … it’s a puzzle, not
+   ;; production code.
+   (let* ((vector
+            (replace
+             (make-string (+ (length (cdar state)) 8) :initial-element #\.)
+             (cdar state)
+             :start1 4))
+          (rules (cdr state))
+          (new-vector (copy-seq vector)))
+     (loop for i from 0 below (- (length new-vector) 5)
+           do (setf (aref new-vector (+ i 2))
+                    (gethash (subseq vector i (+ i 5)) rules #\.)))
+     (trim (cons (cons (- (caar state) 4) new-vector) rules))))
+ 
+ (defun run (state turns)
+   (dotimes (i turns state)
+     (setf state (turn state))))
+ 
+ (defun solution-1 ()
+   (with-input-from-string (s *input*)
+     (let ((end (run (parse-input s) 20)))
+       (loop for i from (caar end) and pot across (cdar end)
+          when (char= pot #\#)
+          sum i))))
+ 
+ (defun solution-2 ()
+   ;; I cheated and just submitted 3150000000905
+   (with-input-from-string (s *input*)
+     (let ((end (run (parse-input s) 5000000000)))
+       (loop for i from (caar end) and pot across (cdar end)
+             when (char= pot #\#)
+               sum i))))

A 2018-12-13.lisp => 2018-12-13.lisp +287 -0
@@ 0,0 1,287 @@
+ (defpackage "AOC/13" (:use "CL"))
+ (in-package "AOC/13")
+ 
+ (defvar *test-input* "/->-\\
+ |   |  /----\\
+ | /-+--+-\\  |
+ | | |  | v  |
+ \\-+-/  \\-+--/
+   \\------/   ")
+ 
+ (defvar *test-input-2* "/>-<\\
+ |   |
+ | /<+-\\
+ | | | v
+ \\>+</ |
+   |   ^
+   \\<->/")
+ 
+ (defvar *input* "                      /---------------------\\           /----------------<---------------------------------------------------------------------\\
+                       |                     |           |         /---------------------------\\                                                |
+               /-------+---------------------+-----------+---------+---------------------------+------\\                                         |
+    /----------+-------+---------------------+---\\      /+---------+---------------------------+------+------\\                                  |
+  /-+----------+-------+---------------------+---+------++---------+-------------------------\\ |      |      |                                  |
+  | |          |       |  /------------------+---+------++---------+-----\\                   | | /----+------+---------------------------\\      |
+  | |   /------+-------+--+------------------+---+-\\    ||         |     |              /----+-+-+----+------+----------------\\          |      |
+  | |   |      | /-----+--+------------------+---+-+----++---------+-----+--------------+----+-+-+----+---\\  |                |          |      |
+  | |   |      | |    /+--+--------------\\   |   | |    ||    /----+-----+--------------+----+-+-+----+---+--+----------------+---------\\|      |
+  | |   |      | |    ||  |              |   |   | |    ||    |    |     |              |    | | |    |   |  |                |         ||      |
+  | |   |      | |    ||  |      /-------+---+-\\ | |    ||    |    |     |              |    | | |    |   |  |        /-------+---------++------+---\\
+  | |   |      | |    ||  |      |       |   | | | |    ||    |    |     |     /--------+----+-+-+----+---+\\ |        |       |         ||      |   |
+  | |   |      | |    ||  |      |       |   | | | |    ||    |/---+-----+\\    |        |    | | |    |   || |        |       |         ||      |   |
+  | |   |    /-+-+----++--+------+-------+---+-+-+-+----++----++---+-----++--->+--------+----+-+-+----+---++-+-----\\  |       |         ||      |   |
+  | |   |    | | |   /++--+------+-------+---+-+-+-+----++----++---+-----++----+--------+----+-+-+----+---++-+-----+--+-------+-\\       ||      |   |
+  | |  /+----+-+-+---+++--+------+-------+---+-+-+-+----++-\\  ||   |     ||    |        |    | | |    |   || |     |  |       | |       ||      |   |
+  | |  ||    | | |   |||  |      |       |   | | | |    || | /++---+-----++----+--------+----+-+-+-\\  |   || |     |  |       | |       ||      |   |
+  | |  ||    |/+-+---+++--+------+-------+---+-+-+-+----++-+-+++---+\\  /-++----+--\\     |    | | | |  |   || |     |  |       | |/------++--\\   |   |
+  | |  ||    ||| |/--+++--+------+-------+---+-+-+-+----++\\| |||   ||  | ||    |  | /---+----+-+-+-+--+---++-+----\\|  |       | ||      ||  |   |   |
+  | |  ||    ||| ||  |||  |/-----+-------+---+-+-+-+----++++-+++---++--+-++----+--+-+---+----+-+-+-+--+---++-+----++--+-------+-++---\\  ||  |   |   |
+  | |  ||    ||| ||  |\\+--++-----+-------/   | | | |    |||| |||   v|  | ||    |  | |   |    | | | |  |/--++-+----++--+-------+-++---+--++--+---+--\\|
+ /+-+--++----+++-++--+-+--++-----+-----------+-+-+-+----++++-+++---++--+-++----+--+-+-\\ \\----+-+-+-+--++--++-+----++--+-------/ ||   |  ||  |   |  ||
+ || | /++----+++-++--+-+--++-----+-----------+-+-+-+----++++-+++---++--+-++---\\|  | | |      | | | |  ||  || |    ||  |         ||   |  ||  |   |  ||
+ || | |||    ||| ||  | |  ||     |           | | | |/---++++-+++---++--+-++---++--+-+-+------+-+-+-+--++--++-+----++--+---------++--\\|  ||  |   |  ||
+ || | |||/---+++-++--+-+--++-----+-----------+-+-+-++---++++-+++---++--+-++---++--+-+-+---\\  | | | |  ||  || |    ||  |         ||  ||  ||  |   |  ||
+ || | ||||   ||| ||  | |  ||     |           | | | ||   |||| |||   ||  | ||/--++--+-+-+---+--+-+\\| |  ||  || |    ||  |         ||  ||  ||  |   |  ||
+ || | ||||   ||| || /+-+--++-----+-----------+-+-+-++---++++-+++---++--+-+++--++--+-+-+---+--+-+++-+--++--++-+----++--+-------\\ ||  ||  ||  |   |  ||
+ || | ||||   ||| \\+-++-+--++-----+-----------+-+-+-++---++++-+++---++--+-+++--++--+-+-+---+--+-+++-+--++--/| |    ||  \\-------+-++--++--++--+---+--+/
+ || | ||||   |||  |/++-+--++-----+-----------+-+-+-++---++++-+++---++--+-+++\\ ||  | | |   |  | ||| |  ||   | |    ||          | ||  ||  ||  |   |  |
+ || | ||||   |||  |||| |  ||     |           | | | ||   |||| |||   ||  | |||| ||  | | |   |  | ||| |  ||   | |    ||          | ||  ||  ||  |   |  |
+ || |/++++---+++--++++-+--++---\\ |           | | | ||   |||| |||   ||/-+-++++-++--+-+-+---+--+-+++-+--++---+-+----++----------+-++--++--++--+---+--+\\
+ || ||||||   |||  |||| |  ||   | |           | | | ||   |||| |\\+---+++-+-++++-++--+-+-+---+--+-+++-+--++---+-+----++----------+-++--++--/|  |   |  ||
+ || ||||||   |||  |||| |  ||   | |  /--------+-+-+-++---++++-+-+---+++-+-++++-++--+-+-+---+-\\| ||| |  ||   | |    ||          | ||  ||   |  |   |  ||
+ || ||||||   |||  |||| | /++---+-+--+--------+-+-+-++---++++-+-+---+++-+-++++-++--+-+-+---+-++-+++-+--++-\\ | |    || /--------+-++--++---+--+\\  |  ||
+ || ||||||   |||  |||| | |||   | |  |   /----+-+-+-++---++++-+-+---+++-+-++++-++--+-+-+---+-++-+++-+--++-+-+-+----++-+--------+-++--++---+--++--+-\\||
+ || ||||||   |||  |||| | |||   | |  |   |    | | | ||  /++++-+-+--\\||| | |||| ||  |/+-+---+-++-+++-+--++-+-+-+----++-+--------+-++--++---+--++-\\| |||
+ || ||||||   |||  |||| | |||  /+-+--+---+----+-+-+-++--+++++-+-+--++++-+-++++-++--+++-+---+-++-+++-+--++-+-+-+----++-+--\\     | ||  ||   |  || || |||
+ || |||||| /-+++--++++-+-+++--++-+--+---+----+-+\\| ||  ||||| | |  |||| | |||| ||  ||| |   | || ||| |  || | | |    || |  |   /-+-++--++---+\\ || || |||
+ || |||||| | |||  |||| \\-+++--++-+--+---+----/ ||| ||  ||||| | |  |||| | |||| ||  ||| |   | || ||| |  || | | |    || |  |   | | ||  ||   || || || |||
+ || ||||\\+-+-+++--++++---+++--++-+--+---+>-----+++-/|  ||||| | |  |||\\-+-++++-++--+++-+---+-++-+++-+--++-+-+-+----++-+--+---+-+-++--++---++-++-++-++/
+ || |||| | | |||  ||||   |||  || |  |   |/-----+++--+--+++++-+-+--+++--+-++++-++--+++-+---+-++-+++-+--++-+-+-+-\\  || |  |   | | ||  ||   || || || ||
+ || |||| | | |||  |\\++---+++--++-+--+---++-----+++--+--+++++-+-+--+++--+-+++//++--+++-+---+-++-+++-+--++-+-+-+-+--++-+--+---+\\| ||  ||   || || || ||
+ || |||| | | |||  | |\\---+++--++-+--+---++-----+++--+--+++++-+-+--+++--+>+++-+++--+++-+---+-++-+++-+--++-+-+-+-+--++-+--+---+++-/|  ||   || || || ||
+ || |||| | | |||  | |    |||  || |  | /-++-----+++--+--+++++-+-+--+++--+-+++-+++--+++-+---+-++-+++-+--++-+-+-+-+--++-+--+---+++--+--++-<-++-++-++\\||
+ || |||\\-+-+-+++--+-+----+++--++-+--+-+-++-----+++--+--++++/ | |  |||  | ||| |||  ||| |   | || ||| |  || |/+-+-+--++-+--+---+++--+--++---++-++-+++++\\
+ || |||  | | |||  | |    |||  || |  | | ||     |||  |  ||||  \\-+--+++--+-+++-+++--+++-+---+-++-+++-/  || ||| | |  || |  |   |||  |  ||   || || ||||||
+ || |||  | | |||  | |    |||  || |  | | ||/----+++--+--++++----+\\ |||  | ||| |||  |||/+---+-++-+++----++-+++-+-+--++-+--+---+++--+--++--\\|| || ||||||
+ || |||  | | |||  | |    |||  || |  |/+-+++----+++--+--++++----++-+++--+-+++-+++--+++++---+-++-+++----++-+++-+-+--++-+--+---+++-\\|  ||  ||| || ||||||
+ || \\++--+-+-+++--+-+----+++--++-+--+++-+++----++/  |  ||||    || |||  \\-+++-+++--/||||   | || |||    || ||| | |  || |  |   ||| ||  ||  ||| || ||||||
+ ||  ||  | | |||  | |    |||  || |  ||| |||    ||   |  ||||    || |||    ||| ||\\---++++---+-++-+++----++-++/ | |  || |  |   ||| ||  ||  ||| || ||||||
+ ||  ||  | | |||  | |    |||  || |  ||| |||    ||   |  ||||    \\+-+++----+/| \\+----++++---+-++-+++----++-++--+-+--++-+--+---+/| ||  ||  ||| || ||||||
+ ||  || /+-+-+++--+-+----+++--++-+--+++-+++----++---+--++++-----+-+++----+-+--+----++++---+\\|| |||    || ||  | |  || \\--+---+-+-++--++--+++-+/ ||||||
+ ||  || || | |||  | |   /+++--++-+--+++-+++----++---+--++++-----+-+++----+-+--+----++++---++++-+++----++-++--+-+--++--\\ |   | | ||  ||  ||| |  ||||||
+ ||  || || | |||  | |   ||||  || |  ||| |||    ||   |  ||||     | |\\+----+-+--+----++++---++++-/||    || ||  | |  ||  | |   | | ||  ||  ||| |  ||||||
+ ||  || || | |||  | |  /++++--++-+--+++-+++----++---+--++++-----+-+-+----+-+--+----++++---++++--++----++-++--+-+--++--+-+---+-+-++-\\||  ||| |  ||||||
+ ||  || || | |||  | |  |||||  || |  ||| |||    ||   |  ||||     | | |    | |  |    ||||   |||| /++----++-++--+-+--++--+\\|   |/+-++-+++\\ ||| |  ||||||
+ ||  || || | |||  | |  |||||  || |  ||| |||    ||   |/-++++-----+-+-+----+-+--+----++++---++++-+++\\   || ||  | |  ||  |||   ||| || |||| ||| |  ||||||
+ ||  || || | |||  | | /+++++--++-+--+++<+++----++---++-++++-----+-+-+----+-+--+----++++---++++-++++---++-++--+\\|  ||  |||   ||| || |||| ||| |  ||||||
+ ||  || || | |||  | | ||||||  || |  |\\+-+++----++---++-++++-----+-+-+----+-+--+----++++---++++-++++---++-++--+++--++--+++---+++-/| |||| ||| |  ||||||
+ ||  || || | |||  | | |||\\++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++++---++++-++++---++-/|  |||  ||  |||   |||  | |||| ||| |  |^||||
+ ||  || || | |||  | |/+++-++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++++\\  |||| ||||   |\\--+--+++--++--+++---+++--+-++++-+++-+--++++/|
+ ||  || || | |||  | ||||| ||  || |  | | |||    ||   || ||||     | | |    | |  |    ||\\++--++++-++++---+---+--+++--++--+++---+++--+-++++-/|| |  |||| |
+ ||  || || | |||  | ||||| ||  || |  | | |||    ||   || ||||     | | |    | |  |    || ||  |||| ||||   |   |  |||  ||  |||   |||  | ||||  || |  |||| |
+ ||  || || | \\++--+-+++++-++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++-++--++++-++++---+---+--+++--+/  |||   |||  | ||||  || |  |||| |
+ ||  || || |  ||  | ||||| ||  || |  | | |||    ||   || ||||     | | |    | |  |    || ||  |||| ||||   |   |  |||  |   |||   |||  | ||||  || |  |||| |
+ ||  ||/++-+--++--+-+++++-++--++-+--+-+-+++----++---++-++++-----+-+-+----+-+--+----++-++\\ |||| ||||   |   |  |||  |   |||   |||  | ||||  || |  |||| |
+ ||  \\++++-+--++--+-+++++-++--+/ |  | | ||| /--++---++-++++-----+-+-+----+-+--+----++-+++-++++-++++>--+---+--+++--+---+++---+++--+\\||||  || |  |||| |
+ ||   |||| |  ||  | ||||| |\\--+--+--+-+-+++-+--++---++-++++-----+-+-+----+-+--+----++-+++-++++-++++---+---+--+++--+---+++---+++--++++/|  || |  |||| |
+ ||   |||| |  ||  | ||||| |   |  |  | | ||\\-+--++---++-++++-----/ | |    |/+--+----++-+++\\|||| ||||   |/--+--+++--+---+++---+++--++++-+--++-+-\\|||| |
+ ||   |||| |  ||  | ||||| |   |  |  | | ||  |/-++---++-++++-------+-+----+++--+----++-++++++++-++++---++--+--+++--+---+++---+++--++++-+--++\\| ||||| |
+ ||   |||| |  ||  | ||||| | /-+--+--+-+-++--++-++---++-++++-------+-+----+++--+----++-++++++++-++++---++--+--+++\\ |   |||   |||  |||| |  |||| ||||| |
+ |\\---++++-+--++--+-+++++-+-+-+--+--+-+-++--++-++---++-++++-------+-+----+++--+----++-+++++++/ ||||   ||  |  |||| |   |||   |||  |||| |  |||| ||||| |
+ |    |||\\-+--++--+-+++++-+-+-+--+--+-+-++--++-++---++-++++-------+-+----+++--+----++-++++/||  ||||   ||  |  |||| |   |||   |||  |||| |  |||| ||||| |
+ |    |||/-+--++--+-+++++-+-+-+--+--+-+-++--++-++---++-++++-------+-+-\\  |||  |    || |||| ||  ||||   ||  |  |||| |   |||   |||  |||| |  |||| ||||| |
+ |    |||| |  ||  | ||||| | | |  |  | | ||  || ||   |\\-++++-------+-+-+--+++--+----++-++++-++--+++/   ||  |  |||| |   |||   |||  \\+++-+--+++/ ||||| |
+ |    |||| |  ||  | ||||| | | |/-+--+-+-++--++-++---+--++++-------+-+-+--+++--+----++-++++-++--+++----++--+--++++\\|   |||   |||   ||| |  |||  ||||| |
+ |    |||| |  ||  | ||||| | | || |  | | ||  || || /-+--++++-------+-+-+--+++--+--\\ || |||| ||  |||    ||  |  ||||||   |||   |||   ||| |  |||  ||||| |
+ |    |||| |  ||  | ||||| \\-+-++-+--+-+-++--++-++-+-+--++++-------+-+-+--/||  |  | || |||| ||  |||    ||  |  ||||||   |||   |||   ||| |  |||  ||||| |
+ |    |||| |  ||  | |||||   | || |  | | ||  || || | |  |||| /-----+-+-+---++--+--+-++-++++-++--+++----++--+--++++++---+++---+++\\  ||| |  |||  ||||| |
+ |    |||| |  \\+--+-+++++---+-++-+--+-+-++--++-++-+-+--++++-+-----+-/ |   ||  |  | || |||| ||  |||   /++--+--++++++\\  |||   |||| /+++-+--+++\\ ||||| |
+ |    |||| \\---+--+-+++++---+-++-+--+-+-++--++-+/ v |  |\\++-+-----+---+---++--+--+-++-++++-++--+++---+++--+--/||||||  |||   |||| |||| |  |||| ||||| |
+ |    ||||  /--+--+-+++++--\\|/++-+--+-+-++--++-+--+-+--+-++-+-----+---+---++--+--+-++-++++-++--+++---+++--+\\  ||||||  |||   |||| |||| |  |||| ||||| |
+ |    ||||  |  | /+-+++++--+++++-+--+-+-++--++-+--+-+--+-++-+-----+---+---++-\\|  | || |||| ||  |||   |||/-++--++++++-\\|||   |||| |||| |  |||| ||||| |
+ |    ||||  |  | || |||||  ||||| |  | | ||  || |  | |  | || |/----+---+---++-++--+-++-++++-++--+++---++++-++--++++++-++++\\  |||| |||| |  |||| ||||| |
+ \\----++++--+--+-++-+++++--+++++-+--+-+-++--++-+--+-+--+-++-++----+---+---++-++--+-++-/||| ||  |||   |||| ||  |||||| |||||  |||| |||| |  |||| ||||| |
+     /++++--+--+-++-+++++\\ |||\\+-+--+-+-++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++++++-+++/|  |||| |||| |  |||| ||||| |
+     |||||/-+--+-++-++++++-+++-+-+-\\| \\-++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++++++-+++-+--++++-++++-+--++++-+++/| |
+     |||||| |  | || |||||| |\\+-+<+-++---++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++/||| ||| |  |||| |||| |  |||| ||| | |
+     |||||| |  | || |||||| | | \\-+-++---++--++-+--+-+--+-++-++----+---+---++-++--+-++--+++-++--+++---++++-++--++-/|| ||| |  |||| |||| |  |||| ||| | |
+     |||||| |  | || |||||| | |   | ||   ||  || |  \\-+--+-++-++----+---+---++-++--/ ||  ||| ||  |||   |||| ||  ||  || ||| |  |||| |||| |  |||| ||| | |
+     |||||| |  | || |||\\++-+-+---+-++---++--++-+----+--+-++-++----+---+---++-++----++--+++-++--+++---++++-++--++--++-+++-+--++++-++/| |  |||| ||| | |
+     |||||| |  |/++-+++-++-+-+---+-++---++--++-+----+--+-++-++----+---+---++-++----++--+++-++\\ |||   |||| ||  ||  || ||| |  |||| || | |  |||| ||| | |
+     |||||| |  |||| ||| || | |   | |\\---++--++-+----+--+-++-++----+---+---++-++----++--+++-+/| |||   |||| ||  ||  || ||| |  |||| || | |  |||| ||| | |
+     |||||| |  |||| ||| || | |   | |    ||  \\+-+----+--+-++-++----+---+---++-++----++--+++-+-+-+++---++++-++--++--++-+++-+--++++-+/ | |  |||| ||| | |
+     |||||| |  |||| ||| || | |   | |    ||/--+-+----+--+-++-++---\\|   |   || ||    ||  ||| | | |||   |||| ||  ||  || ||| |  |||| |  | |  |||| ||| | |
+     |||||| |  |||| ||| || | |   | |    |||  | |    |  | || ||   ||   |   || ||    ||  ||| | | ||\\---++++-++--++--++-+++-+--++++-+--+-+--/||| ||| | |
+     |||||| |  |||| ||| |^ | |   | |    |||  | |    |  | || ||   ||   |   || ||    ||  |||/+-+-++----++++-++--++--++-+++\\|  |||| |  | |   ||| ||| | |
+   /-++++++-+--++++\\||| || | |   | |    |||  | |    |  | || ||   ||   |   \\+-++----++-<++/|| | ||    |||| ||  ||  || |||||  |||| |  | |   ||| ||| | |
+   | |||||| \\--++++++++-++-/ |   | |    ||| /+-+----+--+-++-++---++---+---\\| || /--++--++-++-+-++----++++-++--++--++-+++++--++++-+--+\\|   ||| ||| | |
+   | ||||||    |||||||| ||   |   | |    ||| || |    |  | || ||   ||   |   || || |  ||  || || | \\+----++++-++--++--++-++/||  |||| |  |||   ||| ||| | |
+   | ||||||    |||||||| ||   |   | | /--+++-++-+----+--+-++-++---++---+\\  || || |  \\+--++-++-+--+----++++-++--++--++-++-++--++++-+--+++---+++-+/| | |
+   | ||||||    |||||||| ||   |   | | |  ||| || |    | /+-++-++---++---++--++-++-+---+--++-++-+--+----++++-++--++--++-++-++--++++-+--+++---+++-+-+-+-+\\
+   | ||||||    |||||||| ||   |   | | |  ||| || |    | || \\+-++---++---++--++-++-+---+--++-++-+--+----++++-++--++--++-++-++--++++-+--+++---+++-+-/ | ||
+   | ||||||    \\+++++++-++---+---+-+-+--+++-++-+----+-++--+-++---++---++--++-++-+---+--++-++-+--+----+/|| ||  ||  || || ||  |||| |  |||   ||| |   | ||
+   | ||||||     ||||||| ||   |   | | |  ||| || |    | ||  | ||   ||   ||  || || |   |  || || |  |    | || ||  ||  || || ||  |||| |  |||   ||| |   | ||
+   | ||||||     ||||||| ||   |   | | |  \\++-++-+----+-++--+-++---++---++--++-++-+---+--++-++-+--+----+-++-++--++--++-++-++--++++-+--+++---+++-+---/ ||
+   | \\+++++-----+++++++-+/   |   \\-+-+---++-++-/    | ||  | ||   ||   ||  || || |   \\--++-++-+--+----+-++-++--++--/| || ||  |\\++-+--++/   ||| |     ||
+   |  |v|||/----+++++++-+----+--\\  | |   || ||      \\-++--+-++---++---++--++-++-+------++-++-+--+----+-++-++--++---+-++-++--+-++-+--/|    ||| |     ||
+   |  ||||||    \\++++++-+----+--+--+-+---++-++--------++--+-++---++---++--++-++-+------++-++-/  |    | || ||  ||   | || ||  | || |   |    ||| |     ||
+   |  ||||||   /-++++++-+----+--+--+-+---++-++--------++--+-++---++---++--++-++-+------++-++----+----+-++\\||  ||   | || ||  | || |   |    ||| |     ||
+   |  ||||||   | |||||| |    |  |  | |   \\+-++--------++--+-++---++---++--++-++-+------++-++----+----+-+++++--+/   | || ||  | || |   |    ||| |     ||
+   |  ||||||   | |||||| \\----+--+--+-+----+-++--------++--+-++---++---++--++-++-+------++-++----+----+-+++++--+----+-+/ ||  | || |   |    ||| |     ||
+   \\--++++++---+-++/|||      |  |  | |    | ||        ||  | ||   ||   ||  || || |   /--++-++----+----+-+++++--+----+-+--++\\ | || |   |    ||| |     ||
+      ||||||   | || |||      |  |  | |    | \\+--------++--+-++---++---++--/| || |   |  || ||    |    | |||||  |    | |  ||| | || |   |    ||| |     ||
+      \\+++++---+-++-+++------+--+--+-+----+--+--------++--+-++---++---++---+-+/ \\---+--++-++----+----+-+++++--+----+-+--+++-+-++-+---/    ||| |     ||
+       ||||| /-+-++-+++------+--+--+-+----+--+--------++--+-++---++---++---+-+------+--++-++----+----+-+++++--+\\   | |  ||| \\-++-+--------/|| |     ||
+       ||||| | | || |||      \\--+--+-+----+--+--------++--+-++---++---++---+-+->----+--++-++----+----+-++++/  ||   | |  |||   || |         || |     ||
+       ||||| | | || |||         |  | |    |  |        |\\--+-++---+/   ||/--+-+------+--++-++----+---\\| |\\++---++---+-/  |||   || |         || |     ||
+  /----+++++-+-+-++\\|||         |  | |    \\--+--------+---+-++---/    |||  | |      |  || ||    |   || | |\\---++---+----+++---++-+---------++-+-----/|
+  |    ||||| | | ||||||         |  | |       |        |   | ||        |||  | |      |  || ||    |   || | |    ||   |    |||   || |         || |      |
+  |    ||||| | | |\\++++---------+--+-+-------+--------+---/ ||        |||  | |      |  || ||    |   || | |    ||   |    |||   || |         || |      |
+  |    ||||| | | | ||||         |  | |       |        |     ||   /----+++--+-+------+--++-++----+---++-+-+----++---+----+++---++-+------\\  || |      |
+  |    ||||| | | | ||||         |  | |       |        \\-----++---+----+++--+-+------+--++-++----+---++-+-+----++---+----+++---++-+------+--++-+------/
+  |    ||||| | | | ||||         |  | \\-------+--------------++---+----+/|  | |      |  || ||    |   || | |    ||   |    ||| /-++-+------+-\\|| |
+  |    ||||| | | | ||||         |  |         |              \\+---+----+-+--+-+------+--++-++----+---++-+-+----++---+----+++-+-+/ |      | ||| |
+  |    ||||| | \\-+-++++---------+--+---------+---------------+---+----+-+--+-+------+--++-++----+---++-+-/    ||   |    ||| | |  |      | ||| |
+  |    ||||| |   | |||\\---------+--+---------+---------------+---+----+-+--+-+------+--++-++----+---++-+------/|   |    ||| | |  |      | ||| |
+  |/---+++++-+---+-+++-\\        |  |     /---+---------------+---+----+-+--+-+------+--++-++----+---++-+-------+---+----+++-+-+--+------+\\||| |
+  \\+---+++++-+---+-/|| |        |  |     |   |               |/--+----+-+--+-+------+--++-++----+---++-+-----\\ |   |    ||| | |  |      ||||| |
+   |   \\++++-+---+--++-+--------+--+-----+---+---------------++--+----+-+--+-+------+--+/ ||    |   || |     | |   |    ||| | |  \\------++++/ |
+   |    |||| |   |  || |        |  |     |   |               ||  |    | |  \\-+------+--+--++----/   || |     | |   |    ||| | |         ||||  |
+   |    |||| |   |  || |        |  |     v   |               ||  |    | |    |      |  |  \\+--------++-+-----+-+---+----/|| | |         ||||  |
+   |    ||\\+-+---+--++-+--------+--/     |   |               ||  |    | |    |      |  |   |        |\\-+-----+-+---/     || | |         ||||  |
+   |    || | |   |  \\+-+--------+--------+---+---------------++--+----+-+----+------+--+---+--------+--+----<+-+---------++-+-/         ||||  |
+   |    || | |   |   \\-+--------+--------+---+---------------++--+----+-+----+------+--/   |        |  |     | |         || \\-----------++/|  |
+   |    \\+-+-+---+-----+--------+--------+---+---------------++--+----+-+----+------+------/        |  |     | |         ||             || |  |
+   |     | | |   |     |        |        \\---+---------------++--+----+-+----+------+---------------+--+-----+-+---------++-------------+/ |  |
+   |     | | |   |     |        |            |               ||  |    | |    |      |               |  |     | |         ||             |  |  |
+   |     | | \\---+-----+--------+------------+---------------++--+----+-+----+------+---------------+--+-----+-/         ||             |  |  |
+   |     | |     \\-----+--------+------------+---------------++--+----+-+----/      |               |  |     |           ||             |  |  |
+   |     | |           |        |            |    /----------++--+----+-+-----------+---------------+--+-----+-----------++----\\        |  |  |
+   |     | |           |        |            |    |          \\+--+----+-+-----------+---------------+--+-----+-----------/|    |        |  |  |
+   \\-----+-+-----------/        |            |    |           \\--+----+-+-----------+---------------+--+-----/            |    |        |  |  |
+         | |                    |            \\----+--------------+----+-+-----------+---------------+--+------------------+----+--------+--/  |
+         | |                    |                 |              |    | |           \\---------------+--+------------------/    |        |     |
+         \\-+--------------------+-----------------+--------------+----/ |                           |  |                       |        |     |
+           |                    |                 |              |      \\---------------------------/  |                       |        |     |
+           \\--------------------/                 |              \\-------------------------------------+-----------------------+--------/     |
+                                                  |                                                    \\-----------------------+--------------/
+                                                  \\----------------------------------------------------------------------------/")
+ 
+ (defun read-map (stream)
+   (loop for line = (read-line stream nil) and y from 0
+         for grid = (make-array (list (length line) 1) :adjustable t :element-type 'symbol :initial-element nil)
+           then (adjust-array grid
+                              (list (max (length line) (array-dimension grid 0))
+                                    (1+ (array-dimension grid 1)))
+                              :element-type 'symbol
+                              :initial-element nil)
+         with carts
+         while line
+         do (loop for char across line
+                  for x from 0
+                  do (setf (aref grid x y)
+                           (cond
+                             ((char= char #\/) '/)
+                             ((char= char #\\ ) '\\ )
+                             ((char= char #\-) '-)
+                             ((char= char #\|) '\|)
+                             ((char= char #\+) '+)
+                             ((char= char #\space) nil)
+                             ((char= char #\^) (push (list '^ x y 'left) carts) '\|)
+                             ((char= char #\v) (push (list 'v x y 'left) carts) '\|)
+                             ((char= char #\<) (push (list '< x y 'left) carts) '\-)
+                             ((char= char #\>) (push (list '> x y 'left) carts) '\-)
+                             (t (error "Unknown character ~a." char)))))
+         finally (return (cons grid carts))))
+ 
+ (defun print-map (map)
+   (let* ((grid (car map))
+          (carts (cdr map))
+          (array (make-array (array-dimensions grid :element-type 'character :initial-element #\.))))
+     (loop for y from 0 below (array-dimension grid 1)
+           do (loop for x from 0 below (array-dimension grid 0)
+                    do (setf (aref array x y) ())))))
+ 
+ (defun next (direction x y)
+   (ecase direction
+     (^ (list x (1- y)))
+     (v (list x (1+ y)))
+     (< (list (1- x) y))
+     (> (list (1+ x) y))))
+ 
+ (defvar *turns* (make-hash-table :test 'equal))
+ 
+ ;; FIXME: there’s probably a clever, fast way based on degrees or
+ ;; integers or somesuch to do this, but … hash tables work
+ (loop for (key value) in '(((^ left) (< straight))
+                            ((v left) (> straight))
+                            ((< left) (v straight))
+                            ((> left) (^ straight))
+ 
+                            ((^ straight) (^ right))
+                            ((v straight) (V right))
+                            ((< straight) (< right))
+                            ((> straight) (> right))
+ 
+                            ((^ right) (> left))
+                            ((v right) (< left))
+                            ((< right) (^ left))
+                            ((> right) (v left)))
+       do (setf (gethash key *turns*) value))
+ 
+ (define-condition cart-crash (error)
+   ((carts :initarg :carts)))
+ 
+ (defun tick (map)
+   (let* ((grid (car map))
+          (carts (sort (cdr map) (lambda (x y) (< (array-row-major-index grid (second x) (third x))
+                                                  (array-row-major-index grid (second y) (third y)))))))
+     (loop for cart in carts
+           for next = (next (first cart) (second cart) (third cart))
+           do (loop for other-cart in (remove cart carts)
+                    when (and (= (second cart) (second other-cart))
+                              (= (third cart) (third other-cart)))
+                      do (restart-case (error 'cart-crash :carts (list cart other-cart))
+                           (remove-carts ()
+                             (setf carts (remove-if (lambda (x) (or (eq x cart) (eq x other-cart))) carts)))))
+           do (ecase (apply #'aref grid next)
+                ((- \|) nil)
+                (/ (setf (first cart)
+                         (ecase (first cart)
+                           (^ '>)
+                           (v '<)
+                           (< 'v)
+                           (> '^))))
+                (\\ (setf (first cart)
+                          (ecase (first cart)
+                            (^ '<)
+                            (v '>)
+                            (< '^)
+                            (> 'v))))
+                (+ (destructuring-bind (direction turn)
+                       (gethash (list (first cart) (fourth cart)) *turns*)
+                       (setf (first cart) direction
+                             (fourth cart) turn))))
+           do (setf (second cart) (first next)
+                    (third cart) (second next)))
+     (cons grid carts)))
+ 
+ (defun solution-1 ()
+   (with-input-from-string (s *input*)
+     (handler-bind ((cart-crash (lambda (c)
+                                  (return-from solution-1
+                                    (let ((cart (first (slot-value c 'carts))))
+                                      (format nil "~a,~a" (second cart) (third cart)))))))
+       (loop for map = (read-map s) then (tick map)
+             for i from 1 to 2000))))
+ 
+ (defun solution-2 ()
+   (with-input-from-string (s *input*)
+     (handler-bind ((cart-crash (lambda (c)
+                                  (declare (ignore c))
+                                  (invoke-restart 'remove-carts))))
+       (loop for map = (read-map s) then (tick map)
+             for i from 1 to 20000
+             while (> (length (cdr map)) 1)
+             finally (return (let ((cart (first (cdr map))))
+                               (format nil "~a,~a" (second cart) (third cart))))))))

A 2018-12-14.lisp => 2018-12-14.lisp +48 -0
@@ 0,0 1,48 @@
+ (defpackage "AOC/14" (:use "CL"))
+ (in-package "AOC/14")
+ 
+ (defun turn (state)
+   (destructuring-bind (scoreboard elf-1 elf-2)
+       state
+     (multiple-value-bind (tens ones)
+         (floor (+ (aref scoreboard elf-1) (aref scoreboard elf-2)) 10)
+       (when (plusp tens)
+         (vector-push-extend tens scoreboard))
+       (vector-push-extend ones scoreboard))
+     (list scoreboard
+           (mod (+ 1 elf-1 (aref scoreboard elf-1)) (length scoreboard))
+           (mod (+ 1 elf-2 (aref scoreboard elf-2)) (length scoreboard)))))
+ 
+ (defun next-ten (n)
+   (loop for state = (list (make-array 2 :adjustable t :fill-pointer t :initial-contents '(3 7)) 0 1) then (turn state)
+         until (>= (length (first state)) n)
+         finally (dotimes (x 10 (format t "~{~a~}" (coerce (subseq (first state) n (+ n 10)) 'list)))
+                   (setf state (turn state)))))
+ 
+ (defun solution-1 ()
+   (next-ten 505961))
+ 
+ (defun turn (state)
+   (destructuring-bind (scoreboard elf-1 elf-2)
+       state
+     (multiple-value-bind (tens ones)
+         (floor (+ (aref scoreboard elf-1) (aref scoreboard elf-2)) 10)
+       (when (plusp tens)
+         (vector-push-extend tens scoreboard))
+       (vector-push-extend ones scoreboard))
+     (list scoreboard
+           (mod (+ 1 elf-1 (aref scoreboard elf-1)) (length scoreboard))
+           (mod (+ 1 elf-2 (aref scoreboard elf-2)) (length scoreboard)))))
+ 
+ (defun first-appearance (s)
+   (let ((state (list (make-array 24000000 :adjustable t :fill-pointer 2 :element-type '(integer 0 10)) 0 1)))
+     (setf (aref (first state) 0) 3
+           (aref (first state) 1) 7)
+     (loop with s = (map 'cons (lambda (x) (- (char-code x) (char-code #\0))) s)
+           do (setf state (turn state))
+           until (and (>= (length (first state)) (length s))
+                      (search s (subseq (first state) (- (length (first state)) (length s)))))
+           finally (return (search s (first state))))))
+ 
+ (defun solution-2 ()
+   (first-appearance "505961"))