-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_01_2.py
More file actions
31 lines (25 loc) · 825 Bytes
/
day_01_2.py
File metadata and controls
31 lines (25 loc) · 825 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
25
26
27
28
29
30
31
from typing import List, Optional
with open('inputs/input_01.txt', 'r') as infile:
s = [int(x) for x in infile.readlines()]
s.sort()
global_sum = 2020
def recursive_binary_search(r: List[int, ...], x: int, my_sum: int) -> Optional[int]:
if len(r) == 0:
return None
y_index = len(r) // 2
y = r[y_index]
if x + y == my_sum:
return y
elif x + y < my_sum:
return recursive_binary_search(r[y_index + 1:], x, my_sum)
elif x + y > my_sum:
return recursive_binary_search(r[:y_index], x, my_sum)
for j, z in enumerate(s):
target_sum = global_sum - z
for k, x in enumerate(s[j + 1:]):
y = recursive_binary_search(s[k + 1:], x, target_sum)
if y is not None:
break
if y is not None:
print(z, x, y, z * x * y)
break