advent of code 2025

Compare changes

Choose any two refs to compare.

+1
.gitignore
···
*.input
+
.mypy_cache
+41
4.py
···
+
import sys
+
from typing import List, Optional, Tuple
+
+
removed_rolls = 0
+
file = '4.input' if len(sys.argv) <= 1 else sys.argv[1]
+
grid = list(map(list, map(str.strip, open(file).readlines())))
+
+
def check_occupied(y: int, x: int) -> Optional[Tuple[int, int]]:
+
try:
+
if x >= 0 and y >= 0 and grid[y][x] == "@":
+
return (y, x)
+
except IndexError: pass
+
return None
+
+
def compute_adj(y: int, x: int) -> List[Optional[Tuple[int, int]]]:
+
return [
+
check_occupied(y-1, x-1), check_occupied(y-1, x), check_occupied(y-1, x+1),
+
check_occupied(y, x-1), check_occupied(y, x+1),
+
check_occupied(y+1, x-1), check_occupied(y+1, x), check_occupied(y+1, x+1)
+
]
+
+
def search_removable() -> List[Tuple[int, int]]:
+
removable = []
+
for y in range(len(grid)):
+
for x in range(len(grid[y])):
+
if grid[y][x] != "@": continue
+
adj = compute_adj(y, x)
+
if sum(map(bool, adj)) < 4:
+
removable.append((y, x))
+
return removable
+
+
removable = search_removable()
+
print(f'p1: {len(removable)}')
+
+
while len(removable):
+
for (y, x) in removable:
+
grid[y][x] = "."
+
removed_rolls += len(removable)
+
removable = search_removable()
+
+
print(f'p2: {removed_rolls}')
+10
4.test
···
+
..@@.@@@@.
+
@@@.@.@.@@
+
@@@@@.@.@@
+
@.@@@@..@.
+
@@.@@@@.@@
+
.@@@@@@@.@
+
.@.@.@.@@@
+
@.@@@.@@@@
+
.@@@@@@@@.
+
@.@.@@@.@.
+44
5.py
···
+
import sys
+
import heapq
+
from typing import List, Tuple, Optional
+
+
listed_in_range = 0
+
file = '5.input' if len(sys.argv) <= 1 else sys.argv[1]
+
+
ranges = []
+
numbers = None
+
for line in open(file).readlines():
+
line = line.strip()
+
if line == '':
+
numbers = []
+
elif numbers is not None:
+
numbers.append(int(line))
+
else:
+
heapq.heappush(ranges, tuple(map(int, line.split('-'))))
+
+
for num in numbers:
+
for start, end in ranges:
+
if start <= num and num <= end:
+
listed_in_range += 1
+
break
+
+
print(f'p1: {listed_in_range}')
+
+
dedup_ranges = []
+
last_start, last_end = None, None
+
while ranges:
+
start, end = heapq.heappop(ranges)
+
if last_start is None:
+
last_start, last_end = start, end
+
elif start <= last_end: # merge interval
+
# print("merge", last_start, last_end, "with", start, end)
+
last_start, last_end = last_start, max(last_end, end)
+
else: # commit interval
+
# print("commit", last_start, last_end)
+
dedup_ranges.append((last_start, last_end))
+
last_start, last_end = start, end
+
+
dedup_ranges.append((last_start, last_end))
+
# print(dedup_ranges)
+
+
print(f'p2: {sum(end - start + 1 for start, end in dedup_ranges)}')
+11
5.test
···
+
3-5
+
10-14
+
16-20
+
12-18
+
+
1
+
5
+
8
+
11
+
17
+
32
+48
6.py
···
+
import sys
+
from operator import mul, add
+
from functools import reduce
+
+
total_wordwise = 0
+
total_digitwise = 0
+
file = '6.input' if len(sys.argv) <= 1 else sys.argv[1]
+
+
columns = []
+
ops = []
+
op_line = None
+
+
for line in open(file).readlines():
+
line = line.strip('\n')
+
for word in line.split(' '):
+
if not word: continue
+
if word in '+*':
+
ops.append(word)
+
op_line = line
+
else:
+
columns.append(word)
+
+
for op_i, op in enumerate(ops):
+
column = [int(c) for i, c in enumerate(columns) if i % len(ops) == op_i]
+
if op == '+':
+
total_wordwise += sum(column)
+
elif op == '*':
+
total_wordwise += reduce(mul, column, 1)
+
+
print(f'p1: {total_wordwise}')
+
+
lines = []
+
op_pos = [i for i, ch in enumerate(op_line) if ch in '+*']
+
for line in open(file).readlines():
+
if line.strip() == op_line.strip(): break
+
words = [line[i:j][:-1] for i, j in zip([0] + op_pos[1:], op_pos[1:] + [None])]
+
lines.append(words)
+
+
for op_i, op in enumerate(ops):
+
cols = [line[i] for i in range(len(lines[0])) if i % len(ops) == op_i for line in lines]
+
digits = [col[i] for i in range(len(cols[0])) for col in cols]
+
numbers = [int(''.join(digits[i:i+len(cols)])) for i in range(0, len(digits), len(cols))]
+
if op == '+':
+
total_digitwise += sum(numbers)
+
elif op == '*':
+
total_digitwise += reduce(mul, numbers, 1)
+
+
print(f'p2: {total_digitwise}')
+4
6.test
···
+
123 328 51 64
+
45 64 387 23
+
6 98 215 314
+
* + * +