From 94bf752568cdc9e23c1cdf48f7b984fa2432285e Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sat, 16 Dec 2023 06:02:16 +0000 Subject: [PATCH] day 16 part 2 --- day16/sol.tcl | 55 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/day16/sol.tcl b/day16/sol.tcl index e22babd..5d27703 100755 --- a/day16/sol.tcl +++ b/day16/sol.tcl @@ -24,17 +24,19 @@ proc mark-visited {visitp x y d} { return $result } -proc follow {gridp visitp beams} { +proc follow {gridp visitp beams {first 0}} { upvar $gridp grid upvar $visitp visit set newbeams {} foreach {x y d} $beams { - if { [mark-visited visit $x $y $d] } continue - switch $d { - n { incr y -1 } - s { incr y +1 } - w { incr x -1 } - e { incr x +1 } + if {!$first} { + if { [mark-visited visit $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)]} continue switch -exact "$grid($y,$x) $d" { @@ -60,14 +62,15 @@ proc follow {gridp visitp beams} { proc solve {} { global grid - set beams {-1 0 e} + set beams [follow grid visit {0 0 e} 1] while {[llen $beams] > 0} { set beams [follow grid visit $beams] } set t 0 - for {set i 0} {$i < 110} {incr i} { + set N 10 + for {set i 0} {$i < $N} {incr i} { set row {} - for {set j 0} {$j < 110} {incr j} { + for {set j 0} {$j < $N} {incr j} { if {[info exists visit($i,$j)] && $visit($i,$j)} { lappend row "#" incr t @@ -80,8 +83,40 @@ proc solve {} { puts [array size visit] } +proc lmax {list} { + set t 0 + foreach x $list { if {$t < $x} {set t $x} } + return $t +} + +proc solve2 {width height} { + global grid + 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 { + array unset visit + set beams [follow grid visit $beams 1] + while {[llen $beams] > 0} { + set beams [follow grid visit $beams] + } + puts [array size visit] + lappend l [array size visit] + } + + puts [lmax $l] +} + read-input grid stdin puts [array get grid] puts [follow grid visit {0 0 e}] solve +solve2 110 110