Skip to content

Advent of Code 2024

Day 01
from operator import add
from functools import reduce

with open("day01_input.txt", "r") as f:
    data = [[int(d) for d in row.split()] for row in f.read().splitlines()]

print(
    reduce(
        add,
        [
            abs(a - b)
            for a, b in zip(sorted(r[0] for r in data), sorted(r[1] for r in data))
        ],
    )
)
from collections import Counter
from operator import add
from functools import reduce

with open("day01_input.txt", "r") as f:
    data = [[int(d) for d in row.split()] for row in f.read().splitlines()]


c = Counter(r[1] for r in data)

print(reduce(add, [c[a] * a for a in [r[0] for r in data]]))
Day 02
with open("day02_input.txt", "r") as f:
    data = [[int(d) for d in row.split()] for row in f.read().splitlines()]

safe = 0
for row in data:
    diff_safe = all(
        [abs(row[i] - row[i + 1]) in [1, 2, 3] for i in range(len(row) - 1)]
    )
    trend_safe = all([row[i] - row[i + 1] > 0 for i in range(len(row) - 1)]) or all(
        [row[i] - row[i + 1] < 0 for i in range(len(row) - 1)]
    )
    if diff_safe and trend_safe:
        safe += 1

print(safe)