Zebra puzzle: Difference between revisions
Content deleted Content added
→Tcl: Added implementation |
→{{header|Tcl}}: Significantly faster version that uses constant binding |
||
Line 657:
<lang tcl>package require struct::list
# Implements the constants by binding them directly into the named procedures.
foreach {class elems} {▼
# This is much faster than the alternatives!
Number {One Two Three Four Five}▼
proc initConstants {args} {
Color {Red Green Blue White Yellow}▼
global {}
Drink {Milk Coffee Water Beer Tea}▼
set remap {}
Smoke {PallMall Dunhill Blend BlueMaster Prince}▼
▲ foreach {class elems} {
Pet {Dog Cat Horse Bird Zebra}▼
Nation {British Swedish Danish Norwegian German}▼
} {▼
foreach e $elems {set c($e) [incr i]}▼
set ($class) $elems▼
▲ } {
set i -1
}
foreach procedure $args {
proc $procedure [info args $procedure] \
[string map $remap [info body $procedure]]
}
}
proc isPossible {number color drink smoke pet} {
if {[llength $number] && [lindex $number $Nation(Norwegian)] != $Number(One)} {
return false
Line 759 ⟶ 768:
}
}
initConstants isPossible
main</lang>
{{out}}
|