-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
47 lines (39 loc) · 1.09 KB
/
day9.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
file = open('input.txt', 'r')
inp = file.read()
file.close()
inp = inp.split('\n')
inp = [int(x) for x in inp]
def adds(numSet, mySum):
looked = set()
for num in numSet:
if mySum - num in looked: # We want A + B = 2020. Therefore we can see if 2020 - A to get B, and see if B is in the list
return True
looked.add(num)
return False
def partOne(inp):
preamble = 25
numSet = set()
for num in inp[:preamble]:
numSet.add(num)
for i in range(preamble, len(inp)):
curr = inp[i]
isValid = adds(numSet, curr)
if not isValid:
return curr
last = inp[i - preamble]
numSet.remove(last)
numSet.add(curr)
def partTwo(inp):
goal = 1721308972 # from part one
count = 0
contiguous = []
for num in inp:
contiguous.append(num)
count += num
while count > goal:
back = contiguous.pop(0)
count -= back
if count == goal:
print(contiguous)
return min(contiguous) + max(contiguous)
print(partTwo(inp))