2024 Day 01

This commit is contained in:
germax26 2024-12-01 21:10:07 +11:00
parent 30e4e6049b
commit f7e6dd05df
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM

16
2024/day01/day01.py Normal file
View File

@ -0,0 +1,16 @@
from collections import Counter
with open('input.txt', 'r') as f:
lines = [tuple(map(int, line.strip().split())) for line in f.readlines()]
list1 = [line[0] for line in lines]
list2 = [line[1] for line in lines]
list1.sort()
list2.sort()
differences = [abs(a-b) for a, b in zip(list1, list2)]
print(f'Part 1: {sum(differences)}')
counts = Counter(list2)
scores = [x * counts.get(x, 0) for x in list1]
print(f'Part 2: {sum(scores)}')