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.
main
magical 2023-12-16 06:57:12 +00:00
parent 94bf752568
commit c592761d00
1 changed files with 3 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}]
}