diff options
| author | Mistivia <i@mistivia.com> | 2024-12-21 17:40:21 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-12-21 17:40:21 +0800 |
| commit | 211677cbbfd0e0d4015bea07d84cd37834c5a5d1 (patch) | |
| tree | c7cb30fbd35a3def7c3b3dca07151295912def2a /07/1.tcl | |
| parent | f56a745129023bb6373b3b440dfba3054b083758 (diff) | |
day 07 part 1
Diffstat (limited to '07/1.tcl')
| -rw-r--r-- | 07/1.tcl | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/07/1.tcl b/07/1.tcl new file mode 100644 index 0000000..788712b --- /dev/null +++ b/07/1.tcl @@ -0,0 +1,30 @@ +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}]]}] +} + +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 |
