diff options
author | Aleteoryx <alyx@aleteoryx.me> | 2024-12-07 16:05:26 -0500 |
---|---|---|
committer | Aleteoryx <alyx@aleteoryx.me> | 2024-12-07 16:05:26 -0500 |
commit | da0e33bae4d1543aa62af7c60270fa36b56050cd (patch) | |
tree | b695a29c052f860c6028eda91af9f251e1f6e037 /2024/tcl/07.tcl | |
parent | f62e9e8045844195febe990ddc0f049d1bc45902 (diff) | |
download | adventofcode-da0e33bae4d1543aa62af7c60270fa36b56050cd.tar.gz adventofcode-da0e33bae4d1543aa62af7c60270fa36b56050cd.tar.bz2 adventofcode-da0e33bae4d1543aa62af7c60270fa36b56050cd.zip |
nonfunctional 2024.7 because i like this idea and have to scrap it
Diffstat (limited to '2024/tcl/07.tcl')
-rwxr-xr-x | 2024/tcl/07.tcl | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/2024/tcl/07.tcl b/2024/tcl/07.tcl new file mode 100755 index 0000000..9c96af9 --- /dev/null +++ b/2024/tcl/07.tcl @@ -0,0 +1,54 @@ +#!/bin/env tclsh + +source lib.tcl +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]}] + } + set e2 [join $e2 " || "] + + _op_search $e1 $e2 $targ $cur $rest +} +proc _op_search {e1 e2 targ cur rest} { + set rest [lassign $rest next] + if {$rest == {}} { + expr $e1 + } else { + expr $e2 + } +} + +set test_total 0 +set cat_test_total 0 +foreach line $input { + lassign [split $line :] targ nums + set nums [lassign $nums first] + if [op_search {{ + } { * }} $targ $first $nums] { + incr test_total $targ + } + if [op_search {{ + } { * } {}} $targ $first $nums] { + incr test_total $targ + } +} + +puts "Total: $test_total" + +puts "" +puts {Part 2: Total of valid test lines with || operator} + +puts "2: $" |