-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_09_2.py
More file actions
24 lines (20 loc) · 713 Bytes
/
day_09_2.py
File metadata and controls
24 lines (20 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
with open('inputs/input_09.txt', 'r') as infile:
input_lines = infile.readlines()
numbers = [int(x) for x in input_lines]
invalid_pos = 501 # from first part
invalid_num = numbers[invalid_pos]
k = 0
candidate_list = [numbers[k]]
candidate_sum = numbers[k]
# this should require at most 2n additions
# (every number is added and removed from candidates at most once)
while k < invalid_pos:
while candidate_sum < invalid_num:
k += 1
candidate_list.append(numbers[k])
candidate_sum += numbers[k]
if candidate_sum == invalid_num:
print(min(candidate_list) + max(candidate_list))
break
remove_value = candidate_list.pop(0)
candidate_sum -= remove_value