blob: af6bd6578b0007d1da5c0e773a5ee28ee77ee645 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
set fp [open "input"]
set equations {}
while {[gets $fp line] >= 0} {
set s1 [split $line ":"]
lappend equations $s1
}
proc can_match_impl {target numbers current} {
if {0 == [llength $numbers]} {
return [expr {$target == $current}]
}
set rest [lrange $numbers 1 end]
set first [lindex $numbers 0]
return [expr {[can_match_impl $target $rest [expr {$current * $first}]]
|| [can_match_impl $target $rest [expr {$current + $first}]]
|| [can_match_impl $target $rest "$current$first"]}]
}
proc can_match {target numbers} {
return [can_match_impl $target [lrange $numbers 1 end] [lindex $numbers 0]]
}
set sum 0
foreach elem $equations {
set test_value [lindex $elem 0]
set numbers [lindex $elem 1]
if {[can_match $test_value $numbers]} {
set sum [expr {$sum + $test_value}]
}
}
puts $sum
|