Advent of Code 2024
Published on December 2, 2024 advent-of-code christmas programmingSome of my solutions to this years advent of code.
December first 🇷🇴
col1 = [3, 4, 2, 1, 3, 3]
col2 = [4, 3, 9, 3, 5, 3]
pairs = []
while len(col1) != 0:
pairs.append([min(col1), min(col2)])
col1.pop(col1.index(min(col1)))
col2.pop(col2.index(min(col2)))
print(pairs)
distances = list(map(lambda x: abs(x[0]-x[1]), pairs))
print(distances)
December second
import operator
reports = [
[7, 6, 4, 2, 1],
[1, 2, 7, 8, 9],
[9, 7, 6, 2, 1],
[1, 3, 2, 4, 5],
[8, 6, 4, 4, 1],
[1, 3, 6, 7, 9],
]
def is_safe(report: list[int]) -> bool:
# Pairs of two.
# eg. [a, b, c] -> [[a, b], [b, c]]
pairs = list(zip(report, report[1:]))
diffs = list(map(lambda x: x[0]-x[1], pairs))
diff_ok = lambda x: x >= 1 and x <= 3
op = operator.lt
if report and not op(report[0], report[-1]):
op = operator.gt
is_monotone = all(op(x, y) for x, y in pairs)
safe_diffs = min(diff_ok(abs(d)) for d in diffs)
return is_monotone and safe_diffs
print(list(map(is_safe, reports)))
December third
import re
memory = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
commands = re.findall("mul\(([1-9]\d?|100),([1-9]\d?|100)\)", memory)
num_pairs = [re.findall("([1-9]\d?|100)", str(cmd)) for cmd in commands]
result = sum(map(lambda x: int(x[0])*int(x[1]), num_pairs))
print(result)