commit 4af8595fb520002054c5e07b0661b6135c69420b from: Lukas Henkel date: Fri Dec 08 05:39:55 2023 UTC Day 8 task 2 commit - 0208257989a583a9dee119a5269316a42eb39b71 commit + 4af8595fb520002054c5e07b0661b6135c69420b blob - 975f5f85ccebdfe07e0127c43fd49d2d4e1fa93f blob + 9f0a8cff3c62f7a8d93f2213b9baa570b239843e --- src/day-8.lisp +++ 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)))))) blob - 6525fd470b0a7ac48ebaf4524046fc026916d302 blob + c148247ceec030b4af7522e7d61056d012794527 --- t/day-8.lisp +++ 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)))