#!/usr/bin/env tclsh while {[gets stdin line] >= 0} { if {$line eq ""} break if {[scan $line {%d: "%[a-z]"} n c] == 2} { set char($n) $c continue } scan $line %d%n: n length set line [string range $line $length+1 end] ;# +1 for the colon set tmp {} while {[string length $line]} { if {[scan $line %d%n i length] == 2} { lappend tmp $i set line [string range $line $length end] } else { # must be the pipe scan $line %s%n i length set line [string range $line $length end] lappend rule($n) $tmp set tmp {} } } if {[llength $tmp]} {lappend rule($n) $tmp} } proc make_regex {n} { global rule char if {[info exists char($n)]} {return $char($n)} if {$n == 8} {return "([make_regex 42])+"} if {$n == 11} { # Cheat. Cheat really fucking hard. set a [make_regex 42] set b [make_regex 31] return "($a$b|$a{2}$b{2}|$a{3}$b{3}|$a{4}$b{4}|$a{5}$b{5})" } foreach list $rule($n) { set tmp "" foreach i $list { append tmp [make_regex $i] } lappend tmplist $tmp } return "([join $tmplist |])" } set r "^[make_regex 0]$" set n 0 while {[gets stdin line] >= 0} { if {$line eq ""} continue if {[regexp $r $line]} {incr n} } puts $n