From 645bf066b803f6a5b62aba177f792269b92e5377 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Tue, 3 Dec 2024 03:53:49 -0500 Subject: prep --- 2024/tcl/lib.tcl | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to '2024/tcl/lib.tcl') 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 } -- cgit v1.2.3-70-g09d2