From 735b9fdf4535be1d721c59077e66def8b115d2ae Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Tue, 3 Dec 2024 00:12:57 -0500 Subject: 2024.3 --- 2024/tcl/03.tcl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 2024/tcl/03.tcl (limited to '2024/tcl/03.tcl') diff --git a/2024/tcl/03.tcl b/2024/tcl/03.tcl new file mode 100755 index 0000000..d1ff7c9 --- /dev/null +++ b/2024/tcl/03.tcl @@ -0,0 +1,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" -- cgit v1.2.3-70-g09d2