December 2024 I participated in Advent of Code
.

It annoyed me, that I had code for 2023 and 2025 in my repository
, but not 2024. So I’ve added it. Enjoy.
(I already wrote about all of this code, beginning from day 1.)
December 2024 I participated in Advent of Code
.

It annoyed me, that I had code for 2023 and 2025 in my repository
, but not 2024. So I’ve added it. Enjoy.
(I already wrote about all of this code, beginning from day 1.)
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!
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! ![]()
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 ![]()
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 |
Thoughts:
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.
Thoughts:
Thoughts, continued in Desmos.
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).
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.
Result: 8.
Update program to use 100 chilis instead.
Result: 1014.
November is over, and I’d like to show you, how much of #EverybodyCodes I was able to finish and how fast. #coding #puzzles

My code: Github. ![]()
My points:
| Quest | Part | Global Rank | Global Score | Global Time |
|---|---|---|---|---|
| 190 | 9d 19h 10m 52s 000ms | |||
| 3 | III | 136 | 15 | 57m 14s 203ms |
| 4 | III | 116 | 35 | 46m 38s 809ms |
| 8 | II | 95 | 6 | 31m 48s 095ms |
| III | 104 | 47 | 1h 04m 58s 186ms | |
| 13 | III | 118 | 33 | 56m 03s 484ms |
| 14 | III | 109 | 42 | 2h 04m 39s 331ms |
| 18 | III | 142 | 9 | 10h 09m 06s 517ms |
| 19 | III | 148 | 3 | 10h 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:
| Quest | Part | Local Time |
|---|---|---|
| 6d 02h 13m 34s 269ms | ||
| 1 | I | ✔️ 10m 44s 561ms |
| II | ✔️ 41m 42s 557ms | |
| III | ✔️ 57m 47s 008ms | |
| 2 | I | ✔️ 39m 00s 446ms |
| II | 1h 08m 07s 492ms | |
| III | 1h 13m 24s 707ms | |
| 3 | I | ✔️ 5m 44s 474ms |
| II | ✔️ 9m 44s 694ms | |
| III | ✔️ 12m 52s 639ms | |
| 4 | I | ✔️ 8m 47s 061ms |
| II | ✔️ 16m 12s 579ms | |
| III | ✔️ 26m 06s 430ms | |
| 5 | I | ✔️ 25m 20s 628ms |
| II | ✔️ 36m 38s 965ms | |
| III | 3h 57m 23s 504ms | |
| 6 | I | ✔️ 9m 56s 776ms |
| II | ✔️ 14m 50s 792ms | |
| III | 6h 40m 26s 861ms | |
| 7 | I | ✔️ 30m 12s 217ms |
| II | ✔️ 34m 55s 935ms | |
| III | 4h 50m 16s 212ms | |
| 8 | I | ✔️ 8m 45s 964ms |
| II | ✔️ 30m 22s 913ms | |
| III | 1h 03m 33s 004ms | |
| 9 | I | ✔️ 17m 03s 727ms |
| II | 1h 04m 46s 554ms | |
| III | 1h 43m 48s 759ms | |
| 10 | I | ✔️ 25m 41s 852ms |
| II | 8h 50m 38s 955ms | |
| III | ❎ 3d 05h 01m 07s 738ms | |
| 11 | I | ✔️ 27m 27s 719ms |
| II | ✔️ 38m 23s 615ms | |
| III | 11h 12m 05s 955ms | |
| 12 | I | ✔️ 20m 51s 077ms |
| II | ✔️ 30m 47s 006ms | |
| III | 9h 47m 24s 856ms | |
| 13 | I | ✔️ 19m 19s 207ms |
| II | ✔️ 34m 59s 623ms | |
| III | ✔️ 55m 49s 017ms | |
| 14 | I | ✔️ 17m 12s 045ms |
| II | ✔️ 19m 23s 352ms | |
| III | 1h 41m 08s 303ms | |
| 15 | I | ✔️ 39m 54s 585ms |
| II | 3h 34m 49s 830ms | |
| III | ❎ – | |
| 16 | I | ✔️ 6m 59s 911ms |
| II | ✔️ 55m 52s 154ms | |
| III | 2h 15m 29s 888ms | |
| 17 | I | ✔️ 16m 28s 064ms |
| II | ✔️ 32m 15s 080ms | |
| III | ❎- | |
| 18 | I | ✔️ 35m 45s 878ms |
| II | ✔️ 58m 08s 269ms | |
| III | 8h 26m 20s 369ms | |
| 19 | I | ✔️ 51m 40s 365ms |
| II | 1h 01m 05s 460ms | |
| III | 8h 45m 22s 518ms | |
| 20 | I | ✔️ 11m 33s 080ms |
| II | ✔️ 56m 01s 591ms | |
| III | ❎ – | |
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.) |

