How to resolve the algorithm Towers of Hanoi step by step in the Ruby programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Towers of Hanoi step by step in the Ruby programming language
Table of Contents
Problem Statement
Solve the Towers of Hanoi problem with recursion.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Towers of Hanoi step by step in the Ruby programming language
Implementation of the Towers of Hanoi in Ruby
The code implements the Towers of Hanoi algorithm, a mathematical puzzle where the goal is to move a stack of disks from one pole to another, following certain rules. It includes two functions: move
and solve
.
move
Function:
- This function recursively moves a specified number of disks (
num_disks
) from a starting pole (start
) to a target pole (target
), using an auxiliary pole (using
). - If there's only one disk, it simply moves it from
start
totarget
. - For multiple disks, it:
- Recursively moves
num_disks-1
disks fromstart
tousing
. - Moves one disk from
start
totarget
. - Recursively moves
num_disks-1
disks fromusing
totarget
.
- Recursively moves
solve
Function:
- This function solves the Towers of Hanoi puzzle for a given set of input towers.
- It calculates the total number of disks and initializes a sequence number (
x
) to 0. - It iteratively tries legal moves:
- Calculates the source (from which to move) and target (to which to move) poles based on
x
. - Checks if there's a disk on the source pole.
- Checks if the move is legal (the target pole is empty or the disk being moved is smaller than the one on the target).
- If the move is legal, it pops the disk from the source pole and pushes it onto the target pole.
- Calculates the source (from which to move) and target (to which to move) poles based on
Source code in the ruby programming language
def move(num_disks, start=0, target=1, using=2)
if num_disks == 1
@towers[target] << @towers[start].pop
puts "Move disk from #{start} to #{target} : #{@towers}"
else
move(num_disks-1, start, using, target)
move(1, start, target, using)
move(num_disks-1, using, target, start)
end
end
n = 5
@towers = [[*1..n].reverse, [], []]
move(n)
# solve(source, via, target)
# Example:
# solve([5, 4, 3, 2, 1], [], [])
# Note this will also solve randomly placed disks,
# "place all disk in target with legal moves only".
def solve(*towers)
# total number of disks
disks = towers.inject(0){|sum, tower| sum+tower.length}
x=0 # sequence number
p towers # initial trace
# have we solved the puzzle yet?
while towers.last.length < disks do
x+=1 # assume the next step
from = (x&x-1)%3
to = ((x|(x-1))+1)%3
# can we actually take from tower?
if top = towers[from].last
bottom = towers[to].last
# is the move legal?
if !bottom || bottom > top
# ok, do it!
towers[to].push(towers[from].pop)
p towers # trace
end
end
end
end
solve([5, 4, 3, 2, 1], [], [])
You may also check:How to resolve the algorithm Poker hand analyser step by step in the 11l programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Last letter-first letter step by step in the 11l programming language
You may also check:How to resolve the algorithm Sockets step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Median filter step by step in the Go programming language