Skip to content

Commit

Permalink
Day 21
Browse files Browse the repository at this point in the history
Added solution for day 21. This only works for my registers so probably won't do for other inputs.
  • Loading branch information
abraemer committed Jan 4, 2019
1 parent 3271e59 commit 9b0f359
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions advent-of-code-2018.asd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
(:file "day17")
(:file "day18")
(:file "day19")
(:file "day20")
(:file "day21")
(:file "day23")
(:file "day25")
(:file "day20")))
(:file "day25")))

1 change: 1 addition & 0 deletions day19.lisp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

;;;; day19.lisp

(in-package :advent-of-code-2018)
Expand Down
24 changes: 24 additions & 0 deletions day21.lisp
Original file line number Diff line number Diff line change
@@ -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))))

0 comments on commit 9b0f359

Please sign in to comment.