Oh, look at that, we have a repeat.
My thoughts:
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.)
I am doing the #EverybodyCodes quests this November. ( #coding #puzzle ) Today I’d like to talk about quest 11, part 3. Spoilers.

This next bit will make much more sense if you are actually familiar with part 3. If you haven’t solved it yourself, you might want to look at this description
.
Quest 11, part 3 has been described this way:
Maybe I am dumb and just don’t see, that b actually follows from a.
Maybe I needed more than intuition and “when I used this method, I got the correct result”.
Anyway. I have tried writing a proof.
We have some columns of ducks.
a b c d e f g
a <= b <= c etc.
The average of a-g is x
We can insert |, so that e.g. c <= x < d
a b c | d e f g
WHAT HAPPENS BEFORE |?
Choose the first among a-c (they are left of |), so that the chosen one is strictly smaller than the one to its right.
Call this candidate m.
So we might have
a b c | d … (a < b)
b b c | d … (a = b < c)
c c c | d … (a = b = c < d)
Notice that it is always possible to choose m.
We might have
m b c | d …
In that case the next step is
m+1 b-1 c | d …
As b <= c, it follows that b-1 < c, and the next step is
m+1 b c-1 | d …
This goes on until we reach c and d.
m+1 b c | d-1 …
IMPORTANT: m grows 1, d-g shrinks 1.
IMPORTANT: the list is still sorted. See below what happens when c = d.
We might also have
m m c | d …
In the first round we get 1 or more steps bringing us to this
m m+1 c | d-1 …
In the next round this becomes
m+1 m+1 c | …
IMPORTANT: each m grows 1, d-g shrinks accordingly.
IMPORTANT: given z m’s, each m grows 1, d-g shrinks z.
IMPORTANT: the list is still sorted after each round.
WHAT HAPPENS AFTER |?
Choose the last among d-g, so that the chosen one is strictly larger than the one to its left.
Call this candidate n.
So we might have
… c | d e f g (f < g)
… c | d e f f (e < f = g)
… c | d e e e (d < e = f = g)
… c | d d d d (c < d = e = f = g)
Notice that it is always possible to choose n.
We might have a step, right after d shrinking.
… | d-1 e f n
In that case we go through these steps.
… | d e-1 f n
… | d e f-1 n
… | d e f n-1
IMPORTANT: d goes back to d, n shrinks 1
We might have
… | d-1 e n n
In that case we go through these rounds
… | d e n n-1
… | d e n-1 n-1
IMPORTANT: d goes back to d, each n shrinks 1.
IMPORTANT: given y n’s, each n shrinks 1, a-c grows y.
IMPORTANT: the list is still sorted after y rounds.
We might have
… | n-1 n n n
In that case we end up here after a round
… | n-1 n-1 n-1 n-1
IMPORTANT: d goes to d-1 = n-1, each n shrinks 1. See below what happens when c = d. Otherwise we keep the property that c < d.
IMPORTANT: given y n’s, each n shrinks 1, a-c grows y.
IMPORTANT: the list is still sorted after y rounds.
To sum up:
We begin with a sorted list.
A duck crosses the | in each round.
The list before | will stay sorted.
The list after | will stay sorted or go back to being sorted after a finite number of steps.
We are headed to a situation looking like
m m m | n n n n
or something similar.
This process ends when m = n = x.
1 round before the end we had something like
m-1 m m | n+1 n n n
All the way through the process, c < d. (Ending with m < n+1.)
Maybe all of this is easier to understand watching an example.
This week the #puzzle is: Can You Irrigate the Garden? #probabilities #geometry #trigonometry #average #integration
| You and your assistant are planning to irrigate a vast circular garden, which has a radius of 1 furlong. However, your assistant is somewhat lackadaisical when it comes to gardening. Their plan is to pick two random points on the circumference of the garden and run a hose straight between them. |
| You’re concerned that different parts of your garden—especially your prized peach tree at the very center—will be too far from the hose to be properly irrigated. |
| On average, how far can you expect the center of the garden to be from the nearest part of the hose? |
And for extra credit:
| As before, your assistant intends to pick two random points along the circumference of the garden and run a hose straight between them. |
| This time, you’ve decided to contribute to the madness yourself by picking a random point inside the garden to plant a second peach tree. On average, how far can you expect this point to be from the nearest part of the hose? |

