-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass-day-9-2.py
43 lines (34 loc) · 1.11 KB
/
ass-day-9-2.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
from itertools import combinations
def is_valid(idx, preamble_size, sequence):
if idx<preamble_size:
return None
preamble = sequence[idx-preamble_size:idx]
cmbnts = [pair for pair in combinations(preamble, 2) if sum(pair) == sequence[idx]]
return cmbnts
# Read input
f = open("ass-day-9-input.txt", "r")
lines = f.read().split("\n")
numbers = [int(line) for line in lines]
preamble_sz = 25
for idx, n in enumerate(numbers):
if idx<preamble_sz:
print(f"{idx} - {numbers[idx]} -")
continue
v= is_valid(idx, preamble_sz, numbers)
if len(v)==0:
print(f"{idx} -{numbers[idx]} - {len(v)>0}")
break
n = numbers[:idx]
invalid_number = numbers[idx]
for i in range(len(n)-1):
for j in range (i+1, len(n)-1):
contiguous_set = n[i:j]
s = sum(n[i:j])
if s == invalid_number:
print("---------------------------")
print ("FOUND")
print(contiguous_set)
print(max(contiguous_set) + min(contiguous_set))
print("---------------------------")
elif s > invalid_number:
break