This time I kept track of my thoughts as the days went on. I have (now that the competition is over) posted them on github.
This time was the first time I had the 24 first clues right in the first attempt. And so on the 24th I could also order the right sleigh in 1 try! Yeah me!
#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:
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.
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!)
Example of a magic square with magic sum 2026: Example of a prime magic square with magic sum 240:
506
509
512
499
47
7
79
107
511
500
505
510
37
101
31
71
501
514
507
504
73
19
89
59
508
503
502
513
83
113
41
3
Based on the methods found on entertainmentmathematics.nl, I code my own “find a prime pan magic square, 4×4”. For a magic sum of 240, it correctly finds a solution (see example above). For 2026, it does not. There is no magic square of this kind. However, here’s a few others:
43
131
877
977
193
181
809
857
857
997
23
151
653
1013
37
337
137
37
971
883
211
163
827
839
991
863
157
17
983
683
367
7
And for extra credit:
Thoughts:
N odd won’t work, as the sum of (odd) primes has to be even.
N = 2 won’t work, as no magic square of any kind exists for N = 2.
We’ve already shown that N = 4 doesn’t work.
There are 305 primes below 2026. N = 18 and above won’t work, because 182 = 324.
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 𝜽?
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:
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.)
First a note. No, this isn’t just the “how many different kinds of coins do you need for paying all amounts of money”, as a coin can be used more than one time. No, this isn’t just the “how many weights do you need for weighing all weights”, as that is done on a scale and a weight can be used to subtract from the total.
Thankfully it’s related to these problems, so they got me thinking in the right way.
How many sauces (or chilis) needed to get all strengths 1-10? ⌈log2(10)⌉ = 4. It might be the chilis 1, 2, 4 and 8.
Recursion is a beautiful way to look at the problem. To begin with, we need chili 1, otherwise we can’t get to a (combined) strength of 1. Likewise, we need chili 2, otherwise we can’t get to a (combined) strength of 2. (1 + 1 isn’t allowed.) With chilis 1 and 2, we have access to combined strengths 1, 2 and 3 (and 0 — no chilis).
Choice A1: skip chili 3 and go straight to chili 4.
Choice B1: skip chili 4 and go straight to chili 5.
…
Choice B2: add chili 4.
Choice C1: skip chili 5 and go straight to chili 6.
…
Choice C2: add chili 5.
…
Choice A2: add chili 3.
Choice B1: skip chili 4 and go straight to chili 5.
…
Choice B2: add chili 4.
Choice C1: skip chili 5 and go straight to chili 6.
…
Choice C2: add chili 5.
…
The next step is to try adding chili 3. (A2.) With chilis 1, 2 and 3, we have access to strengths 1, 2, 3, 4, 5 and 6 (and 0). We already had access to 0-3. Now we also have access to 0+3 (actually we already had that one), 1+3, 2+3 and 3+3. The recursion bit is to then go on to try adding chili 4. (A2B2.)
The alternative to adding chili 3 is not to do it. (A1.) We then again try adding chili 4. (A1B2.) Or not. (A1B1.)
This is what I do in my program. When I have a bunch of chilis giving me all the strengths 1-10, I count it.
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:
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.)
If we had no bounds, there would be 5! = 120 ways to distribute the names.
The 1st bound is, that permutations where a person draws their own name are discarded. This also means the surviving permutations have no cycles of length 1. (Terminology and example calculations: Wikipedia.)
The 2nd bound is, that we want to compare permutations with a cycle of length 5 to those without, to calculate a probability.
How many permutations without cycles of length 1?
A permutation like that might consist of 2 cycles, 1 of length 2, 1 of length 3.
There are C(5,2) * 1! * 2! ways this can be done.
C(5,2) different ways to choose the 2 names for the 2 cycle.
1! = 1 way to permutate them, so that no person draws their own name.
The remaining names can be permutated in 2! = 2 ways.
C(5,2) * 1! * 2!
= 5*4/2*1 * 1 * 2
= 10 * 2
= 20
The bound means no cycle of length 4, because then the other cycle would have length 1.
A valid permutation might have a cycle of length 5.
There are C(5,5) * 4! ways this can be done.
C(5,5) = 1 — we have to choose all the names.
4! = 24 ways to permutate these names, without a person drawing their own name.
In all 20 + 24 = 44 ways to permutate the names, and 24 ways to achieve a 5 cycle.
24/44 = 0.545454545.
A monte carlo program roughly confirms this number.
I expand the program to look at other N’s. An analysis in Desmos suggests, that the formula should be 2.75 * N-1. If N = 5, this is 0.55. (Might 2.75 actually be e = 2.72? Hard to tell.)