Day 1 - Advent of Code 2024

Working solutions for the day 1 puzzles.

Part One

""" day_01_01.py """

# usage: python3 day_01_01.py < input

import sys


with sys.stdin as infile:
    ids = [map(int, line.split()) for line in infile]

lhs, rhs = zip(*ids)
lhs = sorted(lhs)
rhs = sorted(rhs)

print(sum(abs(x - y) for x, y in zip(lhs, rhs)))

Part Two

""" day_01_02.py """

# usage: python3 day_01_02.py < input

import sys


with sys.stdin as infile:
    ids = [map(int, line.split()) for line in infile]

lhs, rhs = zip(*ids)

print(sum(x * rhs.count(x) for x in lhs))