Advent Of Code

#AdventOfCode 2025 has been. This year I solved all the puzzles within 24 hours of publication (details at the end of this post). Yeah me! And I was reasonably happy with my code (PHP) . And I made animations for all 12 days!

Day 1: Spin a dial left or right, counting how many times it hits 0. Or passes 0.

It was fiddly to get the “passes 0” bit right. And how can % ever return a negative number?

Day 2: Search an interval of integers for numbers constructed by concatenating the same sequence of digits together 2 times. Or n times.

So. If we’re currently looking at the interval 95-115, 99 should be detected, because it’s “9” twice. Part 1 was very easy. In part 2 I had to give up doing part 1 and 2 simultaneously. Part of the solution was to treat numbers as strings, some of the time. PHP made that very easy.

Day 3: Given a number, find the highest 2 digit extract. Or 12 digit.

I think I wrote part 1 as brute force and then changed it for part 2. Key insight: If I’m looking for a digit, that will end up as e.g. the 7th digit of the result, counting from the back, it can’t be 1 of the last 6 digits of the number, because they may have to be the last 6 digits. Let’s say that leaves 5 digits. (I may have already used some of the preceding digits for the start of the result.) Then the optimal solution is to choose the highest digit of those 5. If that digit occurs more than once, choose the leftmost, to leave as many candidates as possible for the next digits. Also, recursion.

Day 4: On a map with “@”, given certain rules, remove as many “@”s as possible. For 1 round. Or until nothing more can be removed.

I included a cute ASCII art forklift (the @ are removed with a forklift) in the animation. 🙂

The suggested solution was to mark @ to be removed as x and then remove them. For part 2 I changed between x and y. That allowed me to mark removal with x in a round and then do the actual removal in the next round.

Day 5: Given an integer and an interval, check whether the integer fits in the interval. E.g., does 11 fit in the interval 10-14? (Yes.) In part 2, count how many integers could potentially fit the given intervals.

For part 1: Brute force. For part 2: First merge the intervals, then simply calculate their lengths and sum. That merging required brain power.

Day 6: Given some numbers and a way to manipulate them (e.g., multiplication), calculate a result. 123*45*6 = 33210. In part 2, look at the numbers vertically. 1*24*356.

Array manipulation. In part 2 I was lucky I could recycle a function, that pivots a 2d array. That made it very easy.

Day 7: A beam travels. When it meets a “^”, it splits into 2. Count how many splits occur. In part 2, count how many different paths a beam could travel.

Part 1 was easy. Scan downwards on the map, adding the beams and counting splits. In part 2 I had to keep track of the beams. If e.g. 4 beams arrived here, simply traveling down, and 3 more beams arrived after a split on my right, 7 beams are traveling through here. At the bottom I add up all the beams.

Day 8: Some points in 3d space have to be connected. Do a number of connections and then find the largest connected groups. In part 2, connect everything and note which 2 points were the last to be connected.

I guess my biggest challenge here was to keep track of “which group does this point belong to” and “which points are in this group”. I had the right idea, but I had some mishaps with using the wrong variable names.

Day 9: Given a number of points in 2d space, construct the largest rectangle possible using 2 of the points as opposite corners. Seeing the points as the corners of a polygon, check whether the rectangle fits inside the polygon.

Part 1 was pretty straightforward. I had learned some new notation: [$x1, $y1] = $data[$i]. In part 2 I could recycle some code from year 2023, day 10: Given a map of a polygon, find all points within that polygon. I also converted the points given into an actual map. I made a list of the x- and y-coordinates used by the points, plus their immediate neighbors. When traveling across my map, I needed to look at immediate neighbors, but I could skip over long distances of uninteresting coordinates. What else? Oh, given the input I could deduce quickly that some rectangles wouldn’t work. And I used memoization to ensure I only checked each point once, regarding what type the point was (corner, edge, inside polygon).

Day 10: So. There are some lights. There are also some buttons. Each button toggles one or more lights. There is a target for which lights should be on. Find the best button combination. In part 1, each button could be pressed at most once, easy. Brute force, recursion. In part 2… Even with the key insight, that we were actually looking for the best solution to a set of equalities, I was stumped. I ended up writing code to produce a Python script, because Python has a library to solve that kind of thing.

Day 11: Given a network, how many ways to travel from a to b. Or from a to b via c and d.

I really like my animation for this day.

Part 1: Brute force, recursion. Part 2: Ehm. Key insight: Figure out whether it’s possible to go from c to d or the other way around. Say it’s d to c. Then count ways to get from a to d, d to c and c to b. Then multiply. Also memoization.

Day 12: Given a rectangle and a number of heptaminos, check whether they fit. It turned out, heurestics were important this day. Each heptamino uses 7 small squares, are there that many in the rectangle? If not, no fit. Each heptamino fit inside a 3×3 square, are there that many in the rectangle? If yes, fit.

Day 1-12: I have used the forums here and there for inspiration. On 1 occasion I would say I stole part of a solution (Python day), but in a way where I understood what I was stealing.

