-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
executable file
·38 lines (30 loc) · 1.02 KB
/
day1.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
from typing import Tuple, Union
def find_combo_of_2(numbers: Union[set, list], target: int = 2020) -> Tuple[int, int]:
for number in numbers:
the_other_half = target - number
if the_other_half in numbers:
return the_other_half, number
raise ValueError("No valid combination")
def find_combo_of_3(
numbers: Union[set, list], target: int = 2020
) -> Tuple[int, int, int]:
for x in numbers:
sub_target = target - x
y, z = find_combo_of_2(numbers, sub_target)
if y and z:
return x, y, z
raise ValueError("No valid combination")
if __name__ == "__main__":
with open("test.txt") as f:
temp = f.readlines()
data = set(list(map(int, temp)))
"""
Part 1: Target is set to 2020, we look for 2 numbers that add up to target
"""
a, b = find_combo_of_2(data)
print(a * b)
"""
Part 2: Target is set to 2020, we look for 3 numbers that add up to target
"""
a, b, c = find_combo_of_3(data)
print(a * b * c)