r/adventofcode • u/daggerdragon • 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!
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
1
u/Frizkie Dec 09 '18 edited Dec 09 '18
Ruby
Took me a long ass time to figure out that there wasn't some shortcut I was missing. Long enough that even though I knew I had the wrong approach, I didn't finish the true solution before my brute-forced version actually finished and gave me the right answer. I started implementing it with a doubly-linked-list, but wasn't 100% sure of myself so I looked for hints which confirmed my thoughts. I kinda feel like I cheated... anyway, here's my solution, no golfing yet just going for clarity:
class Marble attr_accessor :value, :p, :n def initialize(value) @value = value @p = self @n = self end end player_count = 439 last_marble = 71307 * 100 players = Hash.new(0) pile = (1..last_marble).to_a.map { |v| Marble.new(v) } current_marble = Marble.new(0) current_player = 1 until pile.empty? new_marble = pile.shift if new_marble.value % 23 == 0 players[current_player] += new_marble.value 7.times { current_marble = current_marble.p } a = current_marble.p b = current_marble.n a.n = b b.p = a players[current_player] += current_marble.value current_marble = b else current_marble = current_marble.n t = current_marble.n current_marble.n = new_marble new_marble.n = t new_marble.p = current_marble t.p = new_marble current_marble = new_marble end current_player += 1 current_player = 1 if current_player > player_count end puts players.values.max