Pure Programmer
Blue Matrix


Cluster Map

Project: Tower of Hanoi

The [[Tower of Hanoi]] is a puzzle played with disks and pegs. The idea is to move a tower of disks from one peg to another subject to certain restrictions:

  1. Only one disk can be moved at a time
  2. Each move consists of taking the upper disk from one of the pegs and placing it on top of another stack or on an empty peg
  3. A larger disk can not be placed on a smaller disk

Write a program that uses a recursive function to solve the Tower of Hanoi puzzle with three pegs and any number of disks. Print out the configuration every 1, 5, 10 or other multiple of 5 moves depending on the number of moves it takes to solve so that about 7 moves of the solution are displayed. The number of moves (`m`) it takes to solve the puzzle depends on the number of disks (`n`) and is `m = 2^n - 1`

See [[Tower of Hanoi Algorithm]]
See [[Tower of Hanoi Animation]]

Output
$ swiftc TowerOfHanoi.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ swiftc TowerOfHanoi.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)

Solution