Everybody Codes, the 2025 event, quest 16-20

I’ve done Everybody Codes, back in November.

The Song of Ducks and Dragons [ 2025 ]

All my code is available . And so are my times. And I really like, that we operate with local times here.

.........................................................................................#
.................#...........#.....#........#........#.....#...........#.................#
.....#..##.#..#..#.#...#..#..#.....#...#.#..#..#.#...#.....#..#..#...#.#..#..#.##..#.....#
.#####.###.#.###.#.###.#####.#.#####.###.#.###.#.###.#####.#.#####.###.#.###.#.###.#####.#
##########################################################################################

Quest 16. There’s a certain kind of wall, with a connection between a list of numbers, a given length and the resulting wall built by a known number of blocks. For the list of numbers, 1 means, place a block in every column. 9 means, place a block every 9 columns. Given a list of numbers and a length, count all the blocks (part 1). Given a known wall, find the list (part 2). Given a known wall segment and a number of blocks, find the length (part 3).

Oh, the circularity! Part 1 uses floor(). For part 2, I can reverse engineer the wall. Is there a block in column 1? Add 1 to the list of numbers and mark all the corresponding blocks. Is there an unmarked block in column 2? Add 2 to the list of numbers and mark blocks. Is there an unmarked block in column 3? Add 3 to the list… For part 3, bisection is good.

..........4..........
......473483436......
....4671465878781....
...219317375373724...
..81639828352872922..
..73694243721961934..

Quest 17. Given a map with numbers and @ marking a volcano, and a rule for how the volcano “grows”, add up all numbers within radius 10 of the volcano (part 1). The volcano grows in steps, find the step with the highest sum of numbers (part 3). Plan a path from S around the volcano and back to S, given the volcano has grown n steps, each step of the path adds the number and the sum mest be less than (n + 1) * 30. Find n.

In part 1, go through the map and remove all the numbers outside the radius, then add up the remaining numbers. In part 2, visit all numbers. Calculate the step, where this number would be affected, and add the number to a corresponding sum. Find the highest sum. In part 3, Dijkstra! This one I had to think about. In part because the path might visit a number twice. But, there’s a way to do it.

First assume the volcano has grown to radius n. (Beginning with 0.) Then use Dijkstra to find 2 paths, one going clockwise (*) from the red S to a point in the green bar, one counterclockwise. Examine for all points in the green bar. Find the best sum of 2 paths. If it’s too high, calculate the n where this path would have worked, set n to this value and try again. Whew! And I also had to remove my big data structures, when my calculations has finished, or the program would crash.

(*) Force clockwise by removing the lower blue bar.

Plant 1 with thickness 1:
- free branch with thickness 1

Plant 2 with thickness 1:
- free branch with thickness 1

Plant 4 with thickness 17:
- branch to Plant 1 with thickness 15
- branch to Plant 2 with thickness 3

Quest 18. There are plants with branches. Plants like no. 1 can simply be activated. Plants like no. 4 depends on its input from other plants. Given a rule for how plants affect other plants, activate all the simple plants and see how it affects the last plant (part 1). Activate the simple plants using a pattern (part 2). Find the best pattern (part 3).

I had a function to calculate the so called energy for each plant, because I used that code a lot. In part 1 call that function once. In part 2 call it for each pattern. Part 3… For the example data, I could simply try all patterns. But for the notes, a little analysis was required. It turned out, that some simple plants, if activated, would always drag the quality of the pattern down. So, turn on the rest, and boom! Best pattern.

. .......#....#..#........#...#.↑.↑.↑.↑.↑.#.......
9 .......#....#..#...↑....#....↑.↓.↓.↓.↓.↓........
8 ............#..#..↑.↓...#...↑...........↓.......
7 .......↑....#....↑...↓..#..↑............#.......
6 ......↑#↓...#...↑.....↓...↑.............#.......
5 .....↑.#.↓..#..↑.......↓.↑..............#.......
4 ....↑..#..↓.#.↑#........↓...#...........#.......
3 ...↑...#...↓.↑.#............#...........#.......
2 ..↑....#....↓..#............#...........#.......
1 .↑.....#.......#............#...........#.......
↑ S......#.......#........#...#...........#.......