And the times — some days I couldn’t get to the puzzle early:

Day   -Part 1-   -Part 2-
12 04:29:58 04:30:36 🙂🙂
11 03:35:59 11:59:01 🙂
10 05:06:05 11:57:11 🙂
9 01:00:09 06:16:55 🙂
8 06:01:56 06:13:01 🙂🙂
7 13:32:56 13:49:21 🙂
6 03:17:52 03:39:24 🙂🙂
5 02:53:13 03:43:01 🙂🙂
4 02:50:27 03:09:28 🙂🙂
3 01:14:49 05:25:54 🙂
2 05:57:58 06:16:42 🙂🙂
1 01:31:57 02:13:48 🙂🙂

Hamilton gyrates

Recently I rewatched #Hamilton and was fascinated by the gyrations of some of the men. I think this little dance move is clever in signaling rock-‘n’-roll, drugs and above all s*x.

A Winter’s Ball. Looking for ladies. Hamilton, Burr, Laurens.

Helpless. Hamilton has proposed to his future wife, and her father approves of the connection. And then Hamilton does this little move, before he remembers the situation.

The Story Of Tonight, Reprise. Laurens, Lafayette and Mulligan are teasing the groom.

Advent of Code, 2025, animations

This year, apart from solving the #AdventOfCode puzzles (24 stars, yeah!), I also made #animations for each day.

I’m trying to make an animation, that can work on its own. You should be able to watch it, deduce the puzzle and understand the solution.

I use example data. It varies whether I show a full or partial solution, and whether I show part 1 or 2.

I’m using ASCII art, because it’s easy for my PHP scripts to produce it. Also, there’s a certain charm to 24×80, right?

Here’s the playlist . Enjoy!

#ThisWeeksFiddler, 20251219

This week the #puzzle is: Happy (Almost) New Year from The Fiddler! #MagicSquare #primes

A magic square is a square array of distinct natural numbers, where each row, each column, and both long diagonals sum to the same “magic number.” ,,,
A prime magic square is a magic square consisting of only prime numbers. Is it possible to construct a 4-by-4 prime magic square with a magic number of 2026? If so, give an example; if not, why not?

And for extra credit:

Find all values of N for which it is possible to construct an N-by-N prime magic square with a magic number of 2026. (Remember, the numbers in a magic square must all be distinct!)

Happy (Almost) New Year from The Fiddler!

Highlight to reveal (possibly incorrect) solution:

Examples of prime magic squares OEIS sequence Magic Squares (4 x 4), Analytic Solution, Pan Magic Squares Prime Magic Squares (4 x 4), Simple Magic Squares (4 x 4) Magic Square Generator

Program

Example of a magic square with magic sum 2026:
Example of a prime magic square with magic sum 240:

50650951249947779107
511500505510371013171
50151450750473198959
50850350251383113413

And for extra credit:

#ThisWeeksFiddler, 20251212

This week the #puzzle is: Can You Topple the Tower? #geometry #trigonometry #CenterOfMass #integral

A block tower consists of a solid rectangular prism whose height is 2 and whose base is a square of side length 1. A second prism, made of the same material, and with a base that’s L by 1 and a height of 1, is attached to the top half of the first block, resulting in an overhang as shown below.
When L exceeds some value, the block tower tips over. What is this critical length L?

And for extra credit:

Instead of rectangular prisms, now suppose the tower is part of an annulus. More specifically, it’s the region between two arcs of angle 𝜽 in circles of radius 1 and 2, as shown below.
For small values of 𝜽, the tower balances on one of its flat sides. But when 𝜽 exceeds some value, the tower no longer balances on a flat side. What is this critical value of 𝜽?

Can You Topple the Tower?

Intermission

