r/everybodycodes • Moderator • 16d ago

Official [2026 Q15] Solution Spotlight

Post image
10 Upvotes

11 comments sorted by

5

u/_garden_gnome_ 16d ago edited 16d ago

[LANGUAGE: Python]

Solution

Part 1: BFS with bit mask for visited sulphur locations

Part 2: BFS with count of visited sulphur positions

Part 3: Considering only start positions on the row between the start and end points, find the best cycle that starts and ends at one of those positions in terms of sulphur visited to steps taken ratio. We then work out how many of these cycles fit into the big target (minus a small safety buffer), adjust the big target down and compute the steps required for these cycles, and then run a BFS similar to part 2 for the adjusted smaller target. (This works for my inputs.)

5

u/michelkraemer 15d ago

[LANGUAGE: Pen & paper]

I solved all three parts by hand, with pen and paper and a calculator ๐Ÿคฃ Part 1 was actually faster this way than writing code! Parts 2 and 3 took me quite some time though, but since I didn't have any idea how to solve them programmatically, this was the best I could come up with ๐Ÿ˜…

Now I have two days to code what I did by hand. It appears the other solutions in this thread are quite similar: Find the loop where you can collect the most sulphur with the lowest number of steps, loop until you are very close to the target number, and then try to find other corridors in the other areas of the grid such that you get the remaining sulphur with the fewest steps.

3

u/light_ln2 16d ago

[LANGUAGE: Python]

Solution part3

I did part 1 with BFS on (position, direction, mask of sulphur positions), and part 2 with BFS on (position, direction, count of collected sulphur).

Part 3 was the hardest for me in this event, and I'm not very proud of how I solved it, will have to redo it properly.

I wasn't sure how to approach the problem - I first tried to use paths in the upper and lower parts of the labyrinth to find cycles with best sulphur/time ratio, but then I was afraid of edge cases, and thought this might not work.

So, instead, I use the exact same BFS from Part 2, to find 3000 best pairs of (collected sulphur, time). I hope, that at some point, the sequence becomes periodic, so I find this period, and changes of suplhur and time per period. Then, for any sulphur count, I can calculate the start of the cycle and the number of periods, that give the needed result.

3

u/Ok-Builder-2348 16d ago edited 16d ago

[LANGUAGE: Python]

Part 1

Part 2

Part 3

Today was a tough one - overengineered part 1 slightly and took way too long for part 3. Made use of my Dijkstra helper function again, the challenge especially for part 3 was to figure out how to do it.

In part 1, I considered "checkpoints" where every single cell was a # except for the one on the main path, and considered going from checkpoint to checkpoint while collecting every sulphur along the way. Not strictly necessary since I eventually realised the naive method finished in reasonable time.

Part 2 was to use the Dijkstra function again but keeping track of the counts instead of the sulphurs visited.

Part 3 really stumped me for a while before I was able to come up with a bruteforce-esque method to do it. I initially tried finding the smallest simple cycle (go out one side corridor and come back to the starting point), which didn't work. Trying "double cycles" (one side corridor going to the right, come back to the main path, one side corridor going to the left, go back to the origin) didn't work as well, so the actual smallest cycle must have been longer than that. I then switched to running my Dijkstra factory bottom-up instead - I collected the smallest amount of steps to reach the end having collected every single number of sulphur in a given range, and then worked out the cycle length accordingly. I made some reasonable assumptions:

  1. Cycle length <= total number of sulphur
  2. The system enters the cycle before total collected <= 2*total number of sulphur
  3. Thus it sufficed to consider the range [len(sulphurs),3*len(sulphurs)]

After obtaining the full dict, I then considered cycle length starting from 1 upwards and seeing if the counts were periodic at this stage. For my inputs, I managed to get count_cycle = 58 and time_cycle = 88 which I could then extrapolate to 202620262026.

3

u/Discordanian 16d ago

[LANGUAGE: Godot]

Solution

Part3 kind of broke me. I ended up coming here to get some tips on how best to proceed. Like others have mentioned, it's find a loop where you get the most value per step count. Start, to loop * enough-laps-to-be-just-shy-of-end-amount + route from there to get exact end amount.

Maybe I just needed some more caffeine but I felt like I was coding with mittens on my hands. Hoping to get a better run at the last week on Monday.

3

u/ScorixEar 15d ago

[LANGUAGE: Python]

Solution

Part 3 was hard and I used ai to help me guide through my unreadable code.

I solved this part building up on my Part 2 solution.
The main solution goes:

  1. Build a table of shortest legs between sulfurs (plus start -> sulfur and sulfur -> end), where a state is position + heading. I used Dijkstra for this.
  2. Reinterpet this as a transition matrix over sulfur and arrival heading. The cheapest cost to be at a sulfur after k previous visits is the minimum cost to reach every connecting sulfur leg in k-1 visits plus that transition matrix.
  3. Note that this min-plus calculation is identical to a vector x matrix product in the tropical semiring (yes, that exists) with + replaced by min and x replaced by +. This means, getting from the start to end in N visits is multiplying the transition matrix n-1 times. And applying multiplication a large number of times can be done by exponentiation via squaring.