Quest 19. Numbers describe the walls (or rather, the holes in the walls) in a chamber. A rule describes how a duck might fly through the chamber, using the holes. Some of the flight will be flaps upward. Count the flaps (part 1). A wall might have more than 1 hole, find the flight with the lowest number of flaps (part 2 and 3).

There’s recursion involved, because there’s each choice for flap or not flap + the rest of the flight. For small data, trying all choices works fine. For large data… I have a list of the holes. I can calculate the best way to get from a hole to the next. It was fiddly to get this one right. But it’s important to learn. Using sparse data instead of the complete map is a good technique.

T#TTT###T##
.##TT#TT##.
..T###T#T..
...##TT#...
....T##....
.....#.....

Quest 20. A map shows an area with some trampolines. Count pairs of trampolines (part 1). Given 2 points, find the shortest path, jumping between trampolines (part 2). Do it again, but the area is rotating (part 3).

Oh yes. The rotating triangle. But first. Part 1 is very much about interpreting the map correctly. In the top left corner, “T#T” shows a “T#” pair and a “#T” pair. That “#” is also in a pair with a “#” below it, but the “T” on the right is not in a pair with anything below it. Part 2: Convert a Dijkstra to take these pairs into account. Part 3: Implement the rotation. I chose to rotate the jumper. Jump up here, come down there. (Took forever to get right! Better model next time!) And if I come down there, which pairs are available? Wrinkle: I might in essence jump up and down in place. Also, for part 3 I had a more standard graph for Dijkstra. The rotation? Just look at it.

$mn1 = floor($x / 2);
$mn2 = $x % 2;
$m = $mn1 + $mn2;
$n = $mn1;

$op1 = floor($y / 2);
$op2 = $y % 2;
if($mn2 == 0) {
$o = $op1 + $op2;
$p = $op1;
} else {
$p = $op1 + $op2;
$o = $op1;
}

$new_x = $M - 1 - $n + 2 * $o + $p;
$new_y = $M - 1 - $m - $o;
return [$new_x, $new_y];

Everybody Codes, the 2025 event, quest 11-15

I’ve done Everybody Codes, back in November.

The Song of Ducks and Dragons [ 2025 ]

All my code is available .

 Step 1 [1,2]    Step 2 [2,3]    Step 3 [3,4]    Step 4 [4,5]    Step 5 [5,6]
  1 2 3 4 5 6     1 2 3 4 5 6     1 2 3 4 5 6     1 2 3 4 5 6     1 2 3 4 5 6
  * * * * * *     * * * * * *     * * * * * *     * * * * * *     * * * * * *
  *(*)  * * *     *  (*)* * *     *   * * * *     *   * * * *     *   * * * *
  *     * * *     *     * * *     *     * * *     *     * * *     *     * * *
  *     * * *     *     * * *     *     * * *     *     * * *     *     * * *
  *       * *     *       * *     *       * *     *       * *     *       * *
  *       * *     *       * *     *       * *     *       * *     *       * *
  *       *       *       *       *       *       *       *       *       *(*)
  *       *       *       *       *       *       *       *       *       *
          *               *               *               *

Quest 11. Given a list of numbers, interpret them as columns of ducks. A column will check whether the next columns has fewer birds, and if so, move a duck. This happens to all columns. And until no more moves are possible. Then the check goes the other way. The result is a balanced set of columns. Do this for 10 rounds (part 1). Or until balanced (part 2 and 3).

For part 1 and 2, the hardest part was probably to understand what was going on. For part 3… There is a shortcut. I’ve written about why it works. I made a video! Anyway, once the shortcut was in place, it was, again, easy.

989601     989601     989601     989601     989601     989601     989601
857782 857782 857782 857782 857782 857782 857782
746543 746543 746543 746543 746543 746543 746543
766789 766789 766789 766789 766789 766789 766789

Quest 12. Given a map of numbers and a starting point, add numbers to the group of points by adding numbers that are less than or equal to an already added numbers. Do this until no more can be added (part 1). Again with 2 starting points (part 2). Again, with 3 starting points chosen for maximum effect (part 3).

A bit fiddly, but straightforward. In part 3 it turned out to be important, that a good starting point was also a local maximum. If a < b, a can’t set b on fire.

72
58
47
61
67

Quest 13. Given a list of numbers, construct a dial and turn it 2025 times (part 1). Given a list of intervals, construct a dial and turn it 20252025 times (part 2). Again, but 202520252025 times.