Last week I was busy. ( #AdventOfCode ) I misread the description of the puzzle, and by the time I realized my error, I didn’t have enough time left to fix my mistake. So it goes.

Highlight to reveal (possibly incorrect) solution:

Geeks for Geeks Allen Desmos

And for extra credit:

#ThisWeeksFiddler, 20251128

This week the #puzzle is: Can You Take the Heat? #combinatorics #coding #recursion

In the YouTube show, “Hot Ones,” guests answer interview questions while consuming 10 hot sauces, one at a time, ranked in increasing spiciness from 1 to 10.
You have been invited on as a guest and want to prepare for the show. However, you don’t feel like purchasing all 10 sauces in advance. Your plan is to purchase fewer sauces, and then to combine sauces together for any you are missing. For example, if you are missing sauce #7, then you can instead simultaneously consume sauces #3 and #4, since 3 + 4 = 7. (I know the spiciness of the sauces isn’t linear, but for the purposes of this puzzle, let’s assume it is.)
After some pencil-and-paper scratch work, you realize you only need four spices.
… for how many sets of four spice numbers is it possible to generate all the numbers from 1 to 10 using each spice at most once?

And for extra credit:

You’re prepping for a new show, “Hotter Ones,” which has spices ranked from 1 to 100. Let N be the minimum number of spices needed to generate all the numbers from 1 to 100.
For how many sets of N spice numbers is it possible to generate all the numbers from 1 to 100 using each spice at most once? (Note that I am not asking for the value of N; that’s just something you’ll need to figure out en route to your answer.)

Can You Take the Heat?

Highlight to reveal (possibly incorrect) solution:

Program

And for extra credit:

Everybody codes, 2025

November is over, and I’d like to show you, how much of #EverybodyCodes I was able to finish and how fast. #coding #puzzles

Everybody Codes.

My code: Github.

My points:

QuestPartGlobal RankGlobal ScoreGlobal Time

1909d 19h 10m 52s 000ms
3III1361557m 14s 203ms
4III1163546m 38s 809ms
8II95631m 48s 095ms
III104471h 04m 58s 186ms
13III1183356m 03s 484ms
14III109422h 04m 39s 331ms
18III142910h 09m 06s 517ms
19III148310h 27m 18s 880ms

A lot of the puzzles I could solve within an hour. There are on the other hand 4 part 3s I spent a lot of time on or haven’t solved yet. My personal times:

QuestPartLocal Time

6d 02h 13m 34s 269ms
1I✔️ 10m 44s 561ms
II✔️ 41m 42s 557ms
III✔️ 57m 47s 008ms
2I✔️ 39m 00s 446ms
II1h 08m 07s 492ms
III1h 13m 24s 707ms
3I✔️ 5m 44s 474ms
II✔️ 9m 44s 694ms
III✔️ 12m 52s 639ms
4I✔️ 8m 47s 061ms
II✔️ 16m 12s 579ms
III✔️ 26m 06s 430ms
5I✔️ 25m 20s 628ms
II✔️ 36m 38s 965ms
III3h 57m 23s 504ms
6I✔️ 9m 56s 776ms
II✔️ 14m 50s 792ms
III6h 40m 26s 861ms
7I✔️ 30m 12s 217ms
II✔️ 34m 55s 935ms
III4h 50m 16s 212ms
8I✔️ 8m 45s 964ms
II✔️ 30m 22s 913ms
III1h 03m 33s 004ms
9I✔️ 17m 03s 727ms
II1h 04m 46s 554ms
III1h 43m 48s 759ms
10I✔️ 25m 41s 852ms
II8h 50m 38s 955ms
III❎ 3d 05h 01m 07s 738ms
11I✔️ 27m 27s 719ms
II✔️ 38m 23s 615ms
III11h 12m 05s 955ms
12I✔️ 20m 51s 077ms
II✔️ 30m 47s 006ms
III9h 47m 24s 856ms
13I✔️ 19m 19s 207ms
II✔️ 34m 59s 623ms
III✔️ 55m 49s 017ms
14I✔️ 17m 12s 045ms
II✔️ 19m 23s 352ms
III1h 41m 08s 303ms
15I✔️ 39m 54s 585ms
II3h 34m 49s 830ms
III❎ –
16I✔️ 6m 59s 911ms
II✔️ 55m 52s 154ms
III2h 15m 29s 888ms
17I✔️ 16m 28s 064ms
II✔️ 32m 15s 080ms
III❎-
18I✔️ 35m 45s 878ms
II✔️ 58m 08s 269ms
III8h 26m 20s 369ms
19I✔️ 51m 40s 365ms
II1h 01m 05s 460ms
III8h 45m 22s 518ms
20I✔️ 11m 33s 080ms
II✔️ 56m 01s 591ms
III❎ –

#ThisWeeksFiddler, 20251121

This week the #puzzle is: A Loopy Holiday Gift Exchange #probabilities #coding #montecarlo #combinatorics

You are participating in a holiday gift exchange with your classmates. You each write down your own name on a slip of paper and fold it up. Then, all the students place their names into a single hat. Next, students pull a random name from the hat, one at a time. If at any point someone pulls their own name from the hat, the whole class starts over, with everyone returning the names to the hat.
Once the whole process is complete, each student purchases a gift for the classmate whose name they pulled. Gifts are handed out at a big holiday party at the end of the year.
At this party, you observe that there are “loops” of gift-giving within the class. For example, student A might have gotten a gift for B, who got a gift for C, who got a gift for D, who got a gift for A. In this case, A, B, C and D would form a loop of length four. Another way to have a loop of length four is if student A got a gift for C, who got a gift for B, who got a gift for D, who got a gift for A. And of course, there are other ways.
If there are a total of five students in the class, how likely is it that they form a single loop that includes the entire class?

And for extra credit:

If there are N students in the class, where N is some large number, how likely is it that they form a single loop that includes the entire class, in terms of N? (For full credit, your answer should be proportional to N raised to some negative power.)

A Loopy Holiday Gift Exchange

Intermission

Oh, look at that, we have a repeat.

Highlight to reveal (possibly incorrect) solution:

Program

A monte carlo program roughly confirms this number.

And for extra credit:

Desmos