This commit is contained in:
Joseph Montanaro 2018-12-10 16:37:50 -08:00
parent eec8b960ea
commit 934b153829
2 changed files with 30 additions and 0 deletions

29
2018/09.py Normal file
View File

@ -0,0 +1,29 @@
from collections import deque
import re
def simulate(num_players, last_marble):
circle = deque([0])
players = [0 for i in range(num_players)]
current_turn = 1
while current_turn <= last_marble:
current_player = (current_turn % len(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 max(players)
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')

1
2018/data/09.txt Normal file
View File

@ -0,0 +1 @@
470 players; last marble is worth 72170 points