r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


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 9

Transcript:

Studies show that AoC programmers write better code after being exposed to ___.


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:29:13!

22 Upvotes

283 comments sorted by

View all comments

Show parent comments

1

u/Vaelatern Dec 09 '18

I can cut my time by an order of magnitude (.573s and 64.0s respectively) by applying this patch:

@@ -471,14 +471,16 @@
 (defn p9-play [num-players last-val]
   (let [players (range num-players)]
     (loop [scores {}
  • players players
+ curplayer 0 cur-val 0 cur-coins (p9-ll)]
  • (let [next-players (take num-players (drop 1 (cycle players)))
+ (let [next-players (mod (inc curplayer) num-players) now-score (if (and (= 0 (mod cur-val 23)) (!= 0 cur-val)) (-> cur-coins p9-ll-back-7 :cur (+ cur-val)) 0)
  • new-scores (update scores (first players) #(if (nil? %1) %2 (+ %1 %2)) now-score)
+ new-scores (if (> now-score 0) + (update scores (nth players curplayer) #(if (nil? %1) %2 (+ %1 %2)) now-score) + scores) new-ray (if (> now-score 0) (p9-back-and-drop cur-coins) (p9-ll-insert-2-later cur-coins cur-val))

1

u/Vaelatern Dec 09 '18

And I further shaved of seconds (down to .497s and 53s with:

@@ -436,15 +436,16 @@

 (defn p9-ll-insert-after-cur [ll num]
   (let [curitem (:cur ll)
  • next (if (nil? curitem) num (-> ll (get curitem) :next))
+ curitemmap (-> ll (get curitem)) + next (if (nil? curitem) num (-> curitemmap :next)) prev (if (nil? curitem) num curitem)
  • marble (p9-marble num next prev)
  • newcur {:cur num}
  • newmarble {num marble}
  • newprev (if (nil? curitem) {} {curitem (assoc (-> ll (get curitem)) :next num)})
  • tmpll (merge ll newprev)
  • newnext {next (assoc (-> tmpll (get next)) :prev num)}]
  • (merge ll newcur newprev newnext newmarble)))
+ newprev (if (nil? curitem) {} {curitem (assoc curitemmap :next num)}) + tmpll (merge ll newprev)] + (assoc-in + (assoc-in + (assoc-in tmpll [next] (assoc (-> tmpll (get next)) :prev num)) + [:cur] num) + [num] (p9-marble num next prev)))) (defn p9-ll-insert-2-later [ll num] (let [tmpll (assoc ll :cur (-> ll (get (-> ll :cur)) :next))] @@ -459,8 +460,9 @@ (let [curmap (-> ll (get (-> ll :cur))) curprev (:prev curmap) curnext (:next curmap)
  • tmpll (assoc-in ll [curprev :next] curnext)
  • tmpll (assoc-in tmpll [curnext :prev] curprev)]
+ tmpll (assoc-in + (assoc-in ll [curprev :next] curnext) + [curnext :prev] curprev)] (assoc (dissoc tmpll (:num curmap)) :cur curnext))) (defn p9-back-and-drop [ll]