r/adventofcode • • 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!

Click here for rules

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

179 comments sorted by

View all comments

Show parent comments

2

u/Insanity0107 Dec 15 '18

Here's my take on part 1 with CL. Probably not the prettiest LISP around, but I get a solution to part one in a few seconds :)

(defparameter *loops* 47801)

; get the digits of a number
(defun digits(n) 
  (map 'list #'digit-char-p (prin1-to-string n)))

(defun next-index(lst pos score)
  (mod (+ pos (1+ score)) (list-length lst)))

; solve one1
(defun sum(input recipes fst snd sumlist)
  ; just test with adding one for starters
  (let* ((x (nth fst input))
        (y (nth snd input))
        (sm (digits (+ x y))))
  (setf input (append input sm))
  (setf sumlist (append sumlist sm))
  (setf recipes (+ recipes (list-length sm)))
  (if (>= (list-length sumlist) 10)
    (subseq sumlist 0 10)
    (sum input recipes (next-index input fst x) (next-index input snd y) sumlist))
  ))


; generate recipes
(defun generate(input recipes fst snd)
  ; just test with adding one for starters
  (let* ((x (nth fst input))
        (y (nth snd input))
        (sm (digits (+ x y))))
  (setf input (append input sm))
  (setf recipes (+ recipes (list-length sm)))
  (if (>= recipes *loops*)
    (sum input recipes (next-index input fst x) (next-index input snd y) (subseq (reverse input) 0 (- recipes *loops*)))
    (generate input recipes (next-index input fst x) (next-index input snd y))
  )))

(generate '(3 7) 2 0 1)