Skip to content

Commit

Permalink
Day 14
Browse files Browse the repository at this point in the history
Added solution for day 14. Nice exercise for vectors with fillpointers.
  • Loading branch information
abraemer committed Dec 14, 2018
1 parent 25c7f5c commit 668ae72
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion advent-of-code-2018.asd
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
(:file "day10")
(:file "day11")
(:file "day12")
(:file "day13")))
(:file "day13")
(:file "day14")))
49 changes: 49 additions & 0 deletions day14.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
;;;; day14.lisp

(in-package :advent-of-code-2018)

(defun day14-prepare-recipes (length)
(let ((recipes
(make-array (+ length 10) :fill-pointer 0
:element-type '(mod 10)
:adjustable t)))
(vector-push 3 recipes)
(vector-push 7 recipes)
recipes))

(defun day14-digits (number)
(nreverse (loop
:for (rest digit) := (multiple-value-list (floor number 10))
:then (multiple-value-list (floor rest 10))
:collect digit
:while (> rest 0))))

(defun day14 (&optional (input 327901))
(loop :with recipes := (day14-prepare-recipes input)
:for elf1 := 0 :then (mod (+ elf1 1 recipe1) (length recipes))
:for elf2 := 1 :then (mod (+ elf2 1 recipe2) (length recipes))
:for recipe1 := (aref recipes elf1)
:for recipe2 := (aref recipes elf2)
:do (dolist (new-recipe (day14-digits (+ recipe1 recipe2)))
(vector-push new-recipe recipes))
:while (< (length recipes) (array-dimension recipes 0))
:finally (format t "Answer Part1: ~a~%"
(map 'string #'digit-char (subseq recipes input)))))

(defun day14-extra (&optional (input 327901))
(loop :with recipes := (day14-prepare-recipes 21000000)
:with code := (day14-digits input)
:with at := 0
:for elf1 := 0 :then (mod (+ elf1 1 recipe1) (length recipes))
:for elf2 := 1 :then (mod (+ elf2 1 recipe2) (length recipes))
:for recipe1 := (aref recipes elf1)
:for recipe2 := (aref recipes elf2)
:do (dolist (new-recipe (day14-digits (+ recipe1 recipe2)))
(vector-push-extend new-recipe recipes 10000)
(cond
((null (nth at code)) (incf at))
((eql (nth at code) new-recipe) (incf at))
(t (setf at 0))))
:while (nth at code)
:finally (format t "Answer Part2: ~a~%"
(- (length recipes) at))))

0 comments on commit 668ae72

Please sign in to comment.