blob: 3bd815567c614de677e039483f299c3e4b7308c6 (
plain)
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
|
"""Day 9."""
from itertools import permutations
with open("input") as f:
nums = [int(x) for x in f.read().rstrip().split("\n")]
preamble_length = 25
invalid_number = None
# Part 1
for idx, val in enumerate(nums[preamble_length:], start=preamble_length):
pre = nums[idx - preamble_length: idx]
valid = False
for combs in permutations(pre, 2):
if sum(combs) == val:
valid = True
if not valid:
invalid_number = val
print(invalid_number)
break
# Part 2
found = False
for x in range(len(nums)):
for y in range(x + 1, len(nums)):
subset = nums[x:y]
total = sum(subset)
if total > invalid_number:
break
if total == invalid_number:
print(min(subset) + max(subset))
found = True
break
if found:
break
|