May 01, 2026

Solution for Codyssi 2025 - Siren Disruption

Link to the puzzle text

Part 1

In this puzzle, we have a list of values, list of index tuples and a single index. Together they describe an initial state and a series of swap operations. We should take the list of values as input values. For each tuple, we should swap the values at the corresponding indices. The answer is the value in the final list at the index singles out by the last part of the input.

For parsing the input, we needed a bit more complicated parser than usual by keeping track of which part we are currently reading. Since the list of values is 1-indexed (the first index has the number 1 instead of 0 as usual in programming), we simplify the index handling by starting with a None object in index 0. Finally we take each index tuple and swap the values at the indeces:

for a, b in swaps:
f[a], f[b] = f[b], f[a]

 Part 2

In part 2, we should again perform a series of swap operations. This time, we are using a 3-way swap. The 3 indices of the swap are the two indices of each tuple and the first index of the next tuple. So the input `4-8. 5-8, 10-1` performs a swap between 4-8-5 and between 5-8-10.

We first create the list of 3-way swaps by iterating over both the list of operations and a shift list of operations:

[(s1[0],s1[1], s2[0]) for (s1,s2) in zip(swaps, swaps[1:] + swaps)] 

Once we have that list, the swaps are simple:

for a, b, c in swaps:
f[a], f[b], f[c] = f[c], f[a], f[b]

Part 3

In part 3, the swaps are now even more complicated. The indices of the swap operations now indicate start of blocks. The blocks are the same size and as large as possible without overlapping or reaching the end of the list. Once we have the start and size of the blocks, the values are swapped block-wise.

Since the size of a block is limited either by the next block or the end of the list, we check the maximum size allowed by each constraint. The largest block without overlapping is the size of  `larger index - smaller index`, while the largest block still fitting the list is `list size - larger index`.

for a, b in swaps:
greater = max(a,b)
lesser = min(a,b)
block_length = min(greater - lesser, len(f) - greater)
for j in range(block_length):
f[a + j], f[b + j] = f[b + j], f[a + j]

 

Link to my solutions

No comments:

Post a Comment