2018/Day9/Day9.py

31 lines
725 B
Python
Raw Normal View History

2018-12-09 13:36:34 +00:00
from sys import argv, exit
from blist import blist
try:
PLAYER_COUNT = int(argv[1])
LAST_MARBLE = int(argv[2])
except (IndexError, ValueError):
print(f"Usage: {argv[0]} [player_count] [marble_count]")
exit(1)
marbles = blist([0, 2, 1])
current_id = 1
scores = [0] * PLAYER_COUNT
player = 2
for next_marble in range(3, LAST_MARBLE + 1):
if next_marble % 23 == 0:
current_id = (current_id - 7) % len(marbles)
scores[player] += next_marble + marbles.pop(current_id)
else:
current_id += 2
if current_id > len(marbles):
current_id %= len(marbles)
marbles.insert(current_id, next_marble)
player = (player + 1) % PLAYER_COUNT
print(max(scores))