advent/2018/09.py

30 lines
899 B
Python
Raw Permalink Normal View History

2018-12-13 00:33:01 +00:00
from collections import deque, Counter
2018-12-11 00:37:50 +00:00
import re
def simulate(num_players, last_marble):
circle = deque([0])
2018-12-13 00:33:01 +00:00
players = Counter()
2018-12-11 00:37:50 +00:00
current_turn = 1
while current_turn <= last_marble:
2018-12-13 00:33:01 +00:00
current_player = (current_turn % num_players) - 1
2018-12-11 00:37:50 +00:00
if current_turn % 23 == 0:
players[current_player] += current_turn
circle.rotate(7)
players[current_player] += circle.popleft()
else:
circle.rotate(-2)
circle.appendleft(current_turn)
current_turn += 1
2018-12-13 00:33:01 +00:00
return players.most_common()[0][1]
2018-12-11 00:37:50 +00:00
with open('data/09.txt') as f:
m = re.match(r'(\d+) players; last marble is worth (\d+) points', f.read())
num_players = int(m.groups()[0])
last_marble = int(m.groups()[1])
print(f'Part 1: {simulate(num_players, last_marble)}\n')
print(f'Part 2: {simulate(num_players, last_marble * 100)}\n')