day 20 part 2, sorta

finish this up later
main
magical 2023-12-21 05:02:12 +00:00
parent 7178a11eee
commit f3b38a3ece
1 changed files with 21 additions and 15 deletions

View File

@ -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\]"
}
}
}