day 20 part 2 answer

main
magical 2023-12-22 09:11:41 +00:00
parent fd8de9c7c2
commit dae0597628
1 changed files with 15 additions and 6 deletions

View File

@ -115,13 +115,8 @@ proc solve input {
set count(1) 0
global when
set last $when(dn)
for {set i 1} {$i <= 1000} {incr i} {
pulse broadcaster $i
if {$when(dn) ne $last} {
puts $when(dn)
set last $when(dn)
}
}
puts "$count(0)"
@ -131,10 +126,24 @@ proc solve input {
global type
if {![info exists type(rx)]} return
set last $when(dn)
while {1} {
pulse broadcaster $i
if {$when(dn) ne $last} {
puts $when(dn)
# rx is connected to a conj of 4 circuits
# which each emit 1 high pulse on a periodic cycle.
#
# this could be more difficult but it turns out
# that the step when the first output is produced
# is also equal to the cycle length,
# and all the cycles are a prime number so we don't
# even need to do an lcm
set a [lmul [dict values $when(dn)]]
if {$a != 0} {
puts $a
break
}
#puts $when(dn)
set last $when(dn)
}
incr i