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!

21 Upvotes

283 comments sorted by

View all comments

Show parent comment

2

u/anamexis Dec 10 '18 edited Dec 10 '18

I also just wrapped up a clojure solution with mutable data structures.

It implements a circular doubly-linked list, using atoms for next and prev refs. Performance isn't the best - it computes the second answer in about 19 seconds.

;; implement a circular doubly linked list

(defn make-el [p n v]
  {:prev (atom p) :next (atom n) :val v})

(defn init-el [v]
  (let [el (make-el nil nil v)]
    (reset! (:prev el) el)
    (reset! (:next el) el)
    el))

(defn insert-after [{anext :next :as prev} v]
  (let [next @anext el (make-el prev next v)]
    (reset! (:prev next) el)
    (reset! (:next prev) el)
    el))

(defn remove-el [{:keys [prev next]}]
  (reset! (:next @prev) @next)
  (reset! (:prev @next) @prev)
  @next)

(defn traverse [el n]
  (cond (= n 0) el
        (< n 0) (recur @(:prev el) (inc n))
        (> n 0) (recur @(:next el) (dec n))))

;; marble actions
;; return tuple of [current marble, points scored]

(defn place-marble [cur val]
  [(insert-after (traverse cur 1) val) 0])

(defn remove-marble [cur val]
  (let [removing (traverse cur -7)]
    [(remove-el removing)
     (+ (:val removing) val)]))

(defn multiple? [x y]
  (and (not (zero? x))
       (zero? (mod x y))))

(defn turn [cur val]
  (if (multiple? val 23)
    (remove-marble cur val)
    (place-marble cur val)))

(defn turn-reducer [[scores cur] val]
  (let [cur-player (mod val (count scores))
        [next-cur scored] (turn cur val)]
    [(update scores cur-player #(+ % scored))
     next-cur]))

(defn play [n-players max-marble]
  (let [scores (vec (repeat n-players 0))]
    (reduce turn-reducer
            [scores (init-el 0)]
            (range 1 (inc max-marble)))))

(defn max-score [n-players max-marble]
  (->> (play n-players max-marble)
       first
       (apply max)))

(def answer1 (max-score 459 71320))
(def answer2 (max-score 459 7132000))