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 type
global flip global flip
global conj global conj
global when
while {[gets $input line] >= 0} { while {[gets $input line] >= 0} {
# oops # oops
set line [replace $line ">" > "&" &] set line [replace $line ">" > "&" &]
@ -20,6 +21,7 @@ proc read-input input {
} elseif {$sigil eq "&"} { } elseif {$sigil eq "&"} {
set type($name) "conj" set type($name) "conj"
set conj($name) [dict create] set conj($name) [dict create]
set when($name) [dict create]
} else { } else {
error "unknown sigil $sigil" error "unknown sigil $sigil"
} }
@ -31,18 +33,20 @@ proc read-input input {
} }
if {$type($to) eq "conj"} { if {$type($to) eq "conj"} {
dict set conj($to) $n 0 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 next
global type global type
global flip global flip
global conj global conj
global count global count
global when
set n $init set n $init
set pulses {} set pulses {}
@ -78,6 +82,10 @@ proc pulse {init {debug 0}} {
} }
conj { conj {
dict set conj($n) $from $value 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 set all 1
foreach v [dict values $conj($n)] { foreach v [dict values $conj($n)] {
if {!$v} { if {!$v} {
@ -106,12 +114,13 @@ proc solve input {
set count(0) 0 set count(0) 0
set count(1) 0 set count(1) 0
global solved global when
set solved 0 set last $when(dn)
for {set i 1} {$i <= 1000} {incr i} { for {set i 1} {$i <= 1000} {incr i} {
pulse broadcaster pulse broadcaster $i
if {$solved} { if {$when(dn) ne $last} {
puts "rx $i" puts $when(dn)
set last $when(dn)
} }
} }
@ -122,16 +131,13 @@ proc solve input {
global type global type
if {![info exists type(rx)]} return if {![info exists type(rx)]} return
while {!$solved} { while {1} {
pulse broadcaster pulse broadcaster $i
if {$when(dn) ne $last} {
puts $when(dn)
set last $when(dn)
}
incr i incr i
if {$solved} {
puts "rx $i"
break
}
if {$i % 1000 == 0} {
puts "\[$i\]"
}
} }
} }