summaryrefslogtreecommitdiffstats
path: root/2024/tcl/03.tcl
blob: d1ff7c9f8e8a2d92394b9e997e3a1496a8199556 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/env tclsh

source lib.tcl
setup 3

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+)\)} [read $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
    }
  }
}

close $input

puts "Sum: $mulsum"

puts ""
puts {Part 2: ...with control flow.}

puts "Sum: $mulsum_cf"