adventofcode2023/prelude.tcl

35 lines
783 B
Tcl
Raw Normal View History

2023-12-06 08:29:33 +00:00
package require Tcl 8.6
namespace import tcl::mathfunc::min
namespace import tcl::mathfunc::max
2023-12-07 21:59:35 +00:00
proc {#} args {}
proc llen {lst} { return [llength $lst] }
proc slen {str} { return [string length $str] }
proc trim args { return [uplevel [concat string trim $args]] }
proc replace {str a b} { return [string map [list $a $b] $str] }
2023-12-06 08:29:33 +00:00
proc ladd {list} {
set t 0
foreach x $list { incr t $x }
return $t
}
proc lmul {list} {
set p 1
2023-12-09 07:16:10 +00:00
foreach x $list { set p [expr {$p * $x}] }
2023-12-06 08:29:33 +00:00
return $p
}
# split s on a substring
proc splitstr {s sep} {
# replace $sep with ascii char 30, aka "record separator"
return [split [replace $s $sep "\x1E"] "\x1E"]
}
2023-12-08 02:35:27 +00:00
proc must_regexp args {
if {! [uplevel [concat regexp $args]]} {
error "regexp failed"
}
}