From 645bf066b803f6a5b62aba177f792269b92e5377 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Tue, 3 Dec 2024 03:53:49 -0500 Subject: prep --- 2024/tcl/01.tcl | 4 +--- 2024/tcl/02.tcl | 4 +--- 2024/tcl/03.tcl | 6 ++---- 2024/tcl/lib.tcl | 36 ++++++++++++++++++++++++++++++++++-- 2024/tcl/templ.tcl | 2 +- 5 files changed, 39 insertions(+), 13 deletions(-) (limited to '2024/tcl') diff --git a/2024/tcl/01.tcl b/2024/tcl/01.tcl index 2248e11..bf7e82f 100755 --- a/2024/tcl/01.tcl +++ b/2024/tcl/01.tcl @@ -5,7 +5,7 @@ setup 1 puts {Part 1: Find the sum of distances between pairs of numbers with the same ordinal in each list.} -while {[gets $input line] != -1} { +foreach line $input { lassign $line ln rn lappend left_nums $ln @@ -14,8 +14,6 @@ while {[gets $input line] != -1} { dict incr right_num_counts $rn } -close $input - set left_nums [lsort -integer $left_nums] set right_nums [lsort -integer $right_nums] diff --git a/2024/tcl/02.tcl b/2024/tcl/02.tcl index dac290e..8cc14f3 100755 --- a/2024/tcl/02.tcl +++ b/2024/tcl/02.tcl @@ -22,7 +22,7 @@ proc row_errs {row} { return $row_errs } -while {[gets $input report] != -1} { +foreach report $input { if ![row_errs $report] { incr safe_count; continue } # naive, but handles a case like [1 4 2 3] where removing 2 makes it # safe but an iterative approach only trips on [2]. backtracking @@ -37,8 +37,6 @@ while {[gets $input report] != -1} { incr dampened_safe_count $safe_count -close $input - puts "Safe sequence count: $safe_count" puts "" diff --git a/2024/tcl/03.tcl b/2024/tcl/03.tcl index d1ff7c9..7f29b9a 100755 --- a/2024/tcl/03.tcl +++ b/2024/tcl/03.tcl @@ -1,14 +1,14 @@ #!/bin/env tclsh source lib.tcl -setup 3 +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+)\)} [read $input]] { +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}] @@ -26,8 +26,6 @@ foreach {inst a b} [regexp -all -inline {do\(\)|don't\(\)|mul\((\d+),(\d+)\)} [r } } -close $input - puts "Sum: $mulsum" puts "" diff --git a/2024/tcl/lib.tcl b/2024/tcl/lib.tcl index d816a25..698bb9a 100644 --- a/2024/tcl/lib.tcl +++ b/2024/tcl/lib.tcl @@ -1,9 +1,41 @@ -proc setup {n} { +proc setup {n {mode lines}} { puts "Advent of Code 2024 day $n Solution" puts "" puts "" set n [string range 0$n end-1 end] upvar input input - set input [open input.$n.txt] + set fd [open input.$n.txt] + + switch -- $mode { + lines { + set input [split [string trim [read $fd]] "\r\n"] + close $fd + } + data { + set input [read $fd] + close $fd + } + fd { + set input $fd + } + } +} + +proc regroup {pat {var input}} { + upvar $var list + set list2 {} + set cur {} + foreach el $list { + if {[string match $pat $el]} { + if {$cur != {}} { + lappend list2 $cur + set cur {} + } + } else { + lappend cur $el + } + } + lappend list2 $cur + set list $list2 } diff --git a/2024/tcl/templ.tcl b/2024/tcl/templ.tcl index e040b2f..51a96ff 100755 --- a/2024/tcl/templ.tcl +++ b/2024/tcl/templ.tcl @@ -5,7 +5,7 @@ setup ? puts {Part 1: } -while {[gets $input line] != -1} { +foreach line $input { } close $input -- cgit v1.2.3-70-g09d2