adventofcode-2018/09/solve01.py
2018-12-15 15:00:57 +01:00

76 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
def play(players, count):
marbles = dict()
marble_current = None
score = [0] * players
def add_marble(num):
nonlocal marbles
nonlocal marble_current
item = {
'number': num,
'prev': None,
'next': None
}
marbles[num] = item
if len(marbles) == 1:
item['prev'] = num
item['next'] = num
else:
j1 = marbles[marble_current]['next']
j2 = marbles[j1]['next']
item['prev'] = j1
item['next'] = j2
marbles[j1]['next'] = num
marbles[j2]['prev'] = num
marble_current = num
# print('adding marble {} next to {} and {}'.format(num, item['prev'], item['next']))
def rule_23(round):
nonlocal players
nonlocal marbles
nonlocal score
nonlocal marble_current
j1 = marble_current
for i in range(7):
j1 = marbles[j1]['prev']
marble_current = marbles[j1]['next']
j1p = marbles[j1]['prev']
j1n = marbles[j1]['next']
marbles[j1p]['next'] = j1n
marbles[j1n]['pred'] = j1p
player = round % players
score[player] += j1
score[player] += round
# print('removed marble {} between {} and {}'.format(j1,j1p,j1n))
# print('added {} and {} to player {}'.format(j1,round, player))
for r in range(count):
if (r % 23 == 0) and (r > 0):
rule_23(r)
else:
add_marble(r)
print(max(score))
#play(10, 26)
#play(10, 1619)
#play(13, 8000)
#play(17, 1105)
#play(21, 6112)
#play(30, 5808)
play(486, 70834)