From 9b0f3590a890d8026e90e20ddac6fb465c2e5830 Mon Sep 17 00:00:00 2001 From: Adrian Braemer <11058200+NobodysHero@users.noreply.github.com> Date: Fri, 4 Jan 2019 22:38:41 +0100 Subject: [PATCH] Day 21 Added solution for day 21. This only works for my registers so probably won't do for other inputs. --- advent-of-code-2018.asd | 5 +++-- day19.lisp | 1 + day21.lisp | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 day21.lisp 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))))