-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.py
45 lines (35 loc) · 1.2 KB
/
15.py
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
with open("15.txt") as f:
lines=f.read().splitlines()
def part1():
print("Starting part one...")
startingNumbers = [int(i) for i in lines[0].split(",")]
dic={v:(-1,i+1) for i,v in enumerate(startingNumbers)}
last=startingNumbers[-1]
for i in range(8,2021):
if (dic[last][0] == -1): #number is new say zero
last =0
dic[0] = (dic[0][1],i)
else:
last=dic[last][1]-dic[last][0]
if not last in dic.keys():
dic[last] = (-1,i)
else: dic[last] = (dic[last][1],i)
print("2020th number was: ",last)
def part2():
print("Starting part two...")
startingNumbers = [int(i) for i in lines[0].split(",")]
dic={v:(-1,i+1) for i,v in enumerate(startingNumbers)}
last=startingNumbers[-1]
for i in range(8,30000000+1):
if (dic[last][0] == -1): #number is new say zero
last =0
dic[0] = (dic[0][1],i)
else:
last=dic[last][1]-dic[last][0]
if not last in dic.keys():
dic[last] = (-1,i)
else: dic[last] = (dic[last][1],i)
print("30000000th number was: ",last)
if __name__ == "__main__":
part1()
part2()