In part 3 the numbers got too big. It was important not to construct the whole dial as an array. So, only note beginnings and ends of intervals.

 Round 1     Round 2     Round 3     Round 4     Round 5
.#.#.. .###.. #.#.#. #####. ..###.
##.##. #.#### ###.## ...##. ##.###
#.#... #..### #.#.## .#..#. .#.#.#
....## ##...# .#..#. ##.##. .#.###
#.#### #.#.#. #..#.# .####. #...#.
##..#. ...#.. ###.#. .###.# .#...#

Quest 14. A map of tiles. In a game of life way, determine which tiles will be active in the next round. Run some rounds (part 1 and 2). Run a lot of rounds, noting when a certain pattern matches (part 3).

The interesting bit is part 3. Run a few rounds, and keep track of repetitions. Then calculate the rest of the rounds instead of actually going through them. A few interesting questions like how to represent the tiles in a manageable way when recording “I have seen this before”, and then the actual math at the end, trying very hard not to get any off by 1 errors.

 start             R3              R4              L3
S S### S### S###
# #
# #
# #
# ####

Quest 15. Given a list of instructions about how to walk (turn right and walk 3, turn right again and walk 4…), interpret the walked segments as walls and find the shortest path from start to end (part 1-3).

Dijkstra! And then in part 3, it was necessary to not just build the whole map. Similar to day 13, only build the interesting bits. This was fiddly! A lot of small steps, a lot of testing and correcting errors.

Everybody Codes, the 2025 event, quest 6-10

I’ve done Everybody Codes, back in November.

The Song of Ducks and Dragons [ 2025 ]

All my code is available .

AaAaa 
AaAaa
AaAaa
AaAaa
AaAaa

Quest 6. Given a list of letters, find all pairs of e.g. “A*a” (part 1 and 2). Repeating the list 1000 times, do it again (part 3).

Part 1 was nice actually. Going through the list, there’s a counter for how many “A”s I’ve seen so far. And when I encounter an “a”, I add that counter value to the running sum. Part 2 was a little more sophisticated, where it had to work without me knowing what letter I was actually looking at. And using ctype_upper() and strtoupper(). Oh, part 3. Here I could use the knowledge, that repeating the list also meant repeating the numbers. With 1000 repetitions, 998 of them are “in the middle”. Count 1 of these and multiply by 998. Then count the 1st and the last. It was a little fiddly. For some reason I didn’t just create my own example data and work on that.

Oronris,Urakris,Oroneth,Uraketh

r > a,i,o
i > p,w
n > e,r
o > n,m
k > f,r
...

Quest 7. Given a list of names and a list of rules, which names follow all the rules (part 1 and 2)? Like, Oronris is out, because s can’t follow i. Then construct all names possible with the rules (part 3).

For part 1 and 2, look at each pair of letters and check whether that exists as a rule. For part 3, oh, recursion. Given part of a name, add all the possible next letters. And check uniqueness, which I didn’t at first!

1,5,2,6,8,4,1,7,3

Quest 8. Given a list of numbers, interpret these as the numbers of nails. So in this list, there’s a string between 1 and 5, between 5 and 2 etc. How many times does a string pass through the center of the circle of nails (part 1)? How many times does a string cross another string (part 2)? Which extra string would cross the most existing strings (part 3)?

The 1st one is easy. Calculate the distance between the 2 nails. If the distance is the same as halfway around the circle, the string will go through the center. In part 2 I use this knowledge: If 2 strings, 1 -> 5 and 7 -> 3, cross each other, it’s because 1 < 3 < 5, but not 1 < 7 < 3. I also have to take care of the edge case, where “both strings use the same nail in 1 end” isn’t counted as crossing. In part 3 I use some of my existing program, but also create all the possible strings. The one I’m looking for might not already exist.

1:CAAGCGCTAAGTTCGCTGGATGTGTGCCCGCG
2:CTTGAATTGGGCCGTTTACCTGGTTTAACCAT
3:CTAGCGCTGAGCTGGCTGCCTGGTTGACCGCG

Quest 9. So called DNA sequences. We have 3 of these, 2 parents and 1 child. For each letter in the sequence, the child should have the same letter as 1 of the parents. Which 1 is the child (part 1 and 2)? Construct a family tree for a lot of people, and measure the largest family (part 3).