My first attempt was a monte carlo program.
My second was to use integration. I convert from “2 random points” to “1 random point and 1 fixed in a nice way” to “2 random points but with similar angles”. That way the integral is nicer.
Result: 2/π ≈ 0.636619772368.
My first (and only) attempt was the program.
Note that the line going through the 2 hose points might be closer to the random point than the hose itself.
Result: 0.74178.
This week the #puzzle is: The Randy Hall Problem #probabilities #MontyHall
| You are a producer on a game show hosted by Randy “Random” Hall (no relation to Monty Hall). The show has three doors labeled 1 through 3 from left to right, and behind them are various prizes. |
| Contestants pick one of the three doors at which to start, and then they press an electronic button many, many times in rapid succession. Each time they press the button, they either stay at their current door or move to an adjacent door. If they’re at door 2 and move to an adjacent door, that new door will be 1 or 3 with equal probability. |
| Randy has decided that when a contestant presses the button while at door 2, there should be a 20 percent chance they remain at door 2. |
| As the producer, you want the chances of a contestant ultimately winding up at each of the three doors to be nearly equal after many button presses. Otherwise, mathematicians will no doubt write you nasty letters complaining about how your show is rigged. |
| If a contestant presses the button while at door 1 (or door 3), what should the probability be that they remain at that door? |
And for extra credit:
| Randy has an updated suggestion for how the button should behave at door 2. What hasn’t changed is that if a contestant at door 2 and moves to an adjacent door, that new door will be 1 or 3 with equal probability. |
| But this time, on the first, third, fifth, and other odd button presses that happen to be at door 2, there’s a 20 percent the contestant remains at door 2. On the second, fourth, sixth, and other even button presses that happen to be at door 2, there’s a 50 percent chance the contest remains at door 2. |
| Meanwhile, the button’s behavior at doors 1 and 3 should in no way depend on the number of times the button has been pressed. |
| As the producer, you want the chances of winding up at each of the three doors—after a large even number of button presses— to be nearly equal. |
| If a contestant presses the button while at door 1 (or door 3), what should the probability be that they remain at that door? |

Thoughts:
Thoughts:
For illustration of all the probabilities:
| pk(1) | pk(2) | pk(3) |
| 0.333333 | 0.333333 | 0.333333 |
| pk+1(1) | pk+1(2) | pk+1(3) |
| 0.374227 | 0.251545 | 0.374227 |
| pk+2(1) | pk+2(2) | pk+2(3) |
| 0.333333 | 0.333333 | 0.333333 |
| odd | direction | ||
| door | left | stay | right |
| 1 | 0.722681 | 0.277319 | |
| 2 | 0.4 | 0.2 | 0.4 |
| 3 | 0.277319 | 0.722681 |
| even | direction | ||
| door | left | stay | right |
| 1 | 0.722681 | 0.277319 | |
| 2 | 0.25 | 0.5 | 0.25 |
| 3 | 0.277319 | 0.722681 |