-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
47 lines (40 loc) · 1.12 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
lines = list(map(lambda l: int(l.strip()), open('day9-input.txt').readlines()))
def is_valid(num, preamble):
seen = set()
for n in preamble:
print(n, num, num -n, seen)
if (num - n) in seen:
return True
else:
seen.add(n)
return False
def solvePart1():
preamble = lines[0:25]
msg = lines[25:]
for num in msg:
if is_valid(num, preamble):
preamble = preamble[1:]
preamble.append(num)
else:
return num
return None
part1Answer = solvePart1()
print(part1Answer)
def solvePart2():
start = 0
end_idx = start + 1
curr_sum = lines[start] + lines[end_idx]
targetSum = part1Answer
while end_idx < len(lines) and curr_sum != targetSum:
if curr_sum < targetSum:
end_idx += 1
curr_sum += lines[end_idx]
elif curr_sum > targetSum:
curr_sum -= lines[start]
start += 1
if curr_sum == targetSum:
sorted_set = sorted(lines[start:end_idx+1])
return sorted_set[0] + sorted_set[-1]
else:
return None
print(solvePart2())