-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
42 lines (33 loc) · 1001 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
39
40
41
42
from common_py import input_access
def contains_pair(preambleSet, value):
for pair in preambleSet:
if value - int(pair) in preambleSet:
return True
return False
input = list(map(lambda x: int(x), input_access.fetch_input(9).rstrip().split("\n")))
preamble = 25
slidingPreamble = set(input[:preamble])
invalid_number = 0
for i in range(preamble, len(input)):
if not contains_pair(slidingPreamble, input[i]):
invalid_number = input[i]
print("found", input[i])
break
slidingPreamble.remove(input[i-preamble])
slidingPreamble.add(input[i])
#part2
low_end, high_end = -1, -1
for i in range(len(input) - 1):
sum = input[i]
for j in range(i + 1, len(input)):
sum = sum + input[j]
if sum == invalid_number:
print("found sum", i, j)
low_end = i
high_end = j
elif sum > invalid_number:
break
if low_end != -1 and high_end != -1:
break
interval = input[low_end:high_end + 1]
print("minmax: ", min(interval) + max(interval))