blob: 3a300b9a190a5a8d0afdb2f4340fe25cec998ed9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#advent of code 2018
#day 09
#part 1 and part 2
from collections import deque
import time
#parsing input is hardcoded for 1st and 7th word in text
f = open("input.txt", 'r');
inputline = f.readline().split();
playercount = int(inputline[0]); #input1
lastmarble = int(inputline[6]); #input2
testinput = False;
if testinput:
playercount = 30; # 09 / 10 / 13 / 17 / 21 / 30
lastmarble = 5807; # 25 / 1618 / 7999 / 1104 / 6111 / 5807
# 32 / 8317 / 146373 / 2764 / 54718 / 37305
def MarbleGame(playercount, lastmarble):
playerlist = [];
player = 1;
for p in range(playercount+1):
playerlist.append(0);
marblelist = deque([0]);
for m in range(1, lastmarble+1):
if(m%23==0):
playerlist[player] += m;
marblelist.rotate(7);
playerlist[player] += marblelist.pop();
marblelist.rotate(-1);
else:
marblelist.rotate(-1);
marblelist.append(m);
player +=1;
if (player > playercount):
player = 1;
p1 = max(playerlist);
return p1;
t1 = time.time();
part1 = MarbleGame(playercount, lastmarble);
part2 = MarbleGame(playercount, lastmarble*100);
t2 = time.time();
print("part 1 = ", part1);
print("part 2 = ", part2);
print("runtime ", round(t2-t1,3), " sec");
|