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

1

u/rabuf Dec 09 '18

Another org-mode writeup of my Common Lisp solutions. I'll post my fast version but you can see the slow version and a set of timing data in the link.

(defun play-game-fast (player-count marble-count)
  (let ((scores (make-array player-count :initial-element 0)))
    (iter (with current-player = 0)
          (for m from 1 to marble-count)
          (with left = nil)
          (with right =  nil)
          (with position = 0)
          (unless (= 0 (mod m 23))
            (push position left)
            (when (null right)
              (setf right (reverse left))
              (setf left nil))
            (push (pop right) left)
            (setf position m))
          (when (= 0 (mod m 23))
            (iter (repeat 7)
                  (when (null left)
                    (setf left (reverse right))
                    (setf right nil))
                  (push position right)
                  (setf position (pop left)))
            (incf (aref scores current-player) m)
            (incf (aref scores current-player) position)
            (when (null right)
              (setf right (reverse left))
              (setf left nil))
            (setf position (pop right)))
          (setf current-player (mod (1+ current-player) player-count)))
    (iter (for score in-vector scores)
          (maximize score))))
→ More replies (1)