#!/usr/bin/env tclsh9.0 namespace import ::tcl::mathop::* proc check {value list} { puts "check value=<$value> list=<$list>" if {[llength $list] == 1} { return [expr {$value == [lindex $list 0]}] } # 190: 10 19 # Last number is 19, so the first portion must be able to # evaluate to either 171 (value-19) or 10 (value/19). # Multiplication is only an option is the last number is a divisor # of the target value. # 3267: 81 40 27 # Last number is 27, so we must be able to convert {81 40} to 3240, # or to 121. set a [lindex $list end] if {[check [- $value $a] [lrange $list 0 end-1]]} {return 1} if {[% $value $a] != 0} {return 0} check [/ $value $a] [lrange $list 0 end-1] } set total 0 while {[gets stdin line] >= 0} { if {$line eq ""} continue if {[scan $line {%d: %[0-9 ]} value list] != 2} continue if {[check $value [split $list " "]]} {incr total $value} } puts $total # 19472999691 too low