The “Tower of Hanoi” is a well known puzzle. There are 3 pegs with numbers 1, 2 and 3. N disks of different diameters are set on the first peg in the following order:
The lower disk is set, the larger diameter it has. Your aim is to move all disks onto the second peg using the third peg as an auxiliary one. Following the rules within a move it’s allowed to replace only one uppermost disk. Besides, it’s forbidden to put the disk of the bigger diameter onto the disk of the smaller one.
I have recursively solved the Tower of Hanoi problem in the Swift programming language that has O(2 power n) time complexity with an O(n) space complexity.
Tower of Hanoi solution in Swift (version 5.9) is written below:
import Foundation
class Hanoi {
func towerOfHanoi(_ number: Int, _ from: String, _ to: String, _ reserve: String){
if (number == 0){
return
}
towerOfHanoi(number - 1, from, reserve, to)
print("Moved \(number) from \(from) to \(to)")
towerOfHanoi(number - 1, reserve, to, from)
}
}
// driver code to test the above-written solution
var tower = Hanoi()
tower.towerOfHanoi(2, "sourceRod", "destinationROD", "temporaryROD")
