Compare commits

...

4 Commits

Author SHA1 Message Date
magical 539cf753d0 day 16 cleanup
switch to globals (passing array names around is ugly).
(maybe look into dicts sometime?)

also, properly detect width/height from the input
instead of hardcoding it
2023-12-17 06:24:26 +00:00
magical c592761d00 day 15 use dicts
tcl's dict type is exactly what we need for this problem:
an order-preserving de-duplicated list of key-value pairs.
2023-12-16 06:57:12 +00:00
magical 94bf752568 day 16 part 2 2023-12-16 06:02:16 +00:00
magical b14209ff39 day 16 part 1 2023-12-16 05:43:57 +00:00
4 changed files with 264 additions and 24 deletions

View File

@ -44,33 +44,12 @@ proc place {boxp lens} {
set h [hash $label]
set i 0
set found 0
if {![info exists box($h)]} { set box($h) {} }
foreach {l n} $box($h) {
if {$l eq $label} {
lset box($h) $i+1 $num
set found 1
break
}
incr i 2
}
if {!$found} {
lappend box($h) $label $num
}
dict set box($h) $label $num
} else {
set label [trim $lens "-"]
set h [hash $label]
#puts "$lens $label $h"
if {[info exists box($h)]} {
set i 0
foreach {l n} $box($h) {
if {$l eq $label} {
#puts "removing $i from $h"
set box($h) [lreplace $box($h) $i $i+1]
break
}
incr i 2
}
}
dict unset box($h) $label
}
}
@ -83,7 +62,7 @@ proc solve input {
set t 0
foreach {h elems} [array get box] {
set slot 0
foreach {l n} $elems {
dict for {l n} $elems {
incr slot
incr t [expr {($h+1) * $slot * $n}]
}

110
day16/input 100644
View File

@ -0,0 +1,110 @@
\.\............/........|............/.-....................|............|.......-..............//....../../..
......\.-/..|...............-...-\......../.............-.../.............../........-................../../..
.........\..\......................................................../\.......\.............................-.
....|.........-..................|..../....-......................................|.../...............|.../...
.....|...|..-....|................/............./......./............../|.......|.....|.................../...
.........-............../.|.-...\..........................|..................//............-\......|.........
.........|...................||.........-.|/.......---...-...........\........\.-...\..../......|.............
.......|-................................................................./........../..../...................
......-......|......././...............................\.....\.........|...........|..\-............|\........
|...\.............................-...\\-.................|................................-.........|........
...\....|.|........................-.........................\..|............................-....|...........
\.........|..|./....................../..........-......|...-....................\....-../...\.-.....|/.......
......\./................|...................-....../..................................|.......-....-.........
......-.........................................|....\..................../-./-.......|........./.............
................|...|...\....\....-....//........./...../.....-..............|\.|..\.........\......./\....\.\
.\...|.....|......./....-...........|.............-..\...-..-....................|.../.....-...........--.....
......................................-................../..............\.\\........../.../................\..
.-..........|..|......................|....|.-|.....................-..../............|......./..../..\/.-.\..
..\..\.............../-......-................-......................................\....../......|..........
.........\.....\.......................................-../........./-....-.................\....../..../.....
......-..\..-.............|...................|....../.......\../........../.....-............................
.............|../.\...\....|...............|.........../..|-....../........../..../|.|.\........|........\..|.
..\............|...../...................\......................./...-.......-..........................-.....
....|......................|......../.|.\.../......|...|..-.....................|................../..\.......
.....................................................-................/\..-....|..........................-.\.
................../...\..........\...........\............................................./.../.........|.|..
.....-......................................./....-........|.-........................|.........\.....\.......
...............\/\..........|.|................................|.\......\......\............|...-.............
..................................\.......-.........|-........../...........\\..\.|../......|........\..|.....
..............-...........\.....\\./......................./...-...........-..........................././....
...-..............-..........................\..........|...........-/........../.../...|.....................
...\..............-......./-...........|............./.......-./........-..-....|......./\.-.......\./........
.........|......././..\.-......./............-....-..................|....-......../........-.........\.......
....|.........................../....................-..................-...\|............................|...
.-........-...........|..............................|.........................................\......-......-
............./......../..-....\...............................././.......|................................|...
.|\../......-./........./...............-./..............\../..............|....|........./...................
..................................................|............................-..............................
.............-...................\.......|........|.............../......-....|......../.../.-...\|...........
.................../..............-|........................|.............\..\.........................././...
...../.........../..-.......|........../.......|........................./......|......./..........|..........
../...-........../...........................-.|..\../..................\.....-...........|...................
.........\.......-.......-.\.|......|.......||......|.\....-...-.................-..........-..\..............
................................\./.......\/............-................./.....\......../....................
/...........-......|..|....../.....\.........|.....................................\...................|....-.
...........\.............../.......\......................................................................\..-
....../...................-..................|-\......................-..................\....................
/............./...........|.....|............/.................................-....................-......\|.
...............\..|..............|............\../.../..........................\\................|......./...
....--/............|......|......-........-...../........./.........../-..\..|........-.......................
|.-....-.........................|../................\................../......\..../.\..|...../.../|.........
....................................../.........\........-....................../.-..-.\.....././.../.....\..\
......\...../............./................-...........\.................../.....-../......-..................
....../............../...........|......|..................................|...................|..............
......-../.../.|.........././..........\.......\........./......|..............\.................-.........../
...../...|........//...../.................\................../.\.............-............/.........../......
..............-.....|................\................/.........|.....\.......\....................\.....\....
..........|..........-.../..................\.-.......................................\..-.........|..........
....|..................-.....|..................|...........-......./.....-......../...|........\....\-.|.....
.......................-......./......................./.-.-.........|.....|.................|................
...|................/......-.....|....-..-.........-|....|..................../.../....-......\.......-......\
............-.............................-...................../..............|.|.......-....................
-....-..............|................./......../.\............./../............../..-...-...........|.........
.|............|..........-................./................|.....................|./....\......./.....\......
..........|\.|...................-.............\......|..-..\.......\..........\........|......-.../........\.
\.......\....................\..............|................................|..............-.../.............
......../-....................|...............-........................\................\..............-......
...|..-.............//.-.............../........-........../..|.............\|../....................-........
..../.\.-.-|.....\.-|..../.....\..|.........../........................-.....................-..........|.....
...../-..........\.........-....................\.............................\............|-..\..........|...
...-...../....................../-......\...|.....-........................../.|.........|.......\..\.....\../
...................\...../......................./...-.....\.-.........-...../.|...................|......./..
../.....|..|..\.........|.../.................../..-............\../........\.....\.....................\.....
.........................../...\.|........\...||..........\......\.-....|.........\-..\.......................
../.........|............................/../-||..............|-........\............\....\.\................|
../....|....................|.....|-.......|.................../...\.-.\.............-.................-......
....|..............................................................-|.........................................
..................\-.\/.........|../|....../|..\...-...\.........|/............./...\...\.....................
.\.........-../....|-......................./................/\..\........................................./..
./......................-...............|...............|................-.......................\............
................\..........././............................-....\/..........-.../../....|.........-....|...../
.-..-..|\..\/........\./....|.......................\.........../.....|......./-.-..../..../..................
\.....|........................../.|...../.-...............|.../\...|.........|.\|....../.-......|............
.........-|............./..../.\.|//.......-....-...........\.........-..-.........\....\..........|..........
....................|.......-../.....\-..........\................-..................\.-........|.............
.....|.\-........................./........................................../.......-.\....-......../........
...................|..............\..|/................................|/..../........../..|.................|
.......\...|./......../..........-............-....|...........-/.\...-......./....\......./........\....../..
...\............\..........\../................./....-......|..........................................\......
............|......|......../.............\...|.....................\.\\......................................
-.-.-......\.......|..........-.|.....|\....\/..............-/..........|............|......./.........-......
......................../...|................./......\............../..............-/.......\.....\\..........
...|.|.-..-./..................../................................-....\|........./...-..|........\......-....
....\....-../.........|....|........-./..\../...|...../-.|.|....\................................./...|......-
..............\../..............|.|................\......./.............../.\......|.....|..\...../....../...
............|....\......|........|.......\.................................................../.-..............
.|............|...\....................../............................................\.......|.-.-...........
.....-.\|......\.............|............|....-.........\....|.............................../....\....-.|...
............|./.........................\...|..............................\...|..|..............|...-........
.-.....|..................|....-.............-..\..-...............-....-..................-..................
..\......-.-./..................................-............\........./........-.-.............-............-
..../.\--...............//..................................||................\.................|......|......
..|./...\......................|.........../........\.....-....-.................................../-....\....
.........-........\..............................|.......|......../...........|....../......|..\.|..../.......
....../....-....|\............|.....|..\../||....................................\.....................-......
.....\.....--...././......................-........\\............-...............\....-....|................./
........./........../...-................/...........-............................................-........./.
.........-..-...|/..\.........................\............\..-........-.........-...............\......|./.-.
|......................-.....-..-.........../-..................|.....|.........\./........./...-/............
..............-...........|-....-.../.............-........./...../.................../.......--.|....-..../..

10
day16/sample1.in 100644
View File

@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

141
day16/sol.tcl 100755
View File

@ -0,0 +1,141 @@
#!/usr/bin/env tclsh
source ../prelude.tcl
proc read-input {input} {
global grid
global width
global height
set i 0
while {[gets $input line] >= 0} {
set j 0
foreach c [split $line ""] {
set grid($i,$j) $c
incr j
}
incr i
}
set height $i
set width $j
}
proc reset-visited {} {
global visit
array unset visit
}
proc mark-visited {x y d} {
global visit
set v [switch -exact -- $d n {expr 1} s {expr 2} w {expr 4} e {expr 8} default {error "invalid dir '$d'"}]
incr visit($y,$x) 0
set result [expr {($visit($y,$x) & $v) != 0}]
set visit($y,$x) [expr {$visit($y,$x) | $v}]
return $result
}
proc print-visited {} {
global visit
global width
global height
for {set i 0} {$i < $height} {incr i} {
set row {}
for {set j 0} {$j < $width} {incr j} {
if {[info exist visit($i,$j)] && $visit($i,$j)} {
lappend row "#"
} else {
lappend row "."
}
}
puts [join $row ""]
}
}
proc follow {beams {first 0}} {
global grid
set newbeams {}
foreach {x y d} $beams {
if {!$first} {
if { [mark-visited $x $y $d] } continue
switch $d {
n { incr y -1 }
s { incr y +1 }
w { incr x -1 }
e { incr x +1 }
}
}
if {[info exists grid($y,$x)]} {
switch -exact "$grid($y,$x) $d" {
{\ n} { set d w }
{\ s} { set d e }
{\ e} { set d s }
{\ w} { set d n }
{/ n} { set d e }
{/ s} { set d w }
{/ e} { set d n }
{/ w} { set d s }
{| w} { set d {n s} }
{| e} { set d {n s} }
{- n} { set d {w e} }
{- s} { set d {w e} }
}
foreach d' [split $d] {
lappend newbeams $x $y ${d'}
}
}
}
return $newbeams
}
proc solve {} {
global visit
reset-visited
set beams [follow {0 0 e} 1]
while {[llen $beams] > 0} {
set beams [follow $beams]
}
print-visited
puts [array size visit]
}
proc lmax {list} {
set t 0
foreach x $list { if {$t < $x} {set t $x} }
return $t
}
proc solve2 {} {
global grid
global width
global height
global visit
puts "width=$width height=$height"
set edges {}
foreach k [array names grid] {
lassign [split $k ","] y x
if {$x == 0} { lappend edges [list $x $y e] }
if {$y == 0} { lappend edges [list $x $y s] }
if {$x == $width - 1} { lappend edges [list $x $y n] }
if {$y == $height - 1} { lappend edges [list $x $y w] }
}
set l {}
foreach beams $edges {
reset-visited
set beams [follow $beams 1]
while {[llen $beams] > 0} {
set beams [follow $beams]
}
puts [array size visit]
lappend l [array size visit]
}
puts [lmax $l]
}
read-input stdin
puts [array get grid]
puts [follow {0 0 e}]
puts [array get visit]
solve
solve2