#!/usr/bin/env tclsh9.0 namespace import ::tcl::mathop::* proc input {} { global keys locks space set keys [list] set locks [list] set space 0 set schem [list] while {[gets stdin line] >= 0} { if {$line eq ""} { if {$space == 0} {set space [- [llength $schem] 2]} set s1 [lindex $schem 0] if {[string range $s1 0 0] eq "."} { schem2key $schem } else { schem2lock $schem } set schem [list] } else { lappend schem $line } } # Input doesn't end with a blank line, so... if {[llength $schem]} { set s1 [lindex $schem 0] if {[string range $s1 0 0] eq "."} { schem2key $schem } else { schem2lock $schem } } } proc schem2lock {schem} { global locks set s1 [lindex $schem 0] set heights [list] for {set c 0} {$c < [string length $s1]} {incr c} { for {set r 1} {$r < [llength $schem]} {incr r} { if {[string range [lindex $schem $r] $c $c] eq "."} { lappend heights [- $r 1] break } } } lappend locks $heights } proc schem2key {schem} { global keys set s1 [lindex $schem 0] set heights [list] for {set c 0} {$c < [string length $s1]} {incr c} { for {set r [- [llength $schem] 2]} {$r >= 0} {incr r -1} { if {[string range [lindex $schem $r] $c $c] eq "."} { lappend heights [- [llength $schem] $r 2] break } } } lappend keys $heights } proc show {} { global keys locks puts "Locks:" foreach lock $locks { puts [join $lock " "] } puts "\nKeys:" foreach key $keys { puts [join $key " "] } } proc count {} { global keys locks set total 0 foreach key $keys { foreach lock $locks { if {[fits $key $lock]} {incr total} } } puts $total } proc fits {key lock} { global space for {set c 0} {$c < [llength $key]} {incr c} { if {[+ [lindex $key $c] [lindex $lock $c]] > $space} {return 0} } return 1 } input # show count