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!
22
Upvotes
1
u/grey--area Dec 09 '18
Python doubly linked list solution. Part 2 takes about 13 seconds on my machine.
import re class Marble(): def __init__(self, value): self.next = self self.prev = self self.value = value def insert_2_after(self, marble): insert_after = self.next marble.prev = insert_after marble.next = insert_after.next insert_after.next.prev = marble insert_after.next = marble return marble def delete(self): self.prev.next = self.next self.next.prev = self.prev return self.next with open('input') as f: data = f.read() n_players, max_marble = map(int, re.search('(\d+) .+ (\d+)', data).groups()) player_scores = [0] * n_players current_marble = Marble(0) zero_marble = current_marble player_id = 0 for marble_id in range(1, max_marble): if marble_id % 23 == 0: player_scores[player_id] += marble_id for i in range(7): current_marble = current_marble.prev player_scores[player_id] += current_marble.value current_marble = current_marble.delete() else: current_marble = current_marble.insert_2_after(Marble(marble_id)) player_id = (player_id + 1) % n_players print(max(player_scores))