Skip to content

Commit

Permalink
day09 part1
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasrenault committed Nov 22, 2024
1 parent 047e6db commit b1b4b30
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions advent/advent2018/day09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import re
from collections import defaultdict

from advent.utils.utils import Advent

advent = Advent(9, 2018)


def main():
lines = advent.get_input_lines()
players, rounds = list(map(int, re.findall(r"([0-9]+)", lines[0])))

advent.submit(1, max(play(players, rounds).values()))


def play(players, rounds) -> dict[int, int]:
scores: dict[int, int] = defaultdict(int)
player = 0
circle = [0]
marble = 1
current = 0
while marble < rounds:
if marble % 23 == 0:
scores[player] += marble
to_pop = (current - 7) % len(circle)
scores[player] += circle.pop(to_pop)
current = to_pop
else:
to_insert = (current + 2) % len(circle)
if to_insert == 0:
circle.append(marble)
current = len(circle) - 1
else:
circle.insert(to_insert, marble)
current = to_insert

marble += 1
player = (player + 1) % players

return scores


if __name__ == "__main__":
main()

0 comments on commit b1b4b30

Please sign in to comment.