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 versionmain
parent
ef2711b76f
commit
6704766eba
|
@ -20,16 +20,16 @@ proc read-input {input} {
|
|||
|
||||
proc reset-visited {} {
|
||||
global visit
|
||||
array unset visit
|
||||
set visit [dict create]
|
||||
}
|
||||
|
||||
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
|
||||
dict incr visit $y,$x 0
|
||||
set old [dict get $visit $y,$x]
|
||||
dict set visit $y,$x [expr {$old | $v}]
|
||||
return [expr {($old & $v) != 0}]
|
||||
}
|
||||
|
||||
proc print-visited {} {
|
||||
|
@ -39,7 +39,7 @@ proc print-visited {} {
|
|||
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)} {
|
||||
if {[dict exists $visit $i,$j] && [dict get $visit $i,$j]} {
|
||||
lappend row "#"
|
||||
} else {
|
||||
lappend row "."
|
||||
|
@ -93,7 +93,7 @@ proc solve {} {
|
|||
set beams [follow $beams]
|
||||
}
|
||||
print-visited
|
||||
puts [array size visit]
|
||||
puts [dict size $visit]
|
||||
}
|
||||
|
||||
proc lmax {list} {
|
||||
|
@ -125,8 +125,8 @@ proc solve2 {} {
|
|||
while {[llen $beams] > 0} {
|
||||
set beams [follow $beams]
|
||||
}
|
||||
puts [array size visit]
|
||||
lappend l [array size visit]
|
||||
puts [dict size $visit]
|
||||
lappend l [dict size $visit]
|
||||
}
|
||||
|
||||
puts [lmax $l]
|
||||
|
@ -135,7 +135,7 @@ proc solve2 {} {
|
|||
read-input stdin
|
||||
puts [array get grid]
|
||||
puts [follow {0 0 e}]
|
||||
puts [array get visit]
|
||||
puts [dict get $visit]
|
||||
|
||||
solve
|
||||
solve2
|
||||
|
|
Loading…
Reference in New Issue