Rules
The Towers of Hanoi is a puzzle, and if you are not very familiar with it, here is how it works:
The play field consists of 3 rods, and x number of disks, each next one bigger than the previous one. The disks can be put on the rod, with these RULES:
- only one disk can be moved at once, and it must be moved on the top of another rod
- the disk must be taken from the top of a rod
- a disk can be moved somewhere, ONLY if the top-most disk at the target rod is bigger than the one to be moved
And finally - the play field STARTS like this:
- a rod, with x disks, sorted so the largest is on the bottom, and the smallest on the top
- an empty rod
- an empty rod
The GOAL of the game is to move the original "stack" of disks on another rod, that is - put all of the disks on another rod, so (again) the largest is on the bottom, and the smallest on the top
Implementation
YOUR goal will be to make a program in programming language of your choice, that takes an input (described below) and outputs the steps necessary to solve the position.
As always, try to make it as short as possible.
Input
An example input:
4-3,7-6-5,2-1
Input is a string, consisting of 3 parts, separated by commas. The parts are a list of disks on each of the 3 rods. They are separated too, this time with hyphens ( - ), and each subpart is a number, the larger the number is, the larger the disk is.
So - for the above input, this would be a visual representation:
. . .
| =====|===== |
===|=== ======|====== =|=
====|==== =======|======= ==|==
ROD 1 ROD 2 ROD 3
Output
As you can see in the above representation - the the left-most part of the input is rod number one, the middle is rod number two, and the last one is rod number 3.
The output of your program should look like this:
12,23,31,12,23,13
A list of numbers, separated by commas that defines the rod that a disk should be taken of, and the rod that the disk should be put on. There are only 3 rods, so there is just 6 possible combinations (because a disk has to be moved to another rod, not the same one):
12
13
21
23
31
32
Notes
The input does not have to describe a field in "original" state - it can be mid-solved.
Your program can NOT produce null output. If the input IS in the original state, just put the disks to a DIFFERENT rod.
The input can have an empty rod(s), like these:
2-1,3,
,,1
4-3,,2-1
If the input is not in this formatted like that, your program can produce undefined behavior. So it can if the input is not valid (like bigger disk on a smaller one, missing disk, unsolvable). Input will always be valid.
Make sure the solution is as fast as possible (as little turns as possible) - that is, don't waste turns by "12,21,12"...
Testing
So, I prepared this small flash for you, with which you can test if your program produced a good solution without writing it down or anything.
Here it is: Hanoi AlgoTest (wait for it to load then refresh -- Dead link :|)
To use it, paste the input to the program to the INPUT field, and the output produced by your program to the PROCESS field. It will run a simulation, at speed which you can also change, with a visual representation, printing out any errors in the bottom part.
Hope it helps.
,,
)? I think it's allowed by the rules above... – Dilworth