commit bc79fa83d3cb63d8445312b47dbed9295809b28a
parent 31c9f59a5f8946e8d90f2aad96e065ab4b496b67
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Thu, 4 Dec 2025 06:34:16 +0100
Day 4 2025
Diffstat:
4 files changed, 55 insertions(+), 2 deletions(-)
diff --git a/2025/04/a.py b/2025/04/a.py
@@ -1,6 +1,21 @@
import fileinput
+def neigh(a, i, j):
+ return [
+ a[i-1][j-1], a[i-1][j], a[i-1][j+1],
+ a[i][j-1], a[i][j+1],
+ a[i+1][j-1], a[i+1][j], a[i+1][j+1],
+ ].count('@')
+
+s = 0
with fileinput.input() as lines:
+ a = [['.'] * 200]
for line in lines:
- ...
+ a.append(['.'] + list(line.replace('\n', '.')))
+ a.append(['.'] * 200)
+for i in range(1,len(a)-1):
+ for j in range(1,len(a[1])-1):
+ if a[i][j] == '@' and neigh(a, i, j) < 4:
+ s += 1
+print(s)
diff --git a/2025/04/b.py b/2025/04/b.py
@@ -0,0 +1,26 @@
+import fileinput
+
+def neigh(a, i, j):
+ return [
+ a[i-1][j-1], a[i-1][j], a[i-1][j+1],
+ a[i][j-1], a[i][j+1],
+ a[i+1][j-1], a[i+1][j], a[i+1][j+1],
+ ].count('@')
+
+s = 0
+with fileinput.input() as lines:
+ a = [['.'] * 200]
+ for line in lines:
+ a.append(['.'] + list(line.replace('\n', '.')))
+ a.append(['.'] * 200)
+
+rem = True
+while rem:
+ rem = False
+ for i in range(1,len(a)-1):
+ for j in range(1,len(a[1])-1):
+ if a[i][j] == '@' and neigh(a, i, j) < 4:
+ s += 1
+ rem = True
+ a[i][j] = '.'
+print(s)
diff --git a/2025/README.md b/2025/README.md
@@ -11,6 +11,7 @@ Example
```
Day -Part 1- -Part 2-
+ 4 00:25:36 00:27:49
3 00:05:02 00:16:09
2 00:10:36 00:11:55
1 00:06:20 00:50:41
@@ -49,3 +50,14 @@ For part 2 I used recursion with memorization (using Python's
`functools.cache`), which is fast enough. But later Chiara pointed
out to me that actually the solution is quite trivially greedy; I
implemented the greedy version in `b-alt.py`.
+
+### Day 4: Printing Department
+
+The first "map" problem of the year! This one was easy, but I
+made a lot of mistakes in part 1. I decided to use the trick that
+[Jared](https://guissmo.com) suggested a couple of years ago: extend the
+map by 1 cell in all directions so you don't have to deal with indices
+out of bounds.
+
+For part 2 I decided to quickly code the dumb "repeat part 1 until no
+rolls are removed" strategy and it worked.
diff --git a/README.md b/README.md
@@ -14,4 +14,4 @@ See `year/README.md` for instructions on how to run my code.
|2022| 50 | Rust | Done in 2025 to learn Rust |
|2023| 50 | C | All solved by December 25, 2023 |
|2024| 50 | C++ | Each solved within 24h |
-|2025| 6 | Python | Work in progress... |
+|2025| 8 | Python | Work in progress... |