-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday9.py
69 lines (55 loc) · 1.86 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
59
60
61
62
63
64
65
66
67
68
69
"""
https://adventofcode.com/2020/day/9
"""
def get_numbers_list(file) -> [int]:
with open(file) as f:
lines = [int(line.strip()) for line in f]
return lines
def sum_in_cycle(cycle_range: int, l: [int], num_ind: int) -> bool:
searched_sum = l[num_ind]
possibilities = set(l[num_ind - cycle_range:num_ind])
for p in possibilities:
if searched_sum - p in possibilities and searched_sum - p != searched_sum:
return True
return False
def sum_in_cycle2(cycle_range: int, l: [int], num_ind: int) -> bool:
searched_sum = l[num_ind]
possibilities = sorted(l[num_ind - cycle_range:num_ind])
i, j = 0, len(possibilities) - 1
for n in possibilities:
if possibilities[i] + possibilities[j] == searched_sum:
return True
elif possibilities[i] + possibilities[j] < searched_sum:
i += 1
elif possibilities[i] + possibilities[j] > searched_sum:
j -= 1
return False
def get_num_sum_not_in_cycle(cycle_range: int, l: [int]) -> (int, int):
for n_ind in range(cycle_range, len(nums)):
if not sum_in_cycle(cycle_range, l, n_ind):
return n_ind, l[n_ind]
return -1, -1
def find_contiguous_set(l: [int], target: int) -> [int]:
nums = [0]
start_num_ind, nth_num_ind = 0, 1
while start_num_ind < len(l) - 1:
for n in l[start_num_ind:]:
if sum(nums) <= target:
nums.append(n)
if sum(nums) == target:
return nums
nums = []
start_num_ind += 1
return nums
def get_sum(l: [int]):
return min(l) + max(l)
if __name__ == '__main__':
cycle = 25
nums = get_numbers_list('input9.txt')
print(nums)
i, n = get_num_sum_not_in_cycle(cycle, nums)
print(i, n)
cs = find_contiguous_set(nums, n)
print(cs)
res = get_sum(cs)
print(res)