Version 10 of Code Golf Saving Time

Updated 2008-10-21 19:22:41 by dkf

jdc 21-oct-2008 A Tcl solution to http://codegolf.com/saving-time

set O {1 7 16 25 34 43 46 40 31 22 13 4}
set C {8 o \n 4 o {} 7 o \n 0 {} \n 1 o {} 13 o \n 0 {} \n 0 o {} 15 o \n 0 {} \n 1 o {} 13 o \n 0 {} \n 4 o {} 7 o \n 8 o \n}
foreach t $argv {
    if {[regexp {0?(\d+):0?(\d+)} $t -> h m]} {
	set h [lindex $O [expr {($h%12)}]]
	set m [lindex $O [expr {($m/5)}]]
	if {$m==$h} { 
	    set c [lreplace $C $m $m x] 
	} else {
	    set c [lreplace $C $h $h h]
	    set c [lreplace $c $m $m m]
	}
	foreach {s o b} $c { puts -nonewline [string repeat " " $s]$o$b }
    }
}

Example output:

% tclsh w.tcl 21:35
	o
    o       o

 o	     o

h	       o

 o	     o

    m       o
	o

DKF: This can be shortened to:

scan [gets stdin] %d:%d h m
set p [concat [split {LoAoBoCoDoEoFoGoHoIoJoKon
} {}] {2 {  } 5 {     }}]
if [set h [expr $h%12*2+1]]-[set m [expr $m/5*2+1]] {lset p $h h;lset p $m m} {lset p $h x}
puts [string map $p { 25Ln22K25Ann J255 BnnI555Cnn H255 Dnn22G25En 25F}]

(Note that the actual problem calls for the time to be suppled on stdin, not the command line.)