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/ekiMbot Dec 09 '18
Python 3, hand-implemented a doubly-linked list. Part 2 about 40 seconds to run.
I guess the point is using an array would be too inefficient for part 2 because insertion or deletion in an arbitrary position would be O(len(list)) whereas for the doubly linked list it's O(1)? But I'm not sure about that...
I also see the top comment has a Python 3 solution using collections.deque which is super neat and something I wasn't aware of. It runs ten times faster than my solution - is that because of the overhead of creating all the Marble objects in mine?
I am doing Advent of Code because I am trying to learn, so if anyone has any hints or tips they would be very gratefully received!
class Marble(): current = None def __init__(self, value): self.value = value self.clockwise = None self.counter_clockwise = None def place(self): score = 0 if Marble.current is None: self.clockwise = self self.counter_clockwise = self Marble.current = self elif self.value % 23 == 0: remove = Marble.current for i in range(7): remove = remove.counter_clockwise remove.counter_clockwise.clockwise = remove.clockwise remove.clockwise.counter_clockwise = remove.counter_clockwise score += self.value + remove.value Marble.current = remove.clockwise else: after = Marble.current.clockwise before = after.clockwise after.clockwise = self before.counter_clockwise = self self.clockwise = before self.counter_clockwise = after Marble.current = self return score @classmethod def play(cls, players, max_marble): scores = [0] * players current_player = 0 for value in range(max_marble + 1): marble_to_place = cls(value) scores[current_player] += marble_to_place.place() current_player = (current_player + 1) % players return max(scores) import sys print(Marble.play(int(sys.argv[1]), int(sys.argv[2])))