################################################################################### # File: hanoi_man_clean.tcl # Purpose: Man file example for hanoi.tcl # Author: M Shaw # Language: Ticol Tcl v1.26+ # # Versions: # 1.00 26/Jun/2018 For documentation and benchmarking ################################################################################### # Notes: # Hanoi 17 on series v1 i5 gives: # Took 3947 millisecond(s) and 131,072 move(s) # Took 4 second(s) and 131,072 move(s) ################################################################################### option expression off cls set n 0 set yloc 1 if {! [defined draw_status]} { proc draw_status {} { upvar yloc gotoxy 1 $yloc upvar a upvar b upvar c upvar n upvar moves upvar x textcolor white puts "Towers of Hanoi ($n) - Pole Status ([comma $x] moves)\n" textcolor red printf "Pole A(%2i) %-67s\r\n" [stack count a] [stack list a] textcolor yellow printf "Pole B(%2i) %-67s\r\n" [stack count b] [stack list b] textcolor green printf "Pole C(%2i) %-67s\r\n" [stack count c] [stack list c] textcolor } } textcolor white blue puts " Towers of Hanoi " textcolor newline if {[< $argc 2]} { # argv(0) is the script, argv(1) the first arg puts "How many rings (2..25)? " -nonewline # Any more than 25 won't display well gets stdin n if {|| [== $n ""] [== $n 0]} { stop } } else { set n $argv(1) } set n [min $n 25] stack create a $n # Init 3 stacks to given size stack create b $n stack create c $n for {set x 0} {[< $x $n ]} {++ x} { # Create initial stack of hoops stack push a [+ $x 1] # 0..n-1 } set x 1 set shln [<< 1 $n] # Precalc (1 << $n), << for n draw_status set start_secs [clock seconds] set start [timer] while {< $x $shln} { set xminus1 [- $x 1] stack push [chr [calc ((($x|$xminus1) + 1) % 3 )] 97] \ [stack pop [chr [calc ($x & $xminus1) % 3] 97]] is_mod $x 50000 draw_status ++ x } set end [timer] set end_secs [clock seconds] draw_status newline puts "Took [- $end $start] millisecond(s) and [comma $x] move(s)" puts "Took [- $end_secs $start_secs] second(s) and [comma $x] move(s)" newline puts "Done." stack unset a stack unset b stack unset c # EOF #