30 lines
899 B
Python
30 lines
899 B
Python
from collections import deque, Counter
|
|
import re
|
|
|
|
|
|
def simulate(num_players, last_marble):
|
|
circle = deque([0])
|
|
players = Counter()
|
|
current_turn = 1
|
|
while current_turn <= last_marble:
|
|
current_player = (current_turn % num_players) - 1
|
|
|
|
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
|
|
return players.most_common()[0][1]
|
|
|
|
|
|
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')
|