#!/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(486, 7083300)