aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x2025/07/puzzle-1.py23
-rwxr-xr-x2025/07/puzzle-2.py31
-rw-r--r--2025/08/.gitignore1
-rw-r--r--2025/08/Makefile1
-rw-r--r--2025/08/puzzles.py53
5 files changed, 109 insertions, 0 deletions
diff --git a/2025/07/puzzle-1.py b/2025/07/puzzle-1.py
new file mode 100755
index 0000000..0c30dfa
--- /dev/null
+++ b/2025/07/puzzle-1.py
@@ -0,0 +1,23 @@
+#!/usr/bin/python3
+
+
+def main() -> None:
+ with open('input', 'r') as f:
+ grid = tuple(tuple(x.strip()) for x in f.readlines())
+
+ xs = set()
+ xs.add(grid[0].index('S'))
+ cnt = 0
+
+ for row in grid[1:]:
+ for i, ch in enumerate(row):
+ if ch == '^' and i in xs:
+ cnt += 1
+ xs.remove(i)
+ xs.add(i - 1)
+ xs.add(i + 1)
+
+ print(cnt)
+
+if __name__ == '__main__':
+ main()
diff --git a/2025/07/puzzle-2.py b/2025/07/puzzle-2.py
new file mode 100755
index 0000000..3695889
--- /dev/null
+++ b/2025/07/puzzle-2.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python3
+
+import collections
+import functools
+
+
+Pos = collections.namedtuple('Pos', ['x', 'y'])
+
+
+def main() -> None:
+ with open('input', 'r') as f:
+ grid = tuple(tuple(x.strip()) for x in f.readlines())
+
+ pos = Pos(grid[0].index('S'), 0)
+ print(npaths(grid, pos))
+
+
+@functools.cache
+def npaths(grid: list[list[str]], pos: Pos) -> int:
+ if pos.y == len(grid):
+ return 1
+ if grid[pos.y][pos.x] == '^':
+ return (
+ npaths(grid, Pos(pos.x - 1, pos.y))
+ + npaths(grid, Pos(pos.x + 1, pos.y))
+ )
+ return npaths(grid, Pos(pos.x, pos.y + 1))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/2025/08/.gitignore b/2025/08/.gitignore
new file mode 100644
index 0000000..ffc46fe
--- /dev/null
+++ b/2025/08/.gitignore
@@ -0,0 +1 @@
+puzzle-[12].py
diff --git a/2025/08/Makefile b/2025/08/Makefile
new file mode 100644
index 0000000..0a4e980
--- /dev/null
+++ b/2025/08/Makefile
@@ -0,0 +1 @@
+include ../../Makefiles/py.mk
diff --git a/2025/08/puzzles.py b/2025/08/puzzles.py
new file mode 100644
index 0000000..086baca
--- /dev/null
+++ b/2025/08/puzzles.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python3
+
+import functools
+import heapq
+import itertools
+import math
+import operator
+
+
+type Point = tuple[int, int, int]
+type Circuit = set[Point]
+
+
+def main() -> None:
+ with open('input', 'r') as f:
+ boxes = [tuple(map(int, l.split(','))) for l in f.readlines()]
+
+ circuits: list[Circuit] = []
+ pairs = itertools.combinations(boxes, 2)
+ pairs = iter(sorted(pairs, key=lambda p: math.dist(*p)))
+
+# START PART 1
+ for p, q in itertools.islice(pairs, 1000):
+ connect(circuits, p, q)
+
+ lens = heapq.nlargest(3, map(len, circuits))
+ print(functools.reduce(operator.mul, lens))
+# END PART 1 START PART 2
+ while len(circuits) != 1 or len(circuits[0]) != len(boxes):
+ p, q = next(pairs)
+ connect(circuits, p, q)
+ print(p[0] * q[0])
+# END PART 2
+
+
+def connect(circuits: list[Circuit], p: Point, q: Point) -> None:
+ pc = next((c for c in circuits if p in c), None)
+ qc = next((c for c in circuits if q in c), None)
+
+ match (pc, qc):
+ case (None, None):
+ circuits.append({p, q})
+ case (x, None):
+ pc.add(q)
+ case (None, y):
+ qc.add(p)
+ case (x, y) if x is not y:
+ circuits.remove(qc)
+ pc |= qc
+
+
+if __name__ == '__main__':
+ main()