Pretty straight forward coding. Can this person be the child of these 2 persons, yes or no? With more than 3 persons, have a 2d array, $children[$parent1][$parent2] = $child. In part 3 I begin by saying all triples are small families. Then I combine these into larger families, until they can’t grow anymore. I got to use “break 3”.

.......       ...X...       ......D       .......       .......
..X.X.. .D..... ....X.. ....... ....X.X
.X...X. ...X... .....X. ....... ...X...
...D... X.X.... ....... ....... .....D.
.X...X. ....... ....... ..X.X.. ...X...
..X.X.. ....... ....... .X...X. ....X.X
....... ....... ....... ...D... .......

Quest 10. Given a map, that’s actually sort of a chessboard, with D marking a dragon moving like a knight, and S marking sheep (no sheep shown above), how many sheep can the dragon reach and eat (part 1)? Add that the sheep can also move and can hide (part 2). Find all games where the dragon eats all the sheep (part 3).

I can see in part 3 I added memoization. And I borrowed code heavily from a colleague. It was hard to keep it straight in my head, where did the dragon go, where did the sheep go, when did a game end with all sheep eaten. On, there’s also recursion.

Everybody Codes, the 2025 event, quest 1-5

I’ve done Everybody Codes, back in November. And I got all 60 stars, yeah me! 3 of the stars I only got recently, as among other things Advent of Code and mscroggs arrived in December. Nevertheless!

The Song of Ducks and Dragons [ 2025 ]

All my code is available . I aim for low complexity, easily readable code. Including comments. And it’s in PHP.

       Vyrdax
/ \
/ \
Elarzris Drakzyph
\ /
\ /
Fyrryn

Quest 1. Given a list of names, move left and right through the list (part 1). With wrapping (part 2). Or instead of moving around, swap names (part 3).

Create a system to keep track of positions. Use %. Use arrays.

A = [25,9]
R = [ 0,0]

Cycle I
R = R * R = [0,0]
R = R / [10,10] = [0,0]
R = R + A = [25,9]

Quest 2. Given a number, perform a well defined set of math instructions on that number (part 1). Given a map of points, all with coordinates, perform those math instructions of each point (part 2). (Makes a nice picture.) Increase size of map (part 3).

A lot of math. A 2d array.

10,5,1,10,3,8,5,2,2

10 > 8 > 5 > 3 > 2 > 1 the sum of the sizes: 10 + 8 + 5 + 3 + 2 + 1 = 29
10 > 8 > 5 > 2 > 1 the sum of the sizes: 10 + 8 + 5 + 2 + 1 = 26
10 > 5 > 3 > 2 the sum of the sizes: 10 + 5 + 3 + 2 = 20
10 > 5 > 2 the sum of the sizes: 10 + 5 + 2 = 17

Quest 3. Given a list of numbers, create a new list of distinct numbers with the highest sum possible (part 1). Or a list with 20 distinct numbers and the smallest sum possible (part 2). Or the lowest number of lists, covering everything from the original list.

In part 1, simply throw away the duplicates. array_unique(). In part 2, sort the unique numbers and keep the 20 smallest. In part 3, count occurrences of each number and register the largest. array_count_values(). max().

128
64
32
16
8

Quest 4. Interpret a list of numbers as teeth on neighboring gears. If the 1st gear turns 2025 times, how many times does the last gear turn (part 1)? If the last gear turns 10000000000000 times, how many times does the 1st gear turn, rounded up (part 2)? Or do the part 1 calculation again, but with more complicated gears with 2 sets of teeth (part 3).

In part 1 I calculate how many teeth were involved for the 1st gear, and then calculate how many turns that is for the last gear. Part 2 is the same in reverse. In part 3 I did the full calculation, involving all the gears.

 3-5-7
|
1-8-10
|
5-9
|
7-8

Quest 5. The fishbone! Given a list of numbers, construct the valid fishbone. Then make other choices based on the result.

As I was basically doing the same thing a lot, I was quick to put stuff into functions. That actually became a guiding principle. If part 2 was “do the same as part 1, but a lot of times”, create a function. A fun function to use was usort(), a custom sorting function. Not very complicated code, just fiddly.

I am also finding a rhythm. I usually build 1 large program, because later parts don’t require a complete rewrite of earlier work. And I get used to the input for the 3 parts being different.

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: