commit 5da6639f44abd65995b1ff0eb0a0be0489a6e66c
parent 20f599466cbf8ba5052d777358e854b890df5764
Author: phoebos <ben@bvnf.space>
Date: Sun, 3 Dec 2023 23:45:24 +0000
23/1-3 in Tcl
Diffstat:
A | 2023/01/a.tcl | | | 13 | +++++++++++++ |
A | 2023/02/a.tcl | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2023/03/a.tcl | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 146 insertions(+), 0 deletions(-)
diff --git a/2023/01/a.tcl b/2023/01/a.tcl
@@ -0,0 +1,13 @@
+set txt [read -nonewline stdin]
+set lines [split $txt "\n"]
+set m {one o1e two t2o three t3e four f4r five f5e six s6x seven s7n eight e8t nine n9e}
+
+set tot 0
+foreach line $lines {
+ set line [string map $m $line]
+ set line [string map $m $line]
+ regsub -all {[^0-9]} $line {} line
+ set code [string index $line 0][string index $line end]
+ incr tot $code
+}
+puts $tot
diff --git a/2023/02/a.tcl b/2023/02/a.tcl
@@ -0,0 +1,54 @@
+proc max {a b} {
+ return [expr {$a > $b ? $a : $b}]
+}
+
+set txt [read -nonewline stdin]
+set lines [split $txt "\n"]
+
+set maxes [dict create \
+ red 12 \
+ green 13 \
+ blue 14]
+
+set tot 0
+set notpossible 0
+set part2 0
+
+foreach line $lines {
+ set fields [split $line ":;"]
+ set stop 0
+
+ set m [dict create red 0 green 0 blue 0]
+ for {set i 0} {$i < [llength $fields]} {incr i} {
+
+ set words [split [lindex $fields $i] { ,}]
+
+ # first field is "Game N"
+ if {$i == 0} {
+ set game [lindex $words 1]
+ incr tot $game
+ continue
+ }
+
+ # All remaining fields are like " X color"
+ # (there is an initial space hence _)
+ foreach {_ n col} $words {
+ # Part 1
+ if {! $stop && $n > [dict get $maxes $col]} {
+ incr notpossible $game
+ set stop 1 ;# no need to continue with the rest of the line
+ }
+
+ # Part 2
+ dict set m $col [max [dict get $m $col] $n]
+ }
+ }
+ # Calculate "power"
+ set power 1
+ dict map {k v} $m {
+ set power [expr {$power * $v}]
+ }
+ incr part2 $power
+}
+puts "Part 1: [expr {$tot - $notpossible}]"
+puts "Part 2: $part2"
diff --git a/2023/03/a.tcl b/2023/03/a.tcl
@@ -0,0 +1,79 @@
+set txt [read -nonewline stdin]
+set lines [split $txt "\n"]
+
+proc look_for_num {arr y x} {
+ while {[incr x -1] >= 0 && [string is digit [lindex $arr $y $x]]} {}
+ incr x
+ # get end of string
+ set tmp [lindex $arr $y]
+ set tmp [lrange $tmp $x end]
+ set tmp [join $tmp {}]
+
+ if {[string is digit -failindex i $tmp]} { set i end } else { set i [expr {$x+$i-1}]}
+ return [list $x $i]
+}
+
+set 2d {}
+foreach line $lines {
+ lappend 2d [split $line {}]
+}
+
+set height [llength $2d]
+set width [llength [lindex $2d 0]]
+
+set found {}
+set part2 0
+
+for {set y 0} {$y < $height} {incr y} {
+ for {set x 0} {$x < $width} {incr x} {
+ switch -glob [lindex $2d $y $x] {
+ [0-9] {
+ # part of a number
+ }
+ . {
+ # nothing
+ }
+ default {
+ # treat everything else as a "symbol"
+ # look around for numbers
+ set localfound {}
+ foreach dx {-1 0 1} {
+ foreach dy {-1 0 1} {
+ set xx [expr {$x + $dx}]
+ set yy [expr {$y + $dy}]
+ if {$xx < 0 || $xx >= $width || $yy < 0 || $yy >= $height || (($dx==0)&&($dy==0))} {
+ continue
+ }
+
+ if {[string is digit [lindex $2d $yy $xx]]} {
+ set bounds [look_for_num $2d $yy $xx]
+ set num [join [lrange [lindex $2d $yy] {*}$bounds] {}]
+ # add to dictionary of found numbers:
+ # key is y,x of start of number
+ # val is numbervalue
+ dict set localfound "${yy},[lindex $bounds 0]" $num
+
+ # check if already set?
+ }
+ }
+ }
+ # Part 2
+ if {[lindex $2d $y $x] == "*"} {
+ set tmp [dict values $localfound]
+ if {[llength $tmp] == 2} {
+ incr part2 [expr [join $tmp "*"]]
+ }
+ }
+ set found [dict merge $localfound $found]
+ }
+ }
+ }
+}
+
+set tot 0
+dict map {k v} $found {
+ incr tot $v
+}
+puts $tot
+
+puts $part2