-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_01.py
40 lines (30 loc) · 786 Bytes
/
day_01.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
def two_sum(ns: list[int], k: int) -> tuple[int, int]:
d = {}
for x in ns:
if x in d:
return x, k-x
d[k-x] = x
return 0, 0
def three_sum(ns: list[int], k: int) -> tuple[int, int, int]:
for i, x in enumerate(ns[:-2]):
res = two_sum(ns[i+1:], k-x)
if res != (0, 0):
return x, res[0], res[1]
return 0, 0, 0
def nr1(n: list[int]) -> int:
c = 1
for x in two_sum(n, 2020):
c *= x
return c
def nr2(n: list[int]) -> int:
c = 1
for x in three_sum(n, 2020):
c *= x
return c
def main():
with open("inputs/day_01.txt", "r") as f:
s = [int(el.replace("\n", "")) for el in f.readlines()]
print(nr1(s))
print(nr2(s))
if __name__ == "__main__":
main()