-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.py
63 lines (47 loc) · 1.26 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
lines = """1,2,16,19,18,0"""
# lines = """0,3,6"""
# lines = """1,3,2"""
# lines = """2,1,3"""
# lines = """1,2,3"""
# lines = """2,3,1"""
# lines = """3,2,1"""
# lines = """3,1,2"""
lines = lines.strip()
lines = [int(line) for line in lines.split(",")]
from helpers import timer
import copy
def diff(log, answer):
lastTwo = [i for i, x in enumerate(log) if x == answer][-2:]
return lastTwo[1] - lastTwo[0]
def getDiff(index, answer, diffLog):
if answer not in diffLog:
diffLog[answer] = [index]
return 0
if len(diffLog[answer]) == 1:
diff = 0
else:
diff = diffLog[answer][1] - diffLog[answer][0]
if diff not in diffLog:
diffLog[diff] = [index]
else:
diffLog[diff] = [diffLog[diff][-1], index]
return diff
def helper(lines, target):
diffLog = {n: [i] for i, n in enumerate(lines)}
log = copy.copy(lines)
answer = log[-1]
i = len(log)
while i < target:
answer = getDiff(i, answer, diffLog)
i += 1
if i % 1e6 == 0:
print(i, answer)
return answer
# @timer
def part1(lines):
return helper(lines, 2020)
# @timer
def part2(lines):
return helper(lines, 30e6)
print(f"Answer 1: {part1(lines)}")
print(f"Answer 2: {part2(lines)}")