summaryrefslogtreecommitdiffstats
path: root/2024/tcl/lib.tcl
diff options
context:
space:
mode:
Diffstat (limited to '2024/tcl/lib.tcl')
-rw-r--r--2024/tcl/lib.tcl36
1 files changed, 34 insertions, 2 deletions
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 "<https://adventofcode.com/2024/day/$n>"
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
}