You are on page 1of 3

There Is No Spoon – Episode 2

There is no Spoon – Episode 2 is one of my favourite puzzles on codingame. It requires a slightly


different approach than standard graph games, and the algorithm for solving it uses two clever
searches. In this article I will be discussing my solution to There Is No Spoon – Episode 2.

1. Interpretation of the Grid:


The grid can be prepared in a number of ways. Here is what I did –

Make a class Node, for each node, which stores the following data:
1. Power of the node
2. Indexes of neighbour nodes in all directions
3. Number of bridges built will the neighbouring nodes

Make a class Grid, which stores the bridges built currently, and the state of all the nodes

2. Functions not related to game logic:


Before getting started with the 2 pivotal searches, I first made some fundamental methods to help
me get on with the puzzle. They are:

1. Valid – a Boolean method to check if the current grid state is valid. Checks if a node is
isolated. Here is what isolation means:
Over here, these 2 nodes cannot build bridges with other nodes. So
2=====2
they are isolated, which is against the rules
Also checks if the number of bridges a node CAN build is less than its power. Example:
. . 1 . . Over here, this node can build 1-powered bridges with all the
. . . . . surrounding nodes. However, its power is 5, so it still has to build 1
1 . 5 . 1 more bridge. But all neighbouring nodes are saturated. This means
. . . . . this grid state is faulty
. . 1 . .

2. Draw – draws of bridge of given power between 2 nodes. This will update the power of the 2
nodes, their bridges build, possible bridges, and sometimes even neighbouring nodes.
Example:
. 1 . On drawing a link . 1 . Thus left neighbour of (2,1) becomes -1, as
1 . 1 between (1,0) and 1 | 1 this link will prevent them from drawing a
. 1 . (1,2), grid becomes... . 1 . bridge across (bridges cannot intersect).
3. Clone – clones the current grid state.
4. Revert – reverts the current grid state to the cloned grid.

3. Intersection search:
Intersection search looks for moves that are 100% sure. Examples:
1 . 2 Here the node at (0,0) has only 1 neighbour, so of course it will be a node in that
. . . direction.
. . 1
3 . 1 Node at (0,0) has 2 neighbours, so it will build bridges of power 1 in both these
. . . directions. This is because maximum power of a bridge can be 2, so even after a bridge
1 . . of power 2 is built, there will be power 1 left, for the other node.
1 . . Node at (0,2) has 3 neighbours, so it will build 2 bridges with power = 2, and 1 bridge
. . . with power = 1. Thus in all directions at least 1 powered bridges will be built.
5 . 1
. . .
1 . .
From the above table, you have probably guessed when a 100% sure move occurs. It happens when
a node can build a bridge with power at least equal to 1 in all the directions where it can build.

What I just said might sound like Hebrew (considering you don’t know Hebrew). So here’s the
simplified formula which helped me determine sure moves:

𝐿𝑒𝑡 𝑆 = 𝑠𝑢𝑚 𝑜𝑓 𝑎𝑙𝑙 𝑝𝑜𝑠𝑠𝑖𝑏𝑙𝑒 𝑏𝑟𝑖𝑑𝑔𝑒𝑠 𝑎 𝑛𝑜𝑑𝑒 𝑐𝑎𝑛 𝑏𝑢𝑖𝑙𝑑, 𝑎𝑛𝑑 𝑁 = 𝑝𝑜𝑤𝑒𝑟 𝑜𝑓 𝑡ℎ𝑒 𝑛𝑜𝑑𝑒.
𝐿𝑒𝑡 𝐵𝑐𝑛𝑡 𝑏𝑒 𝑡ℎ𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑏𝑟𝑖𝑑𝑔𝑒𝑠 𝑡ℎ𝑒 𝑛𝑜𝑑𝑒 𝑐𝑎𝑛 𝑏𝑢𝑖𝑙𝑑 𝑖𝑛 𝑎 𝑝𝑎𝑟𝑡𝑖𝑐𝑢𝑙𝑎𝑟 𝑑𝑖𝑟𝑒𝑐𝑡𝑖𝑜𝑛 (= 2/1/0).
𝑇ℎ𝑒𝑛 𝑜𝑢𝑟 𝑚𝑎𝑔𝑖𝑐𝑉𝑎𝑙 = 𝑆 − 𝑁 + 𝐵𝑐𝑛𝑡 .
𝐼𝑓 𝑚𝑎𝑔𝑖𝑐𝑉𝑎𝑙 > 0, 𝑡ℎ𝑒𝑛 𝑤𝑒 𝑐𝑎𝑛 𝑏𝑢𝑖𝑙𝑑 𝑎 𝑏𝑟𝑖𝑑𝑔𝑒 𝑜𝑓 𝑝𝑜𝑤𝑒𝑟 = 𝑚𝑎𝑔𝑖𝑐𝑉𝑎𝑙 𝑖𝑛 𝑡ℎ𝑖𝑠 𝑑𝑖𝑟𝑒𝑐𝑡𝑖𝑜𝑛.

This simple formula is run for all directions for each node to determine sure moves. Of course, my
algorithm could be slightly optimized (if I check the left neighbour node[2] of a node[1], then I
needn’t check the right neighbour of the node[2]).

Intersection search runs as long as some sure move is possible. If no sure moves are left, intersection
is not run further. Now we need elimination search.

4. Elimination search
Run out of sure moves? No fear – elimination search is here! Very frequently, intersection search will
only make few moves and then stop. What to do now? The answer is simple – do what you always
do when you run of good algorithms – BRUTE FORCE! 😊

Just make an assumption, and keep running intersection. Before doing this, remember to copy the
grid, as you may make a wrong assumption and then lose it . Once you make an assumption, new
information is now available (explained in 2.2) for intersection search. This is where Boolean valid
comes in. After an assumption, at a further intersection step, the grid may become invalid . As
soon as it becomes invalid, revert the grid to its cloned state, and update the assumption as false
(make something like an “anti-draw” function).

If after an assumption, the need for another arises, you may be assured that your previous
assumption has been correct! In this case, output all the saved intersection moves and then move
onto next assumption.

Note that you cannot print the intersection moves after an assumption before the assumption has
been proven correct. So make some provision to save the moves.

5. Summing up...
That was it! There is no Spoon – Episode 2 broken up into 4 simple parts. Lots of code, but easy to
implement. I have not shared a single line of code, which may be frustrating, but hey! I just told you
all that needs to be done to solve it! The implementation is up to you... Good luck

6. Possible Bugs
1. On drawing a bridge, you do not update the values of the target node.
2. When a node with power = 2 draws a bridge, possible bridges in all other directions become
= 1.
3. When a node becomes saturated, possible bridges in all other directions becomes 0.
4. The solution is supposed to be HUGE, so there is scope for lots of errors. So I suggest that it
be taken one step at a time. First make sure intersection is 100% operational before moving
onto elimination.
5. Lastly, this puzzle took me 5 days to complete, coupled with many sleepless nights. So don’t
get stressed if you don’t get it very quickly. 

You might also like