-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202001.py
46 lines (33 loc) · 1.12 KB
/
aoc202001.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
"""AoC 1, 2020: Report Repair."""
# Standard library imports
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return set([int(line) for line in puzzle_input.split()])
def find_summands(numbers, target=2020):
"""Find two summands that add up to target."""
for first in numbers:
if (second := target - first) in numbers and first != second:
return first, second
def part1(data):
"""Solve part 1."""
first, second = find_summands(data)
return first * second
def part2(data):
"""Solve part 2."""
for first in data:
summands = find_summands(data, target=2020 - first)
if summands:
second, third = summands
return first * second * third
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
print("\n".join(str(solution) for solution in solutions))