advent-of-code-2023

My solutions to AoC 2023
git clone git://git.entf.net/advent-of-code-2023
Log | Files | Refs

commit 4af8595fb520002054c5e07b0661b6135c69420b
parent 0208257989a583a9dee119a5269316a42eb39b71
Author: Lukas Henkel <lh@entf.net>
Date:   Fri,  8 Dec 2023 06:33:16 +0100

Day 8 task 2

Diffstat:
Msrc/day-8.lisp | 32++++++++++++++++++++++----------
Mt/day-8.lisp | 16+++++++++++++++-
2 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/src/day-8.lisp b/src/day-8.lisp @@ -22,16 +22,28 @@ do (setf (gethash key map) left-right) finally (return map))) +(defun walk-to-destination (instructions map starting-point test-fun) + (loop with current = starting-point + for steps from 0 + for direction = (aref instructions (mod steps (length instructions))) + for (left right) = (gethash current map) + do (setf current + (ecase direction + (#\L left) + (#\R right))) + when (funcall test-fun current) + do (return (1+ steps)))) + +(defun destination-p (field) + (char= (last-elt field) #\Z)) + (defun day-8 (input) (let ((instructions (read-line input)) (map (read-map input))) - (loop with current = "AAA" - for steps from 0 - for direction = (aref instructions (mod steps (length instructions))) - for (left right) = (gethash current map) - do (setf current - (ecase direction - (#\L left) - (#\R right))) - when (equal current "ZZZ") - do (return (1+ steps))))) + (values (if (gethash "AAA" map) + (walk-to-destination instructions map "AAA" (curry #'equal "ZZZ")) + 0) + (apply #'lcm + (loop for field string being the hash-key of map + when (char= (last-elt field) #\A) + collect (walk-to-destination instructions map field #'destination-p)))))) diff --git a/t/day-8.lisp b/t/day-8.lisp @@ -21,4 +21,18 @@ ZZZ = (ZZZ, ZZZ)") AAA = (BBB, BBB) BBB = (AAA, ZZZ) ZZZ = (ZZZ, ZZZ)") - (assert= 6 task-1))) + (assert= 6 task-1)) + + (multiple-value-bind (task-1 task-2) + (aoc:run-day 8 "LR + +11A = (11B, XXX) +11B = (XXX, 11Z) +11Z = (11B, XXX) +22A = (22B, XXX) +22B = (22C, 22C) +22C = (22Z, 22Z) +22Z = (22B, 22B) +XXX = (XXX, XXX)") + (declare (ignore task-1)) + (assert= 6 task-2)))