I found a different answer by writing a python program to brute force find the solution using a depth first search(go as deep as possible until you can't make any more moves and then backtrack). I have't created an animation like they did to show the solution but I will show the output of the program and how to interpret it so that you can verify the solution.
Program output: (True, [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])
The list of numbers represents the index of the stack to pull off at each step. Note that it's 0 indexed, so 0 means pull off the leftmost stack. 2, for example, means pull off the third from the left stack.
Source Code
def duplicate_stacks(stacks):
return [stack[:] for stack in stacks]
def solve(stacks, last_played=None, past_moves=[]):
if all(len(stack)==0 for stack in stacks):
return True, past_moves
allowed_numbers = []
if not last_played:
allowed_numbers = [1,2,3,4,5,6,7]
else:
allowed_numbers.append(last_played % 7 + 1)
allowed_numbers.append((last_played-1) or 7)
allowed_moves = []
for i, stack in enumerate(stacks):
if len(stack) > 0 and stack[0] in allowed_numbers:
allowed_moves.append(i)
for move in allowed_moves:
new_stacks = duplicate_stacks(stacks)
last_played = new_stacks[move].pop(0)
solved, moves = solve(new_stacks, last_played, past_moves + [move])
if solved:
return solved, moves
return False, past_moves
question_stacks = [
[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]
]
print(solve(question_stacks))