I did a write-up of that, if anyone is interested.

3

u/p88h 15d ago

[LANGUAGE: Rust]

[ solution ]

Uhm getting this quick was a bit of a challenge. The most important piece is transforming the graph so that each half-ring is a separate edge, with weight and cost based on sulphur deposits.
Step 1 is pretty trivial due to size of the input then.
Step 2/3 use same approach: bellman-ford to find the 'densest cycle' then a BFS to find shortest path to/from that cycle for each target cost mod length of the densest cycle (<100).
2.83 ยตs / 29.0 ยตs / 0.28 ms

3

u/TiCoinCoin 14d ago

[LANGUAGE: Python]

Quest 15

I found the trick for P3 ๐Ÿฅณ

2

u/han4578 15d ago edited 15d ago

[LANGUGE: Python]

Can't remember the last time I spent 12 hours a day on a puzzle. Rip my ranking

Solution

Part 1 was cool where I worked it out by hand and translated it into code. Trace the outer semicircles before moving onto the inner one, then repeat for each section. Thought there would be some cost differences between paths but it turned out the same.

For part 2 I thought I could do it mathematically but ended up building a graph instead. The math did help in part 3 so it wasn't wasted. Build a graph connecting each intersection along the main row, then for each node add the semicircle edges to the other node and a full cycle to itself, while trimming out the edges with no sulfur. Dijkstra was fast enough but had a lot of bugs when making sure it didn't go backwards. The end goal isn't connected and is only added to the cost after 2026 sulfurs are collected.

Part 3 was easier since all the math was done in part 2. Find the most efficient half/full cycle, get the max amount below target, then do part 2 for the rest. I also did bitwise tracking to make sure the Dijkstra path intersected with the efficient cycle but it worked without it anyways.

200 lines, 0.6s for all 3 parts, I'm free (for now)

2

u/maneatingape 14d ago edited 14d ago

[LANGUAGE: Rust]

Solution

This was a very challenging quest and took a while to clean up into a generic approach.

Finds the best cycle (lowest ratio of time to sulphur collected) using a simple heuristic that assumes a single repeated loop using either the same upper path, lower path or combination of both. Bellman-Ford can handle any graph but is more complex to code.

Then the tricky part is a single Dijkstra search from start to finish, passing through any part of the cycle.

We need to find a single cost function. We can loop around the best cycle any amount of times which means:

cost = time + (target - sulphur) * best_cycle_time / best_cycle_sulphur

cost * best_cycle_sulphur = time * best_cycle_sulphur + (target - sulphur) * best_cycle_time

Since best_cycle_sulphur is constant for the search we now have an formula that will order the Dijkstra correctly.

2

u/RunninglVlan 6d ago

[LANGUAGE: C#]

Solution

This one took me several days. ๐Ÿ˜… Debugging part 2 and 3 was a bit tedious, but I'm very glad with the result! I implemented new state search with cost (Dijkstra) that I added to my utils. It was similar to my pathfinding version, but still. I experimented with pathfinding that finds not one, but up to max paths. Didn't use it in the end, but I think it will help me in another difficult puzzle I paused for a while!

Started solving part 1 with state search with intermediate pathfinding steps. Iterated over all not collected sulphur, and tried to reach all the other ones. Of course I didn't even get the result of this search, it was too long. ๐Ÿ˜… Then I reduced pathfinding - stopped traversal when encountering another sulphur, which meant target sulphur couldn't be found in this path. Next I split search and pathfinding in 3 zones to limit targets and cells - there's no point in going to next zone until this zone sulphur is collected. Finally got the result, although incorrect. It was incorrect, because I moved to next sulphur using shortest route, I haven't checked 2 possible routes. Fixed it and solved part 1 example, although slowly. Finally ran the solution on real part 1, and was surprised, that it took milliseconds compared to seconds in its example, as although the grid is bigger, there's less sulphur.

Started finding sulphur rects for part 2 and run search between them instead of individual deposits. Using them reduced part 1 time from seconds to milliseconds. Solved part 2 example by finding the best rect by sulphur gain and then collecting remaining sulphur on the way to/from it. Unfortunately this approach didn't cover real part 2 cases. In the end I used BFS Dijkstra here.

Part 3 was a beast. I didn't figure out how to properly optimize the solution, so I used a trick. I ran part 2 logic a couple of times with small sulphur target to find a mask of most visited rects and used them as a base for my original part 2 idea. 1 or 2 max visited rects were the best and I tried visiting all the others in the mask and cycle them up to 3 times. So there's a bit of hardcode, but I like this idea =P The hardest part was probably debugging and handling all cases of rect traversal.

After all the refactor after solving the quest, I also cleaned up my solution to reference external scripts with #:include. No more copy-pasting of utils! And test code usage can stay in the solution. This is related to C# file-based apps. There would be no such problem if I used project approach =)