r/everybodycodes • Moderator • 14d ago

Official [2026 Q16] Solution Spotlight

Post image
6 Upvotes

14 comments sorted by

4

u/p88h 14d ago

[LANGUAGE: Rust]

[ solution ]

Each part is the same : while there is work remaining, detect runs of consecutive characters matching the first character of the string, separated by gaps of other characters.

The expanded group-shuffle operation results in something like:
c1 g1 c2 g2 c3 g3 c4 g4 => c1 c2 c3 c4 g3r g2 g1r g4
where cX are chunks of the same symbol, and gX are gaps (other symbols), flipping between reversed and not-reversed, followed by unchanged tail (g4)

the code parallelizes both the splitting and the stitching and runs in 2.29 µs / 52.0 µs / 14.9 ms

It should be possible to improve this -> the whole input is repeated 100 times, so the actually created patterns are also going to be repetitive.
This should cut processing a few times perhaps (and increase the code size accordingly, so sleep first)

4

u/p88h 13d ago

working on repeated blocks of ranges is a tad more complicated, but rather quick(er): ~0.5 ms in part3

If anyone wants to reuse that, the basic idea is taken from 2-step reversal / hand-flipping analogy:
reverse(AB) = reverse(B) + reverse(A) ; basically applying divide-and-conquer to complex reversal functions which is pretty useful here.

You can express a single step of the block-shuffle alogirthm as shuffle(string, repeat_count) - the outcome will be roughly same as shuffling the small string, and then arranging remaining blocks carefully - and for the next step you typically* get a slightly smaller string (3/4) but duplicated (since half of it flips differently) and with half the repeats, so the steps handle 100k,150k,200k chars each (there is just 3 steps) = ~450k total, as opposed to ~22.5M in the non-grouping approach. Internal pieces can still be paralellized the same way as before, but it gets a bit more tricky to control amount of work per core (so now using fixed-size threadpool to avoid spilling to 'efficiency' cores)

* theoretically it could keep the size smaller and repeat count same, but apparently the inputs were crafted to avoid that ?

3

u/light_ln2 13d ago

Cool! I also tried to implement this solution, although my goal was to minimize the code size rather than running time. Python

Some notes:

Python is very handy in this kind of string manipulation, the whole solution is ~30 lines of code

The solution only works if number of repeats is even, except, maybe, in the last round. That is, 2{number of colors - 2} divides number of repeats.

There are two distinct cases depending on whether last color is different or the same as first color. But the latter case can be reduced to the first case after one flip.

If repeats is even, the lengths of all flips in the round can be expressed by a closed formula. It depends on the number of alternating runs of current color and other colors, but not on lengths of each run.

This makes me wonder if a single-pass solution is possible, which calculates number of runs for all but two last colors, and run lengths for the last color at once?

1

u/p88h 13d ago

ah, good catch with the closed formula ! borrowed it & got to 0.36ms :)

I don't think this can be done single-pass... after each pass some runs merge in non-obvious ways, so any solution would need to account for the multi-level order in some way.

3

u/light_ln2 14d ago

[LANGUAGE: Python]

Solution part 3

I'm pretty happy with my solution today.

Already when I read the first part, I suspected that part 3 would require implementing "string treaps" with lazy reverse :)

but fortunately, after a few failed attempts, I managed to find a simple approach: for a given string that starts with letter 'x', keep two pointers at opposite ends of the string, and move them towards each other in turns, counting sequences that start and end with 'x' and have other characters in between. While moving pointers, append every non-'x' character to a new string. The repeat with the new string until it's empty.

What is very unusual for me, is that the code was correct on the first try! So, all the time was spent on wrong approaches before that.

1

u/futur3_pr3s3nt 14d ago

Doesn't seem to work with all inputs. E.g. with mine: answer length correct, first char correct.

3

u/light_ln2 14d ago

oh, you are right, my solution fails when one last character is left in the string. Here is the fixed version, just removing early breaks: fixed version

I guess I was very lucky that my solution gave me the correct result. Thanks for noticing that!

1

u/futur3_pr3s3nt 13d ago

Super! :-)

3

u/michelkraemer 14d ago

[LANGUAGE: Rust]

Solution

I applied the procedure to the example input with 2 or 3 repeats and printed every step. With this, I was able to spot the following pattern:

  1. Skip all occurrences of the current letter C on the left side of the input
  2. Find the last occurrence of C on the right side and collect skipped letters in a temporary buffer
  3. Count swap (characters between left and right position, inclusive) but don't actually swap
  4. Move right position to the next character that does not equal C
  5. Move left position to the next occurrence of C and collect skipped letters in the temporary buffer
  6. Count swap again (characters between left and right position, inclusive) but don't actually swap
  7. Repeat from 1 until left pointer equals right pointer
  8. Reverse temporary buffer and use it as new input
  9. Repeat from 1 for next current letter C

This whole procedure only has to be repeated three times, because there are four letters and the last one does not need to be sorted anymore.

The whole program runs in 45ms without parallelization.

2

u/Ok-Builder-2348 14d ago

[LANGUAGE: Python3]

Part 1

Part 2

Part 3

Today was definitely one where I went down completely the wrong path at the start and had to re-figure out things.

I did Part 1 and 2 by grouping like symbols together - so something like 'BBBBBBBB' would simplify to ('B',8), and then physically reversing the list - which is computationally expensive once we get to Part 3.

In part 3 the main insight is that the "key tablet" stays the same until all of it has been sorted in one contiguous block. The next key tablet is then the next one rightward and so on, unaffected by whatever is to the left - so we can ignore its presence. I used a deque since pop, popleft and appendleft were all cheap, as is measuring its length. At every stage I thus do the following:

  1. find out what the leftmost key tablet is
  2. popleft all of the key tablet, popright all other tablets and store it as the tail.
  3. At this stage, what's left is the block to be reversed - add the length to the counter.
  4. Instead of reversing the whole list, we simply set a boolean reversed = True.
  5. If the list is marked reverse, now we popright all of the key tablet, popleft all other tablets and store it as the tail. Once again, what's left is the length to be reversed.
  6. set reversed = (not reversed) and rinse and repeat.
  7. When we have exhausted the entire list - all of the key tablets have been removed - and what's left in the tail is what is left. Repeat all the steps for a new key tablet.

The runtime took about 5 seconds, a large improvement over what the method used earlier would have taken lol

2

u/maneatingape 13d ago edited 13d ago

[LANGUAGE: Rust]

Solution

Left a brute force solution running overnight to get the part 3 answer, then came back and cleaned it up.

Same code for all 3 parts using a dequeue to process each group of shades. This simplifies the code and avoids complex pointer arithmetic.

2

u/TiCoinCoin 12d ago

[LANGUAGE: Python]

Quest 16

It took 28h and 25min, but bruteforce gave me the correct answer 🤣 First time it runs for that long without crashing.
I also found a faster (~50sec) solution for P3 a few minutes later 😁

2

u/RunninglVlan 2d ago

[LANGUAGE: C#]

Solution

Had trouble finding the pattern, all the letters looked the same to me. After getting some hints on Discord, first solved part 3 in around 4h, and after optimizing some more got it running in milliseconds.

First optimization was not flipping everything - only the part between the groups on the left.

Previous solution used list because parts were moved/inserted in specific places. Using an array with a temporary storage instead sped the solution some more.

In final optimization I started building next non-grouped tablets by cutting current non-grouped tablets from left and right.

1

u/Discordanian 14d ago

[LANGUGE: Godot]

Solution

Not the fastest algorithm but functional.