-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathday9.py
58 lines (43 loc) · 1.52 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
48
49
50
51
52
53
54
55
56
57
58
def get_first_invalid_num(nums: list[int], preamble_len: int) -> int:
for index, num in enumerate(nums):
if index >= preamble_len:
check = False
previous_nums = nums[index - preamble_len: index]
for ix, i in enumerate(previous_nums):
for jx, j in enumerate(previous_nums):
if ix > jx:
if i + j == num:
check = True
if not check:
return num
return -1
def get_continuous_set_that_sum_to_target(nums: list[int], target: int) -> list[int]:
sum_of_set = 0
left_ix = 0
right_ix = 0
# sliding window solution
while left_ix <= right_ix < len(nums):
next_num = nums[right_ix]
if sum_of_set + next_num < target:
sum_of_set += next_num
right_ix += 1
elif sum_of_set + next_num == target:
return nums[left_ix: right_ix + 1]
else:
while sum_of_set + next_num > target:
sum_of_set -= nums[left_ix]
left_ix += 1
return []
def part1(nums):
return get_first_invalid_num(nums, 25)
def part2(nums):
first_invalid_num = get_first_invalid_num(nums, 25)
continuous_set = get_continuous_set_that_sum_to_target(nums, first_invalid_num)
return min(continuous_set) + max(continuous_set)
with open('day9-data.txt') as f:
inputs = [
int(line)
for line in f.read().splitlines()
]
print(part1(inputs))
print(part2(inputs))