summaryrefslogtreecommitdiffstats
path: root/2024
diff options
context:
space:
mode:
authorAleteoryx <alyx@aleteoryx.me>2024-12-07 16:15:48 -0500
committerAleteoryx <alyx@aleteoryx.me>2024-12-07 16:15:48 -0500
commitb16625cf6797da68f9c3c82a48509c7697d01040 (patch)
tree624820079f3850b263ecb6031da2af841d9b41d8 /2024
parentda0e33bae4d1543aa62af7c60270fa36b56050cd (diff)
downloadadventofcode-b16625cf6797da68f9c3c82a48509c7697d01040.tar.gz
adventofcode-b16625cf6797da68f9c3c82a48509c7697d01040.tar.bz2
adventofcode-b16625cf6797da68f9c3c82a48509c7697d01040.zip
2024.7; could be more elegant, but it works
Diffstat (limited to '2024')
-rwxr-xr-x2024/tcl/07.tcl42
1 files changed, 18 insertions, 24 deletions
diff --git a/2024/tcl/07.tcl b/2024/tcl/07.tcl
index 9c96af9..9cc9ddd 100755
--- a/2024/tcl/07.tcl
+++ b/2024/tcl/07.tcl
@@ -5,31 +5,25 @@ setup 7
puts {Part 1: Total of valid test lines}
-proc op_search {ops targ cur rest} {
- # cursed, produces exprs of the form "$cur<op>$next == $targ || ..."
- set e1 {}
- foreach op $ops {
- lappend e1 [string cat {$cur} $op {$next == $targ}]
- }
- set e1 [join $e1 " || "]
- # cursed, produces exprs of the form "[_op_search $e1 $e2 $targ [expr {$cur<op>$next}] $rest] || ..."
- set e2 {}
- foreach op $ops {
- lappend e2 [string cat \
- {[_op_search $e1 $e2 $targ [expr } \
- [list [string cat {$cur} $op {$next}]] \
- {] $rest]}]
+proc op_search {targ cur rest} {
+ set rest [lassign $rest next]
+ if {$rest == {}} {
+ expr {$cur + $next == $targ || $cur * $next == $targ}
+ } else {
+ expr {[op_search $targ [expr {$cur + $next}] $rest] ||
+ [op_search $targ [expr {$cur * $next}] $rest]}
}
- set e2 [join $e2 " || "]
-
- _op_search $e1 $e2 $targ $cur $rest
}
-proc _op_search {e1 e2 targ cur rest} {
+proc op_search_cat {targ cur rest} {
set rest [lassign $rest next]
if {$rest == {}} {
- expr $e1
+# puts "leaf: $cur,$next // $targ"
+ expr {$cur + $next == $targ || $cur * $next == $targ || [string cat $cur $next] == $targ}
} else {
- expr $e2
+# puts "branch: $cur,$next .. $rest // $targ"
+ expr {[op_search_cat $targ [expr {$cur + $next}] $rest] ||
+ [op_search_cat $targ [expr {$cur * $next}] $rest] ||
+ [op_search_cat $targ [string cat $cur $next] $rest]}
}
}
@@ -38,11 +32,11 @@ set cat_test_total 0
foreach line $input {
lassign [split $line :] targ nums
set nums [lassign $nums first]
- if [op_search {{ + } { * }} $targ $first $nums] {
+ if [op_search $targ $first $nums] {
incr test_total $targ
}
- if [op_search {{ + } { * } {}} $targ $first $nums] {
- incr test_total $targ
+ if [op_search_cat $targ $first $nums] {
+ incr cat_test_total $targ
}
}
@@ -51,4 +45,4 @@ puts "Total: $test_total"
puts ""
puts {Part 2: Total of valid test lines with || operator}
-puts "2: $"
+puts "Total: $cat_test_total"