blob: 7f29b9ac504b62ffa08a488aa8221de2f6ede5f3 (
plain) (
tree)
|
|
#!/bin/env tclsh
source lib.tcl
setup 3 data
puts {Part 1: Sum of mul(x,y) calls...}
set mulsum 0
set mulsum_cf 0
set cf 1
foreach {inst a b} [regexp -all -inline {do\(\)|don't\(\)|mul\((\d+),(\d+)\)} $input] {
switch -- [string range $inst 0 2] {
mul {
set inc [expr {$a * $b}]
incr mulsum $inc
if $cf {
incr mulsum_cf $inc
}
}
do( {
set cf 1
}
don {
set cf 0
}
}
}
puts "Sum: $mulsum"
puts ""
puts {Part 2: ...with control flow.}
puts "Sum: $mulsum_cf"
|