File: hanoi.el.html
Solves the Towers of Hanoi puzzle while-U-wait.
The puzzle: Start with N rings, decreasing in sizes from bottom to top, stacked around a post. There are two other posts. Your mission, should you choose to accept it, is to shift the pile, stacked in its original order, to another post.
The challenge is to do it in the fewest possible moves. Each move shifts one ring to a different post. But there's a rule; you can only stack a ring on top of a larger one.
The simplest nontrivial version of this puzzle is N = 3. Solution time rises as 2**N, and programs to solve it have long been considered classic introductory exercises in the use of recursion.
The puzzle is called Towers of Hanoi because an early popular
presentation wove a fanciful legend around it. According to this
myth (uttered long before the Vietnam War), there is a Buddhist
monastery at Hanoi which contains a large room with three time-worn
posts in it surrounded by 21 golden discs. Monks, acting out the
command of an ancient prophecy, have been moving these disks, in
accordance with the rules of the puzzle, once every day since the
monastery was founded over a thousand years ago. They are said to
believe that when the last move of the puzzle is completed, the
world will end in a clap of thunder. Fortunately, they are nowhere
even close to being done...
1999 addition: The Towers of Unix command (hanoi-unix) stems from
the never-disproven legend of a Eunuch monastery at Princeton that
contains a large air-conditioned room with three time-worn posts in
it surrounded by 32 silicon discs. Nimble monks, acting out the
command of an ancient prophecy, have been moving these disks, in
accordance with the rules of the puzzle, once every second since
the monastery was founded almost a billion seconds ago. They are
said to believe that when the last move of the puzzle is completed,
the world will reboot in a clap of thunder. Actually, because the
bottom disc is blocked by the "Do not feed the monks" sign, it is
believed the End will come at the time that disc is to be moved...
Defined variables (7)
hanoi-base-face | Face for base. Ignored if hanoi-use-faces is nil. |
hanoi-even-ring-face | Face for even-numbered rings. Ignored if hanoi-use-faces is nil. |
hanoi-horizontal-flag | Non-nil means that hanoi poles are oriented horizontally. |
hanoi-move-period | Time, in seconds, for each pole-to-pole move of a ring. |
hanoi-odd-ring-face | Face for odd-numbered rings. Ignored if hanoi-use-faces is nil. |
hanoi-pole-face | Face for poles. Ignored if hanoi-use-faces is nil. |
hanoi-use-faces | If nil, all hanoi-*-face variables are ignored. |
Defined functions (13)
hanoi | (NRINGS) |
hanoi-0 | (RINGS FROM TO WORK START-TIME) |
hanoi-goto-char | (POS) |
hanoi-insert-ring | (RING POLE) |
hanoi-internal | (NRINGS BITS START-TIME) |
hanoi-move-ring | (RING FROM TO START-TIME) |
hanoi-n | (BITS RINGS FROM TO WORK START-TIME) |
hanoi-pos-on-tower-p | (POS) |
hanoi-put-face | (START END VALUE &optional OBJECT) |
hanoi-ring-to-pos | (RING POS) |
hanoi-sit-for | (SECONDS) |
hanoi-unix | () |
hanoi-unix-64 | () |