summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleteoryx <alyx@aleteoryx.me>2024-12-03 03:53:49 -0500
committerAleteoryx <alyx@aleteoryx.me>2024-12-03 03:56:22 -0500
commit645bf066b803f6a5b62aba177f792269b92e5377 (patch)
tree65c344f818db7f3aab7b765b401c3f0dbf466fdc
parent735b9fdf4535be1d721c59077e66def8b115d2ae (diff)
downloadadventofcode-645bf066b803f6a5b62aba177f792269b92e5377.tar.gz
adventofcode-645bf066b803f6a5b62aba177f792269b92e5377.tar.bz2
adventofcode-645bf066b803f6a5b62aba177f792269b92e5377.zip
prep
-rwxr-xr-x2024/tcl/01.tcl4
-rwxr-xr-x2024/tcl/02.tcl4
-rwxr-xr-x2024/tcl/03.tcl6
-rw-r--r--2024/tcl/lib.tcl36
-rwxr-xr-x2024/tcl/templ.tcl2
5 files changed, 39 insertions, 13 deletions
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 "<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
}
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