-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
38 lines (31 loc) · 850 Bytes
/
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
with open("input/day9", "r") as input:
data = input.read().split("\n")
data.pop()
data = [int(entry) for entry in data]
def checkPreamble(list, number):
for n in list:
if number - n in list:
return True
return False
def part1():
for idx in range(25, len(data)):
if not checkPreamble(data[idx-25:idx], data[idx]):
return data[idx]
invalid = part1()
idxStart = 0
idxEnd = 0
acc = 0
while idxStart < len(data):
if acc == invalid:
break
elif acc < invalid:
acc += data[idxEnd]
idxEnd += 1
else:
while acc > invalid and idxEnd > idxStart:
acc -= data[idxStart]
idxStart += 1
pt2_sorted = data[idxStart:idxEnd]
pt2_sorted.sort()
print("Part 1: " + str(invalid))
print("Part 2: " + str(pt2_sorted[0] + pt2_sorted[-1]))