day 16 part 2

main
magical 2023-12-16 06:02:16 +00:00
parent b14209ff39
commit 94bf752568
1 changed files with 45 additions and 10 deletions

View File

@ -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