use dict for 'visit' var in day 16

not sure if this is an improvement? it's more or less the same
as the array version
main
magical 2023-12-19 04:57:53 +00:00
parent ef2711b76f
commit 6704766eba
1 changed files with 10 additions and 10 deletions

View File

@ -20,16 +20,16 @@ proc read-input {input} {
proc reset-visited {} { proc reset-visited {} {
global visit global visit
array unset visit set visit [dict create]
} }
proc mark-visited {x y d} { proc mark-visited {x y d} {
global visit global visit
set v [switch -exact -- $d n {expr 1} s {expr 2} w {expr 4} e {expr 8} default {error "invalid dir '$d'"}] 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 dict incr visit $y,$x 0
set result [expr {($visit($y,$x) & $v) != 0}] set old [dict get $visit $y,$x]
set visit($y,$x) [expr {$visit($y,$x) | $v}] dict set visit $y,$x [expr {$old | $v}]
return $result return [expr {($old & $v) != 0}]
} }
proc print-visited {} { proc print-visited {} {
@ -39,7 +39,7 @@ proc print-visited {} {
for {set i 0} {$i < $height} {incr i} { for {set i 0} {$i < $height} {incr i} {
set row {} set row {}
for {set j 0} {$j < $width} {incr j} { for {set j 0} {$j < $width} {incr j} {
if {[info exist visit($i,$j)] && $visit($i,$j)} { if {[dict exists $visit $i,$j] && [dict get $visit $i,$j]} {
lappend row "#" lappend row "#"
} else { } else {
lappend row "." lappend row "."
@ -93,7 +93,7 @@ proc solve {} {
set beams [follow $beams] set beams [follow $beams]
} }
print-visited print-visited
puts [array size visit] puts [dict size $visit]
} }
proc lmax {list} { proc lmax {list} {
@ -125,8 +125,8 @@ proc solve2 {} {
while {[llen $beams] > 0} { while {[llen $beams] > 0} {
set beams [follow $beams] set beams [follow $beams]
} }
puts [array size visit] puts [dict size $visit]
lappend l [array size visit] lappend l [dict size $visit]
} }
puts [lmax $l] puts [lmax $l]
@ -135,7 +135,7 @@ proc solve2 {} {
read-input stdin read-input stdin
puts [array get grid] puts [array get grid]
puts [follow {0 0 e}] puts [follow {0 0 e}]
puts [array get visit] puts [dict get $visit]
solve solve
solve2 solve2