Advent of Code 2024 Solutions
All solutions on this page were written by me without reading any hints or solutions.
My solutions aren’t necessarily great, but they did the job and got me the answers.
My suggestion to run these solutions:
$ cat input.txt | ./solution.py
Links to the full challenge specifications are included throughout this document. The original specifications aren’t allowed to be reproduced, so I can’t repost them here.
Day 1 [Spec]
Part 1 Solution
#!/usr/bin/env python3
from sys import stdin
pairs = [tuple(map(int, s.split())) for s in stdin.readlines()]
(a, b) = (sorted([x[0] for x in pairs]), sorted([x[1] for x in pairs]))
print(sum(abs(x - y) for (x, y) in zip(a, b)))
Part 2 Solution
#!/usr/bin/env python3
from sys import stdin
from collections import Counter
pairs = [tuple(map(int, s.split())) for s in stdin.readlines()]
cnt = Counter(x[1] for x in pairs)
print(sum(x[0] * cnt[x[0]] for x in pairs))
Day 2 [Spec]
Part 1 Solution
#!/usr/bin/env python3
from sys import stdin
from itertools import pairwise
reports = [[int(x) for x in s.split()] for s in stdin.readlines()]
reports = [r for r in reports if (r == sorted(r) or r == sorted(r, reverse=True))]
print(sum(all(0 < abs(a - b) < 4 for a, b in pairwise(r)) for r in reports))
Part 2 Solution
My first solution:
#!/usr/bin/env python3
from sys import stdin
from itertools import pairwise
solution = 0
for s in stdin.readlines():
report = [int(x) for x in s.split()]
for i in range(len(report) + 1):
r = report[:i] + report[i+1:]
if (r == sorted(r) or r == sorted(r, reverse=True)) \
and all(0 < abs(a - b) < 4 for a, b in pairwise(r)):
solution += 1
break
print(solution)
In an attempt to make a more compact solution, I reimplemented with a lambda:
#!/usr/bin/env python3
from sys import stdin
from itertools import pairwise
is_safe = lambda r: (r == sorted(r) or r == sorted(r, reverse=True)) \
and all(0 < abs(a - b) < 4 for a, b in pairwise(r))
reports = [[int(x) for x in s.split()] for s in stdin.readlines()]
print(sum(any(is_safe(r[:i] + r[i+1:]) for i in range(len(r) + 1)) for r in reports))
Day 3 [Spec]
Part 1 Solution
I cheated by using eval()
rather than parsing out the mul()
myself:
#!/usr/bin/env python3
from sys import stdin
from re import findall
from operator import mul
lines = "".join(stdin.readlines())
print(sum(eval(s) for s in findall(r"mul\([0-9]{1,3},[0-9]{1,3}\)", lines)))
Part 2 Solution
My first solution, retaining the use of eval()
:
#!/usr/bin/env python3
from sys import stdin
from re import findall
from operator import mul
lines = "".join(stdin.readlines())
matches = findall(r"(mul\([0-9]{1,3},[0-9]{1,3}\))|(do\(\))|(don't\(\))", lines)
solution = 0
do_mul = True
for a, b, c in matches:
if a and do_mul:
solution += eval(a)
elif b:
do_mul = True
elif c:
do_mul = False
print(solution)
My attempt at making a more compact solution:
#!/usr/bin/env python3
from sys import stdin
from re import findall
from operator import mul
lines = "".join(stdin.readlines())
solution = 0
do_mul = True
for s, do, _ in findall(r"(mul\([0-9]{1,3},[0-9]{1,3}\))|(do\(\))|(don't\(\))", lines):
if not s:
do_mul = do
elif do_mul:
solution += eval(a)
print(solution)