with open('data-2024-01.txt', 'r') as f:
inp = f.read().splitlines()Advent of Code 2024 Day 1
— Day 1: Historian Hysteria —
Help the senior historians find the chief historian.
There are lists of locations to check.
Your actual left and right lists contain many location IDs. What is the total distance between your lists?
This seems simple with sorting lists
L = []
R = []
for i in inp:
x,y = i.split()
L.append(int(x))
R.append(int(y))
L.sort()
R.sort()
summ = 0
for l,r in zip(L, R):
summ += abs(l-r)
summ
# 23677732367773
— Part Two —
Some numbers appear in both lists.
Once again consider your left and right lists. What is their similarity score?
from collections import Counter
summ = 0
r_count = Counter(R)
for l in L:
if l in r_count:
summ += l * r_count[l]
summ
# 2127193921271939