Inspired by this question on the puzzling stack exchange.
Rules
Given 5 stacks of 5 cards with numbers from 1 to 7, output the moves needed to empty all stacks. A move is defined as the index of a column to pull the card off of, and a move is only valid if:
- The stack is non-empty
- The value of the card at the top of the stack is exactly 1 lower or higher than the value of the last card removed from any of the stacks. Note 1 and 7 wrap.
Input
This challenge has loose input rules. You can use any format as input for the 25 numbers so long as the input is unambiguous among all possible inputs. The same token must be used for every instance of a card's representation. That is, if you use 2
to represent a 2
, you must use that everywhere in your input. But you may use 2
to represent 2
and 532jv4q0fvq!@$$@VQea
to represent 3
if this is somehow beneficial.
Output
The list of moves needed to empty all stacks without getting stuck. This format is also loose, but it needs to represent a list of indices, where 0
is the leftmost stack.
Your program may assume that the input provided is solvable. That is, you may have undefined behaviour in the case of unsolvable input.
Note: If the input has multiple possible solutions, outputting at least 1 valid solution is acceptable
Example/Test Case explained
Input: Representing the 5 stacks, where stack one is 5,5,3,2,6
with 5
being the top of the stack. Likewise for the other 4 stacks. Again, as stated in the question, this input format is loose and can be changed to facilitate the answer
5 5 3 2 6
2 4 1 7 7
4 2 7 1 3
2 3 3 1 4
4 6 5 5 1
Output: List of moves representing the order in which the cards are pulled off the stacks. I'll explain the result of the first 3 moves. The first 3 pops 2
off of the 4th stack (2 3 3 1 4
stack). The second 3 pops 3
off of the same stack. And then the 1 pops the 2
off of the 2nd stack. If you follow these moves on the input you will end up with empty stacks
[3, 3, 1, 3, 1, 0, 4, 0, 4, 4, 2, 0, 2, 1, 1, 3, 2, 2, 0, 2, 3, 4, 0, 1, 4]
3313104044202113220234014
? \$\endgroup\$