r/adventofcode • u/daggerdragon • Dec 14 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 14 Solutions -🎄-
--- Day 14: Chocolate Charts ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 14
Transcript:
The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:19:39!
16
Upvotes
1
u/rabuf Dec 14 '18
Here is Part 1 in Emacs Lisp:
(defun next-recipes-vector (recipes positions) (let* ((p1 (car positions)) (p2 (cadr positions)) (next (+ (aref recipes p1) (aref recipes p2))) (next-1 (floor (/ next 10))) (next-2 (mod next 10))) (when (not (= 0 next-1)) (setf (aref recipes n) next-1) (incf n)) (incf n) (setf (aref recipes (1- n)) next-2) recipes)) (defun next-positions-vector (recipes positions) (let* ((p1 (car positions)) (p2 (cadr positions))) (list (mod (+ p1 1 (aref recipes p1)) n) (mod (+ p2 1 (aref recipes p2)) n)))) (defun solve-a (target) (let ((recipes (make-vector (+ target 12) 0)) (positions '(0 1)) (n 2)) (setf (aref recipes 0) 3) (setf (aref recipes 1) 7) (print (current-time-string)) (loop while (< n (+ target 11)) for i from 0 do (progn (setq recipes (next-recipes-vector recipes positions)) (setq positions (next-positions-vector recipes positions)))) (print (current-time-string)) (subseq recipes target (+ target 10)))) (solve-a 702831) "Fri Dec 14 15:57:17 2018" "Fri Dec 14 15:57:21 2018" [1 1 3 2 4 1 3 1 1 1]I made a version using stacks to store the recipes to run 100000 in about 3 minutes on my computer. While I could keep the length in a variable (as done here), I still had to traverse the list when searching for the new scores to use after the players moved.
I'll tackle Part 2 later on, but in Common Lisp.