solve.py (1143B)
1import sys 2sys.path.append("../common") 3import aoc 4 5from collections import deque 6 7words = aoc.data.split(" ") 8playercount = int(words[0]) 9lastworth = int(words[6]) 10 11def highscore(playercount, lastworth): 12 lastworth = lastworth - lastworth % 23 13 14 players = [0 for x in range(playercount)] 15 marbles = deque([0]) 16 pos = 0 17 for i in range(1, lastworth+1): 18 if i % 23 == 0: 19 cp = (i-1) % playercount 20 players[cp] += i + marbles[(len(marbles) + pos - 7) % len(marbles)] 21 marbles.rotate((len(marbles) - 1 - pos) + 7) 22 marbles.pop() 23 pos = 0 24 else: 25 if i < 3: 26 pos = 1 27 marbles.rotate(1) 28 marbles.append(i) 29 else: 30 marbles.rotate((len(marbles)- 1 -pos) - 1) 31 marbles.append(i) 32 pos = len(marbles)-1 33 34 return max(players) 35 36 37def solve1(args): 38 return highscore(playercount, lastworth) 39 40def solve2(args): 41 return highscore(playercount, lastworth * 100) 42 43aoc.run(solve1, solve2, sols=[412127, 3482394794])