OneCompiler

mgw BW

104
 
def move_block(n, source, target, spare):
    if n > 0:
        move_block(n-1, source, spare, target)
        print(f"Move block {n} from {source} to {target}")
        move_block(n-1, spare, target, source)

initial_state = ['A', 'B', 'C','D']
goal_state = ['C', 'B','D', 'A']

num_blocks = len(initial_state)

for i in range(num_blocks):
    source = initial_state[i]
    target = goal_state[i]
    spare = [peg for peg in ['A', 'B', 'C'] if peg != source and peg != target][0]

    move_block(1, source, target, spare)