diff --git a/advent-of-code-2018.asd b/advent-of-code-2018.asd index 451acbf..18e80af 100644 --- a/advent-of-code-2018.asd +++ b/advent-of-code-2018.asd @@ -27,7 +27,8 @@ (:file "day17") (:file "day18") (:file "day19") + (:file "day20") + (:file "day21") (:file "day23") - (:file "day25") - (:file "day20"))) + (:file "day25"))) diff --git a/day19.lisp b/day19.lisp index 9de9d51..1f99f8e 100644 --- a/day19.lisp +++ b/day19.lisp @@ -1,3 +1,4 @@ + ;;;; day19.lisp (in-package :advent-of-code-2018) diff --git a/day21.lisp b/day21.lisp new file mode 100644 index 0000000..8ea34e6 --- /dev/null +++ b/day21.lisp @@ -0,0 +1,24 @@ +;;;; day21.lisp + +(in-package :advent-of-code-2018) + +;;; works for my input +;;; how to find the correct register? would need more puzzle inputs probably +(defun day21-next (state) + (loop + :while (day19-execute! state) + :until (= 28 (pstate-ip state)) + :finally (return (aref (pstate-registers state) 1)))) + +;;; Part 2 slow +;;; further reassembling the code by hand would probably allow for optimization +(defun day21 () + (let* ((state (day19-initialize-program (day19-parse-input (puzzlefile 21)))) + (first-value (day21-next state))) + (format t "A value of ~a ends the program fastest.~%" first-value) + (loop :with seen := (make-hash-table) + :for prev-value := nil :then next-value + :for next-value := first-value :then (day21-next state) + :until (gethash next-value seen nil) + :do (setf (gethash next-value seen) t) + :finally (format t "A value of ~a ends the program the latest.~%" prev-value))))