-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2020_01.py
executable file
路67 lines (52 loc) 路 1.23 KB
/
AoC2020_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
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
#! /usr/bin/env python3
#
# Advent of Code 2020 Day 1
#
import sys
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
TEST = """\
1721
979
366
299
675
1456
"""
Input = list[int]
Output1 = int
Output2 = int
class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return [int(_) for _ in input_data]
def part_1(self, numbers: Input) -> int:
seen = set[int]()
for n1 in numbers:
seen.add(n1)
n2 = 2020 - n1
if n2 in seen:
return n1 * n2
raise RuntimeError()
def part_2(self, numbers: Input) -> int:
seen = set[int]()
for i, n1 in enumerate(numbers):
seen.add(n1)
for n2 in numbers[i:]:
n3 = 2020 - n1 - n2
if n3 in seen:
return n1 * n2 * n3
raise RuntimeError()
@aoc_samples(
(
("part_1", TEST, 514_579),
("part_2", TEST, 241_861_950),
)
)
def samples(self) -> None:
pass
solution = Solution(2020, 1)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()