-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.py
53 lines (37 loc) · 1022 Bytes
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Advent of Code 2024, Day 1
# (c) blu3r4y
from collections import Counter
from aocd.models import Puzzle
from funcy import print_calls, print_durations
@print_calls
@print_durations(unit="ms")
def part1(data):
left, right = data
left, right = sorted(left), sorted(right)
distances = 0
for l, r in zip(left, right):
distances += abs(l - r)
return distances
@print_calls
@print_durations(unit="ms")
def part2(data):
left, right = data
counts = Counter(right)
similarity = 0
for l in left:
similarity += l * counts[l]
return similarity
def load(data):
pairs = []
for line in data.splitlines():
nums = map(int, line.split())
pairs.append((*nums,))
return zip(*pairs)
if __name__ == "__main__":
puzzle = Puzzle(year=2024, day=1)
ans1 = part1(load(puzzle.input_data))
assert ans1 == 1765812
puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
assert ans2 == 20520794
puzzle.answer_b = ans2