day 2 tcl solution

main
magical 2023-12-06 00:29:33 -08:00
parent f1e2dc0d24
commit 0fb7b77afe
2 changed files with 39 additions and 0 deletions

23
day02/sol.tcl 100755
View File

@ -0,0 +1,23 @@
#!/usr/bin/env tclsh
source ../prelude.tcl
set input stdin
set total 0
set power 0
while {[gets $input line] >= 0} {
regexp {Game (\d+): (.*)} $line _ num takes
set min(red) 0
set min(blue) 0
set min(green) 0
foreach t [split $takes ";"] {
foreach {n color} [string map {, {}} $t] {
set min($color) [max $min($color) $n]
}
}
if {$min(red) <= 12 && $min(blue) <= 14 && $min(green) <= 13} {
incr total $num
}
incr power [expr {$min(red) * $min(blue) * $min(green)}]
#puts "${min(red)} ${min(blue)} ${min(green)}"
}
puts $total
puts $power

16
prelude.tcl 100644
View File

@ -0,0 +1,16 @@
package require Tcl 8.6
namespace import tcl::mathfunc::min
namespace import tcl::mathfunc::max
proc ladd {list} {
set t 0
foreach x $list { incr t $x }
return $t
}
proc lmul {list} {
set p 1
foreach x $list { set p [eval {$p * $x}] }
return $p
}