From c592761d00a1d6d7a62bac9ff21a69a465b2dbfe Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sat, 16 Dec 2023 06:57:12 +0000 Subject: [PATCH] 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. --- day15/sol.tcl | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/day15/sol.tcl b/day15/sol.tcl index b0138a8..be1be43 100755 --- a/day15/sol.tcl +++ b/day15/sol.tcl @@ -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}] }