Compare commits
8 commits
ca6047c747
...
05c7b5bfb9
| Author | SHA1 | Date | |
|---|---|---|---|
| 05c7b5bfb9 | |||
| 11168d283e | |||
| 6600f416d2 | |||
| d1a1ce775e | |||
| 8a1910e174 | |||
| 4cbd10fe4d | |||
| 04bb4c93ac | |||
| 19c4a2626f |
10 changed files with 570 additions and 1 deletions
45
2025-python/README.md
Normal file
45
2025-python/README.md
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# Advent of Code 2025
|
||||
|
||||
Solutions for #aoc2025 in Python 3 (3.13.4).
|
||||
|
||||
Programming setup:
|
||||
|
||||
- Lenovo Thinkpad T14
|
||||
- OpenSUSE Tumbleweed with labwc
|
||||
- Helix editor w/ Ruff LS
|
||||
- Vivaldi
|
||||
- Foot
|
||||
|
||||
## Help scripts
|
||||
|
||||
Display all solved puzzles:
|
||||
|
||||
python aoc.py
|
||||
|
||||
To bootstrap a new puzzle (creates `input/<day_no>.txt` and `output/day_<day_no>.py`):
|
||||
|
||||
python aoc.py <day_no> new
|
||||
|
||||
Manually copy the puzzle input from https://adventofcode.com and paste it in `input/<day_no>.txt`
|
||||
to start coding.
|
||||
|
||||
wl-paste > input/<day_no>.txt
|
||||
|
||||
Solve separate puzzle (replace `XX` with the puzzle number):
|
||||
|
||||
python -m output.day_XX
|
||||
|
||||
Solve separate puzzle using stdin (replace `XX` with the puzzle number):
|
||||
|
||||
wl-paste | python -m output.day_XX
|
||||
cat tmpfile | python -m output.day_XX
|
||||
|
||||
Execute separate puzzle on file save (replace `XX` with the puzzle number):
|
||||
|
||||
ls output/*.py | entr -c -s 'wlpaste | python -m output.day_XX'
|
||||
ls output/*.py | entr -c -s 'cat tmpfile | python -m output.day_XX'
|
||||
ls output/*.py | entr -c -r python -m output.day_XX
|
||||
|
||||
(requires `entr` and `wl-paste`, Mac users can instead use `pbpaste`. If you
|
||||
prefer X at Linux, use `xclip -selection clipboard -o`).
|
||||
|
||||
118
2025-python/aoc.py
Normal file
118
2025-python/aoc.py
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def headline(n):
|
||||
"""Print day number and name, followed by a ruler. Used by the answer decorator"""
|
||||
print(f"\nDay {int(n)} - https://adventofcode.com/{year}/day/{int(n)}\n")
|
||||
|
||||
|
||||
year = 2025
|
||||
nostrip = []
|
||||
|
||||
try:
|
||||
_, day_no, *name = sys.argv
|
||||
except ValueError:
|
||||
day_no = None
|
||||
name = None
|
||||
|
||||
Path("./input").mkdir(parents=True, exist_ok=True)
|
||||
Path("./output").mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if day_no and name:
|
||||
name = " ".join(name)
|
||||
padded_no = day_no.zfill(2)
|
||||
with open("output/day_{}.py".format(padded_no), "w") as s:
|
||||
s.write(
|
||||
f"""
|
||||
import re
|
||||
from collections import deque, Counter, defaultdict
|
||||
from heapq import heappop, heappush
|
||||
from itertools import compress, combinations, chain, permutations
|
||||
|
||||
from output import matrix, D, DD, ADJ, ints, sints, mhd, mdbg, vdbg, cw, ccw, bk
|
||||
|
||||
|
||||
def solve(data):
|
||||
p1 = None
|
||||
p2 = None
|
||||
return p1, p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
|
||||
# use dummy data
|
||||
inp = \"\"\"
|
||||
replace me
|
||||
\"\"\".strip()
|
||||
|
||||
# uncomment to instead use stdin
|
||||
# import sys; inp = sys.stdin.read().strip()
|
||||
|
||||
# uncomment to use AoC provided puzzle input
|
||||
# with open("./input/{padded_no}.txt", "r") as f:
|
||||
# inp = f.read().strip()
|
||||
|
||||
# uncomment to do initial data processing shared by part 1-2
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
os.system(f"echo {{p1}} | wl-copy")
|
||||
# print(p2)
|
||||
# os.system(f"echo {{p2}} | wl-copy")
|
||||
|
||||
# uncomment and replace 0 with actual output to refactor code
|
||||
# and ensure nonbreaking changes
|
||||
# assert p1 == 0
|
||||
# assert p2 == 0
|
||||
""".strip()
|
||||
+ "\n"
|
||||
)
|
||||
exit(0)
|
||||
|
||||
print(
|
||||
f"\n\033[95m\033[1mAdvent of Code {year}\033[0m"
|
||||
"\n###################"
|
||||
"\n\n\033[96mby Anders Englöf Ytterström\033[0m"
|
||||
)
|
||||
|
||||
|
||||
stars = 0
|
||||
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
||||
if not day_no or day_no.zfill(2) == i:
|
||||
try:
|
||||
day = __import__(
|
||||
"output.day_{}".format(i),
|
||||
globals(),
|
||||
locals(),
|
||||
["solve"],
|
||||
0,
|
||||
)
|
||||
with open(f"./input/{i}.txt", "r") as f:
|
||||
data = f.read()
|
||||
if int(i) not in nostrip:
|
||||
data = data.strip()
|
||||
headline(i)
|
||||
try:
|
||||
data = day.presolve(data)
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
p1, p2 = day.solve(data)
|
||||
except AttributeError:
|
||||
pass
|
||||
if p1:
|
||||
print(f" \033[92m1)\033[0m {p1}")
|
||||
stars += 1
|
||||
if p2:
|
||||
print(f" \033[92m2)\033[0m {p2}")
|
||||
stars += 1
|
||||
except IOError:
|
||||
pass
|
||||
except ImportError:
|
||||
pass
|
||||
if not day_no:
|
||||
print(f"\nStars: {stars}")
|
||||
print("".join("*" if n < stars else "•" for n in range(24)))
|
||||
print("")
|
||||
209
2025-python/output/__init__.py
Normal file
209
2025-python/output/__init__.py
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
import re
|
||||
|
||||
# Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT
|
||||
D = [
|
||||
(-1, 0),
|
||||
(0, 1),
|
||||
(1, 0),
|
||||
(0, -1),
|
||||
]
|
||||
|
||||
Di = [
|
||||
(-1, -1),
|
||||
(-1, 1),
|
||||
(1, -1),
|
||||
(1, 1),
|
||||
]
|
||||
|
||||
# Directions for 2D matrices, as a dict with keys U, R, D, L
|
||||
DD = {
|
||||
"U": (-1, 0),
|
||||
"R": (0, 1),
|
||||
"D": (1, 0),
|
||||
"L": (0, -1),
|
||||
}
|
||||
|
||||
DDa = {
|
||||
"^": (-1, 0),
|
||||
">": (0, 1),
|
||||
"v": (1, 0),
|
||||
"<": (0, -1),
|
||||
}
|
||||
|
||||
# Adjacent relative positions including diagonals for 2D matrices, in the order NW, N, NE, W, E, SW, S, SE
|
||||
ADJ = [
|
||||
(-1, -1),
|
||||
(-1, 0),
|
||||
(-1, 1),
|
||||
(0, -1),
|
||||
(0, 1),
|
||||
(1, 1),
|
||||
(1, 0),
|
||||
(1, -1),
|
||||
]
|
||||
|
||||
|
||||
def ints(s):
|
||||
"""Extract all integers from a string"""
|
||||
return [int(n) for n in re.findall(r"\d+", s)]
|
||||
|
||||
|
||||
def sints(s):
|
||||
"""Extract all signed integers from a string"""
|
||||
return [int(n) for n in re.findall(r"-?\d+", s)]
|
||||
|
||||
|
||||
def mhd(a, b):
|
||||
"""Calculates the Manhattan distance between 2 positions in the format (y, x) or (x, y)"""
|
||||
ar, ac = a
|
||||
br, bc = b
|
||||
return abs(ar - br) + abs(ac - bc)
|
||||
|
||||
|
||||
def grid(d, o="#"):
|
||||
"""Transform a string into an iterable matrix. Returns the matrix, row count and col count"""
|
||||
m = [tuple(r) for r in d.split()]
|
||||
return set([(r, c) for c in range(len(m[0])) for r in range(len(m)) if m[r][c] == o])
|
||||
|
||||
|
||||
def matrix(d):
|
||||
"""Transform a string into an iterable matrix. Returns the matrix, row count and col count"""
|
||||
m = [tuple(r) for r in d.split()]
|
||||
return m, len(m), len(m[0])
|
||||
|
||||
|
||||
def mdbg(m):
|
||||
"""Print-debug a matrix"""
|
||||
for r in m:
|
||||
print("".join(r))
|
||||
|
||||
|
||||
def vdbg(seen, h, w, C="#", M=None):
|
||||
"""Print-debug visited positions of a matrix"""
|
||||
def _m(r, c):
|
||||
return M[r][c] if M else "."
|
||||
for r in range(h):
|
||||
print("".join([C if (r, c) in seen else _m(r, c) for c in range(w)]))
|
||||
|
||||
|
||||
def vvdbg(seen, h, w):
|
||||
"""Print-debug visited positions of a matrix, with values"""
|
||||
for r in range(h):
|
||||
print("".join([seen[(r, c)] if (r, c) in seen else "." for c in range(w)]))
|
||||
|
||||
|
||||
def cw(y, x):
|
||||
"""Flip a (y, x) direction counterwise: U->R, R->D, D->L, L->U.
|
||||
|
||||
>>> cw(-1, 0)
|
||||
(0, 1)
|
||||
>>> cw(0, 1)
|
||||
(1, 0)
|
||||
>>> cw(1, 0)
|
||||
(0, -1)
|
||||
>>> cw(0, -1)
|
||||
(-1, 0)
|
||||
"""
|
||||
return (x, y) if y == 0 else (x, -y)
|
||||
|
||||
|
||||
def ccw(y, x):
|
||||
"""Flip a (y, x) direction counterwise: U->L, L->D, D->R, R->U.
|
||||
|
||||
>>> ccw(-1, 0)
|
||||
(0, -1)
|
||||
>>> ccw(0, -1)
|
||||
(1, 0)
|
||||
>>> ccw(1, 0)
|
||||
(0, 1)
|
||||
>>> ccw(0, 1)
|
||||
(-1, 0)
|
||||
"""
|
||||
return (x, y) if x == 0 else (-x, y)
|
||||
|
||||
|
||||
def bfs(S, E=None):
|
||||
"""BFS algorithm, equal weighted nodes"""
|
||||
seen = set()
|
||||
q = [(S, 0)]
|
||||
g = {} # graph, required to be provided at some point
|
||||
while q:
|
||||
m, w = q.pop(0)
|
||||
if m in seen:
|
||||
continue
|
||||
seen.add(m)
|
||||
# investigate here
|
||||
for s in g[m]:
|
||||
q.append((s, w + 1))
|
||||
# return insights
|
||||
|
||||
|
||||
def mhd_search(r, c, R=20):
|
||||
"""returns all coords that are within R manhattan distance from (r,c)"""
|
||||
p = set()
|
||||
for d in range(1, R + 1):
|
||||
p.add((r, c + d))
|
||||
p.add((r, c - d))
|
||||
p.add((r + d, c))
|
||||
p.add((r - d, c))
|
||||
for dd in range(d):
|
||||
p.add((r - dd, c - d + dd))
|
||||
p.add((r + dd, c - d + dd))
|
||||
p.add((r - dd, c - dd + d))
|
||||
p.add((r + dd, c - dd + d))
|
||||
return p
|
||||
|
||||
|
||||
def dijkstras(grid, start, target):
|
||||
"""
|
||||
1. Create an array that holds the distance of each vertex from the starting
|
||||
vertex. Initially, set this distance to infinity for all vertices except
|
||||
the starting vertex which should be set to 0.
|
||||
2. Create a priority queue (heap) and insert the starting vertex with its
|
||||
distance of 0.
|
||||
3. While there are still vertices left in the priority queue, select the vertex
|
||||
with the smallest recorded distance from the starting vertex and visit its
|
||||
neighboring vertices.
|
||||
4. For each neighboring vertex, check if it is visited already or not. If it
|
||||
isn’t visited yet, calculate its tentative distance by adding its weight
|
||||
to the smallest distance found so far for its parent/previous node
|
||||
(starting vertex in case of first-level vertices).
|
||||
5. If this tentative distance is smaller than previously recorded value
|
||||
(if any), update it in our ‘distances’ array.
|
||||
6. Finally, add this visited vertex with its updated distance to our priority
|
||||
queue and repeat step-3 until we have reached our destination or exhausted
|
||||
all nodes.
|
||||
"""
|
||||
import heapq
|
||||
|
||||
target = max(grid)
|
||||
seen = set()
|
||||
queue = [(start, 0)]
|
||||
while queue:
|
||||
cost, pos, direction, steps = heapq.heappop(queue)
|
||||
y, x = pos
|
||||
dy, dx = direction
|
||||
|
||||
if pos == target:
|
||||
return cost
|
||||
|
||||
if ((pos, "and stuff")) in seen:
|
||||
continue
|
||||
|
||||
seen.add((pos, "and stuff"))
|
||||
|
||||
neighbors = []
|
||||
for n in neighbors:
|
||||
heapq.heappush(queue, ("stuffs"))
|
||||
|
||||
return -1
|
||||
|
||||
|
||||
def bk(graph, p, r=set(), x=set()):
|
||||
"""Bron-Kerbosch algoritm, no pivot: https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm"""
|
||||
if not p and not x:
|
||||
yield r
|
||||
while p:
|
||||
v = p.pop()
|
||||
yield from bk(graph, p & set(graph[v]), r | {v}, x & graph[v])
|
||||
x.add(v)
|
||||
29
2025-python/output/day_01.py
Normal file
29
2025-python/output/day_01.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
def solve(data):
|
||||
d = 50
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
for s in data.split():
|
||||
sf0 = d == 0
|
||||
m, T = (-1, 99) if s[0] == "L" else (1, 1)
|
||||
for _ in range(int(s[1:])):
|
||||
d = (d + m) % 100
|
||||
if d == T:
|
||||
if sf0:
|
||||
sf0 = False
|
||||
continue
|
||||
p2 += 1
|
||||
p1 += d == 0
|
||||
return p1, p1 + p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/01.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 1195
|
||||
assert p2 == 6770
|
||||
35
2025-python/output/day_02.py
Normal file
35
2025-python/output/day_02.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import re
|
||||
|
||||
from output import ints
|
||||
|
||||
|
||||
def solve(data):
|
||||
p1 = set()
|
||||
p2 = set()
|
||||
R = re.compile(r"^(\w+)\1+$")
|
||||
for line in data.split(","):
|
||||
a, b = ints(line)
|
||||
for n in range(a, b + 1):
|
||||
s = str(n)
|
||||
ls = len(s)
|
||||
for seq in re.findall(R, s):
|
||||
sqrts = [i for i in range(1, ls + 1) if ls % i == 0]
|
||||
for t in sqrts:
|
||||
if "".join([seq] * t) == s:
|
||||
if t == 2:
|
||||
p1.add(n)
|
||||
p2.add(n)
|
||||
return sum(p1), sum(p2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/02.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 38437576669
|
||||
assert p2 == 49046150754
|
||||
31
2025-python/output/day_03.py
Normal file
31
2025-python/output/day_03.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
def solve(data):
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
for line in data.splitlines():
|
||||
p1 += _maxj(line, 2)
|
||||
p2 += _maxj(line, 12)
|
||||
return p1, p2
|
||||
|
||||
|
||||
def _maxj(line, C):
|
||||
toexcl = len(line) - C
|
||||
batt = []
|
||||
for c in line:
|
||||
while toexcl and batt and batt[-1] < c:
|
||||
toexcl -= 1
|
||||
batt.pop()
|
||||
batt.append(c)
|
||||
return sum(10**x * int(y) for x, y in zip(range(C - 1, -1, -1), batt))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/03.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 17430
|
||||
assert p2 == 171975854269367
|
||||
31
2025-python/output/day_04.py
Normal file
31
2025-python/output/day_04.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from output import grid, ADJ
|
||||
|
||||
|
||||
def solve(data):
|
||||
p1 = 0
|
||||
p2 = set()
|
||||
G = grid(data, o="@")
|
||||
while True:
|
||||
for r, c in G:
|
||||
if sum((r + dy, c + dx) in G for dy, dx in ADJ) < 4:
|
||||
p2.add((r, c))
|
||||
if p1 == 0:
|
||||
p1 = len(p2)
|
||||
if not G & p2:
|
||||
break
|
||||
G = G - p2
|
||||
p2 = len(p2)
|
||||
return p1, p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/04.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 1493
|
||||
assert p2 == 9194
|
||||
31
2025-python/output/day_05.py
Normal file
31
2025-python/output/day_05.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from output import ints
|
||||
|
||||
|
||||
def solve(data):
|
||||
p1 = None
|
||||
p2 = 0
|
||||
ranges, ids = data.split("\n\n")
|
||||
ids = ints(ids)
|
||||
R = [ints(line) for line in ranges.split()]
|
||||
p1 = sum(any(s <= n <= e for s, e in R) for n in ids)
|
||||
c = 0
|
||||
for s, e in sorted(R):
|
||||
s = max(c + 1, s)
|
||||
if s <= e:
|
||||
p2 += e - s + 1
|
||||
c = max(e, c)
|
||||
|
||||
return p1, p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/05.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 509
|
||||
assert p2 == 336790092076620
|
||||
40
2025-python/output/day_06.py
Normal file
40
2025-python/output/day_06.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import re
|
||||
from math import prod
|
||||
|
||||
|
||||
def solve(data):
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
rows = data.splitlines()
|
||||
J = max(len(r) for r in rows) + 1
|
||||
rows = [r.ljust(J) for r in rows]
|
||||
ops = rows.pop()
|
||||
s = 0
|
||||
for ows in re.findall(r"(\S\s+)", ops):
|
||||
o, *ws = ows
|
||||
e = s + len(ws)
|
||||
col = [ns for ns in [r[s:e] for r in rows]]
|
||||
s += len(ows)
|
||||
col1 = [int(r) for r in col]
|
||||
col2 = [int("".join([nsc for nsc in ns if nsc.isdigit()])) for ns in zip(*col)]
|
||||
match o:
|
||||
case "*":
|
||||
p1 += prod(col1)
|
||||
p2 += prod(col2)
|
||||
case "+":
|
||||
p1 += sum(col1)
|
||||
p2 += sum(col2)
|
||||
return p1, p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/06.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
|
||||
assert p1 == 5524274308182
|
||||
assert p2 == 8843673199391
|
||||
|
|
@ -8,7 +8,7 @@ RUN pip install waitress==3.0.2
|
|||
|
||||
FROM reqs AS app
|
||||
RUN mkdir /app/templates
|
||||
COPY *.jinja2 /app/templates
|
||||
COPY templates/*.jinja2 /app/templates
|
||||
COPY app.py app.py
|
||||
|
||||
ENV AOC_TOKEN=
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue