-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.py
55 lines (39 loc) · 1.16 KB
/
day01.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
"""
Advent of Code 2024, Day 1: Historian Hysteria.
See: https://adventofcode.com/2024/day/1
"""
import sys
from collections import defaultdict
from typing import TextIO
def part_one(file: TextIO) -> int:
"""
Solve part one of the puzzle.
"""
left, right = [], []
for line in file.readlines():
a, b = line.split(maxsplit=1)
left.append(int(a))
right.append(int(b))
left, right = sorted(left), sorted(right)
return sum(map(lambda n: abs(n[0] - n[1]), zip(left, right)))
def part_two(file: TextIO) -> int:
"""
Solve part two of the puzzle.
"""
left, right = [], defaultdict(lambda: 0)
for line in file.readlines():
a, b = line.split(maxsplit=1)
left.append(int(a))
right[int(b)] += 1
return sum(map(lambda n: n * right[n], left))
def main():
"""
The main entrypoint for the script.
"""
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()