From f3b38a3ecee98f3ed4270f527edfe00aff47fb0c Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Thu, 21 Dec 2023 05:02:12 +0000 Subject: [PATCH] day 20 part 2, sorta finish this up later --- day20/sol.tcl | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/day20/sol.tcl b/day20/sol.tcl index f6d61a5..d10c52d 100755 --- a/day20/sol.tcl +++ b/day20/sol.tcl @@ -6,6 +6,7 @@ proc read-input input { global type global flip global conj + global when while {[gets $input line] >= 0} { # oops set line [replace $line ">" > "&" &] @@ -20,6 +21,7 @@ proc read-input input { } elseif {$sigil eq "&"} { set type($name) "conj" set conj($name) [dict create] + set when($name) [dict create] } else { error "unknown sigil $sigil" } @@ -31,18 +33,20 @@ proc read-input input { } if {$type($to) eq "conj"} { dict set conj($to) $n 0 + dict set when($to) $n 0 } } } } -proc pulse {init {debug 0}} { +proc pulse {init i {debug 0}} { global next global type global flip global conj global count + global when set n $init set pulses {} @@ -78,6 +82,10 @@ proc pulse {init {debug 0}} { } conj { dict set conj($n) $from $value + # remember the last time we saw a high bit from each input + if {$value} { + dict set when($n) $from $i + } set all 1 foreach v [dict values $conj($n)] { if {!$v} { @@ -106,12 +114,13 @@ proc solve input { set count(0) 0 set count(1) 0 - global solved - set solved 0 + global when + set last $when(dn) for {set i 1} {$i <= 1000} {incr i} { - pulse broadcaster - if {$solved} { - puts "rx $i" + pulse broadcaster $i + if {$when(dn) ne $last} { + puts $when(dn) + set last $when(dn) } } @@ -122,16 +131,13 @@ proc solve input { global type if {![info exists type(rx)]} return - while {!$solved} { - pulse broadcaster + while {1} { + pulse broadcaster $i + if {$when(dn) ne $last} { + puts $when(dn) + set last $when(dn) + } incr i - if {$solved} { - puts "rx $i" - break - } - if {$i % 1000 == 0} { - puts "\[$i\]" - } } }