Video summary
The video covers the solution for Day 20 of Advent of Code in Haskell, focusing on a puzzle involving electronic modules that communicate via high or low pulses. The system consists of three types of components: flip-flop modules which toggle their state upon receiving specific pulse levels, conjunction modules that remember recent inputs and only send a low pulse if all connected inputs were recently high, and broadcast modules that replicate signals to all destinations. Additionally, there is an input button module that initiates the process by sending a low pulse when pressed. The primary goal for Part 1 of the challenge is to simulate pressing this button repeatedly until the entire circuit returns to its initial state, counting the total number of high and low pulses sent during one full cycle.
To implement this in Haskell, the presenter defines data structures using sum types to represent different module kinds like flip-flops, conjunctions, and broadcasters, each with their own internal states such as memory maps for inputs or boolean flags for on/off status. The parsing logic reads input lines to construct a map of modules connected by cables, utilizing custom hash functions to manage the large number of connections efficiently. A simulation loop is then constructed where every button press triggers a chain reaction through the network; flip-flops ignore high pulses but toggle with low ones, while conjunctions update their memory and determine output based on whether all inputs are currently marked as having received high signals. The code tracks pulse counts in an accumulator that resets once the system state matches the initial configuration again.
For Part 2 of the puzzle, the objective shifts to finding the minimum number of button presses required for a specific module named RX to receive exactly one low pulse without resetting all modules first. This requires analyzing which flip-flops must be active simultaneously and calculating how many cycles are needed before they align in their high states again. The presenter identifies that some layers consist entirely of flip-flops with predictable toggle intervals, leading to the realization that the solution involves finding the least common multiple (LCM) or product of these cycle lengths depending on whether modules need to be synchronized for a specific signal type. Through iterative testing and mathematical simplification rather than brute-force simulation over millions of presses, the correct formula is derived by separating conjunction logic from flip-flop timing constraints.
The session concludes with successful execution on both example inputs and the actual challenge data, resulting in earning stars for completing Day 20. The presenter notes that this stream marks the end of their current Advent of Code series due to upcoming holiday preparations and travel plans, though they remain open to solving future puzzles if time permits. Throughout the video, there are moments of debugging where initial optimizations prove too slow or incorrect, requiring a shift toward more analytical approaches involving cycle detection and modular arithmetic. The final takeaway emphasizes leveraging Haskell's type system for clarity while acknowledging that performance bottlenecks sometimes necessitate stepping back from direct simulation to apply mathematical insights about periodic behavior in digital circuits.
Read the full video transcript
all
right welcome everyone to day 20 of the
ad of code in has we blazed through uh
18 and 19 yesterday after explaining
our solution for day 18 no day
17 which took us a while now we're
streaming early today but we're going to
do day 20 see if we can play through it
but this going to be my last stream uh
for this season I think because I
actually have to start packing preparing
Chistmas that's what we're here for uh
so what's the problem
today about flip-flop modules okay
conjunction okay so we are connecting
cable and they have modules and they
communicate using pulses so they have
high or low
pulses that's ation modules okay so
flipfold modules are either on or off
and they're initially
off and it
flips if it receives a high pulse it fli
it is ignored if it Rees a low pulse it
flips conjunction modules they remember
the type of the most recent PS received
from each of the connected input modules
the initially default to
remembering
AOW pulse for each
input when a pulse is received the
conjunction module first updates in
memory for that
input then if it remembers High pulses
for all inputs it sends a low pulse
otherwise it
sends a high P okay there one broadcast
module and and it receives it sends the
same p
you have the button module that you
press and send a low
PSE okay so they're always Pro in order
to
ascend
okay we're sending a bunch of signals
and
then okay we can look at this later but
the end goal here
is
we press a button so so basically what's
going to happen is that we have this
state we're going to ah Merry Christmas
gin thank you for tuning in so what was
going to happen is we're going
to
H right so let's
see so you know we send signals again
again again
again and then in the
end and then we complete complet this
cycle
right and
then all the modules are in the original
state so we push the button a thousand
times okay and then we
calculate how many low pulses and how
many high pulses are
sent so we are going to just remember
the
state and then
find the length of the cycle
and then figure out how many
pules
uh how many button presses it takes for
one cycle let's get to
it so here is
our first
[Music]
input let
me rewrite
this
[Music]
CD day
20 make de day 20 see ah I conr it day
20 uh g
o day
20s
GC day 20 Jaz o day
20 03
and day
20 we're going to time
it2
okay let start language GC
[Music]
2021 modu main where main
IO main equals return okay so now we are
going to say uh do read file
example uh print
parts so what is the
state so a
um
so
module data
module equals so we have
a so flip-flop
module
flipflop and this is going to be
uh
on
off is buol so whether
it's is on so this is going to be a
ball
my so is
on they're initially
off and uh ignore nothing happens or if
a flip-flop Is Res it flips between on
off if it was off it turns on and sends
a high
pulse
okay so I think and now we're going to
say uh so this is
a there are cables between
the
um between the modules so let's see here
connections there going to be a list of
ins I'm going to use an INT map
here okay so that's this is one kind of
uh
module we're using sum types and
um row types I think they're called
anyway a conjunction module
conjunction is so they don't have this
is AR
[Music]
thing conjunction remember the B of most
recent
inputs receed from each of their
connected input modules they initially
Def fa remembering low
impulses when a pulse is
received so it's going to be a
memory a conjunction memory is going to
be
um in map
B uh let me
import data. int
map in
map
qualified in map as I
am so they have a
memory and they also have
um
connections okay
[Music]
um then if it blah blah blah
okay there's a single broadcast
module
um when it receives a pulse it sends the
same pulse to all of its destination
modules bro
[Music]
broadcast
Connections
in mhm
let me see uh let me get the so I think
the broadcaster is always going to be
first oh
no
[Music]
okay broadcaster is not always first but
I can
actually I can just move it
[Music]
around so we have connections and there
are
[Music]
ins let me
[Music]
actually
[Music]
okay anyway let's say say here
um broadcaster okay so
instance show module
where uh uh
show Flip
Flop i c is equal to
[Music]
um let's have it
remember
the okay I'm just I'm just not going to
show them I'm just going to to driving
show okay driving
show
EQ and we're going to say
uh pars
module taking one line
and we are going to say a CU module so
pars module uh
string let's look at the first character
um so if the first character is
B then we are defining a
broadcaster what do a broadcaster look
like we
um we say
a we say a
and
mods okay we're going to need split on
also so we're going to say a
drop
length s
Str uh drop
length
broadcaster uh
string and then we are going to say a
mod
list is equal
to
um split
on
um comma of mods
[Music]
okay now these are going to
[Music]
be let me see I'll just throw out the
spaces filter not equal to
space and then we are going to say
[Music]
um is there like a hash function for
INS string to
[Music]
int
[Music]
let me just write the hash function
here
[Music]
um we can use the or function
[Music]
data. string to
in
Ash equals
[Music]
a hash Prime and let's start at
zero um
[Music]
where and then the multiplier so this is
the
power of okay so hash H so this is
Cur and this is a
power empty is
C hash prime a c
p
uh let's say
Cur Cur
CR is equal to a hash Prime
um Car Plus or C
time
10 because I think they're all to the
power of 2 p *
P I think we can do it like this
[Music]
this is like this increase P by two and
then
are let me
see did Char
or let's see
um map
Hash
A
cu
let's see uh let's create all the
combinations a
b a
[Music]
b uh
[Music]
m
[Music]
let's see and then I think we have to do
[Music]
around
[Music]
ah okay so it seems like it does
go longer let's make this
[Music]
three m
[Music]
okay we made a
hash so set is 122 okay and now there's
no collisions in the
hash of the names I don't know why
we necessarily went this way
but we
did let me see and they are all two
letter
names it would also work actually for in
okay
um okay so pars module this is going to
be
[Music]
broadcaster
um map
Hash a mod
list let me just tell AI something
[Music]
okay so this is for the
broadcaster if it starts with
string it starts with a percentage
sign then we are defining a
[Music]
flipflop then we're going to say
[Music]
um we're going to say here uh NM is rest
is
span
um not equal to space of
string okay and
then we
drop uh
[Music]
this mod
list split
on filter not equal to space
of uh drop
[Music]
length space nrow
Bas of
string now this is
rest
okay
and this is equal to flip
flop
[Music]
uh FSE that's
off map hash mod
[Music]
list and exactly the same for the
conjunction except here we have
the and
sign and then we're gonna have a
conjunction it's going to have am. mty
as the initial
[Music]
memory
[Music]
read file example print bar is
a let's see
print and also in the input like yeah
nothing is connected to the broadcaster
just it's just on the left hand side
print do map pars module.
lines
[Music]
I forgot to save
it okay let me
see let's
say map and
print
[Music]
um so we got the broadcaster connects to
a b and
c and then a connects to B and B
connects to
9 and then C connects
to Let's actually have
the let's have the ID here also just so
we have
it uh
m is an
[Music]
in M might
in okay and then we're going to say here
this is going to be M jly ser hero it's
going to be hash of
enm and this is going to be
hash and then
[Music]
12 okay
[Music]
and I want to make it look a bit
[Music]
different
okay now let me not dve show let me say
here
okay
um instance
show module where
show
broadcaster I don't care about the ID
here cons is equal to
[Music]
and that's just then print
broadcaster
and and then I'm just going to show the
connections I'm not going
to
I'm not going to recover the
name I'm just going to use the digit
okay this is
uh and show
my and the same here with
with flipflop but we are going to have a
percentage sign
[Music]
here
nice
okay we've done the parsing
now m
[Music]
D now let's create the
state uh which is just going to
be
um so let's see part one
[Music]
Tak any this strings and okay we're not
going to say that yet part one is is
equal
[Music]
to
where modules is equal
to Map Parts
module
lines
STS
part
one and now let's just print
it
and as I am.
from
list
um
map
we have here a
module we just want to go M id M comma
M okay and now we have all the modules
in the
list
[Music]
okay now I would like to initialize the
conjunction memory so that we don't have
like a map dot
empty see for do we even
need remember the type of the most
recent FS each other the connected so
they remember for low
pulse when a pulse is received the cont
first updates its memory for that
input then if it remembers a high pulse
for
four if remembers High pulses for all
inputs okay so we need to know all the
inputs to that
um a to that module okay so let me
see
so we have
the we just we have the modules
[Music]
Here and Now I want to
[Music]
say h
[Music]
so first I want to know the IDS of all
the conjunctive modules so the I'm going
to say
is conjunct
con
um is
Con
conjunction
true
uh is con is equal to
false let see then con
IDs is equal to
um a map
mid
dot uh mid on
filter is Con
[Music]
modules
okay now for each of these con IDs I
want to
say
um
connected con input takes in a con
ID and and we get all the input so we
say
[Music]
um let me see so it also it also
sends to all of them in order so it
always finishes sending all of them
right so I think these connections
should actually be a
set
[Music]
so we do
here i. from
list I start from
lless
is. from
list
import qualify data.
inser inser
is import data.
inet
[Music]
inet what happened there
[Music]
64 so con input con
ID is equal to um filter and then we
have
a we have for each
module and we we have uh we're going to
say connect
s
[Music]
um and we have is.
member key
inser uh
con ID
do connections and then we
just want
the
map the nid of
those okay so
now uh I'm going to
[Music]
say I'm I'm going to say map con
input so then I can tell which
ones
[Music]
so now I'm going to say
um add
inputs
[Music]
um and so for any module that's not a
conjunction we just return the thing add
inputs a
conjunction let me see I always forget
how this
[Music]
find so add inputs conjunction
um M
ID and we don't care about the current
memory and we
want and we don't care about the con we
know we have to keep the cons around is
equal to uh
conjunction um M
ID
and then I want to
initialize
um I'm just going to
say i. from I am. from
list
I a zp con input M ID
uh with
repeat
[Music]
false
cons okay now
modules let me see here when I show
conjunctions I want to
show just in between this Arrow here
show
[Music]
and not be the aror let me show show me
mid and I want to do
this and I wanted to uh show
um
let's also show the state
here okay now let's see um
modules add
[Music]
inputs
[Music]
map
okay and now this is initialize the
false initialize the
false
and and now we have the memory here that
99 last signal was
low
[Music]
okay conjunction
models they initially we reach
input when a pulse is received the
conjunction module first updates its
memory for that input then if it
remembers High pulses for all
inputs it sends a low pulse otherwise it
sends a high
pulse
[Music]
okay so this is the current
state except we are also going to have
the I am. from
list
um
going to
[Music]
map we just want all the okay so this is
the
[Music]
state init state is equal
to
now I want to say
go um so I'm going to have the current
state current
state and I am going to say uh we're
going to have the counts of
high high
pulses high
low
pules
low
pulses
um is equal
[Music]
to and then we have the
[Music]
Q
[Music]
and so I I got to I have to memorize
memorize this
also at some point let me
[Music]
see so then I can basically have like a
scene of the current
[Music]
state seene
St so if I don't have any pulses I just
return High
pulses low
pules go
H seen
ASD h i it need to be seen SD C
SD CHP CL
P I don't
care
[Music]
um
um no so here I should actually
Loop this is where I should
Loop should Loop
[Music]
okay let me see here
I so I'm going to say
[Music]
so case a curse
[Music]
State
uh let me see scene
St a cur state
of
[Music]
nothing then I
go I am no uh map.
insert H curse
State high pulses low
pulses
[Music]
um curse state High pulses low pulses
and the initial
one is a pulse to
[Music]
zero and I need map here
also import data.
map import qualified data. map as
map so go
is
a takes a map uh of int map
module to in comma
in this is the memory the memorization
to an INT
map or
module oh my God this is really bad
module uh
to and int comma in and a list
[Music]
of Inc Comm
in now a list of
pulses uh and I need they need to
be I think yeah so they need to be high
or
low
boom and they ret
[Music]
return uh return the
string seen as the map.
insert cor State
uh seene
s let
[Music]
me import qualif data.
map. strict
actually and I think it's
actually okay
um
I also want
to okay now if I've seen it
before just B
HP
PLP I want to
uh r i want to say
uh
[Music]
let's still here just
[Music]
CP just R I just want to
show R comma
CP okay H
now this is the Lo with the scene and
then we just figure out whether we and
we're going to look at the numbers after
we see the example to see the figure the
so could so it will take a while to so
it might not get into the initial state
but it might get into a repeating state
[Music]
right so let's see H
go
[Music]
HBP okay and then
um here we have a l uh
PID
and
uh P level if it's high or low is equal
to
and then we
have the rest of the
pules they should actually be a sequence
cuz we're going to be adding to the end
of
[Music]
it
okay
um so now we're going to say if we're
going to look up
the what it
is let me see
uh
where
so CP Prime is equal
to um ifp level it's high
then HP + one
LP lse HP LP +
1
um equal so we can say case uh case and
then we're going to say
[Music]
um C
SD I am. bang P
of and let me see
hogo uh data.
[Music]
sequence m
[Music]
let me see
um so for now we're
not and let me
see
empty and then we say
here
um
[Music]
okay now it's a sequence case curse
state of a
[Music]
of so if we
if we hit the
[Music]
broadcaster when it receives a pulse it
sends the same pulse to all of its
destination
modules P of
um
broadcaster I don't care about the
[Music]
connections
cons
so the logic from here is a is.
from two
list
cons
um what can I how can
I
let
see oh and I can just append okay I to
list and then
seek I'm going to map
a comma P level
do
[Music]
this okay and seek. from
list
hey so I'm so then I'm going to
just let
n6 equal
this in
a go scene
St car
St CP
Prime
um pules appended with
n6 that was quite nice
nice okay now let's see what we do for
flip flop flip are either on or off they
NE
off if a flip-flop module receives a
high
pulse it is ignored and nothing
happens it flips between on and off if
it was off it turns on
S so let's see H flip flop
mid so if
it's uh St s cons
so
um
if s if not
s then it is simply a go SC St car St CP
Prime pules so then we just ignore the
PSE okay we just ignore the pulse it is
ignored and nothing happens if a f
module receives a low
pulse
uh let's see so P level
and not
[Music]
so let me
see okay
so so flip-flop modules are either on or
off they are initially off okay it is a
high potic isign and nothing happens
okay so however if
a so if if not P
level
then if s so if it's
on
then they are are either on or off they
are initially
off if a flip poop module receives a
high pulse if a flip poop module
receives a low pulse if let's between it
was
off um
then let
and flip NFL is equal
to uh
flip-flop mid not
as
con I'm realizing now that this is going
to be
uh or is it going to be like expensive
it would just be nicer we could just
track the flip-flop in a Boolean array
or like
Boolean thing directly and not have to
look it up every time but anyway Cur St
is equal
to i.
insert
mid
NFL
[Music]
and
Sig is equal
to if it was half so this is going to
be
map
not Bel not s so if it was off it should
send a high
pulse
it was on it s turns off and send a low
PSE okay
[Music]
um let me write this a
and s
in go SC State uh car SD Prime CP
Prime
houses I am
NFL
okay this is for the flip-flop now for
the
conjunction so first of all let and see
equal flip flop we're not going to
change the flip we say con
conjunction mid
the M Prime
cons now M
Prime uh and I need to remember who sent
the
pulse P
ID
sender
ID
uh let's just say that the button sends
from itself that's
okay
[Music]
H
see see
[Music]
comma
mid C
comma mid
comma P
level so
here it does just does this here again
see
to um C comma s comma not
I need to have the center ID as
well okay so M
Prime remember they have the most recent
PS received from each of their connector
modules so it's first updates its
[Music]
memory m is I am do
insert sender
ID P level
[Music]
m in and find I just want to get the
[Music]
andse I forgot to update the type
here
M Prime is a new okay we update the Mumy
we update the
module when a POS you up remember then
if it remembers to high P for all
inputs and then let's update
[Music]
the current
state
andc and
um
so pulse is equal to
um
and I am.
LMS
h M Prime so the New Pulse so
if if if it Rems High P is for all
inputs it t say low pulse
otherwise so let's
see not
and and then
should n
six n six
[Music]
always and they should
be
I could probably do this in a nicer way
but let me see
um let and Sig comma six
comma C
St
Prime
equal and then I don't have to do as
much
repetition H then n
six
in so here I don't do
anything uh here I also don't do
[Music]
anything
and here we are going to say
also a
n6 car SD
Prime and then
in
go
scene St uh Cur St
Prime and CP
Prime
houses and
[Music]
six let's see they're always sending the
same signal
to all of
[Music]
them oh this should be made
actually
mid
so what I want to say
here is I want to
say
um
let m equal
um Cur S im.
Prime a
ID okay and then I want to say
I want to say here
that cons is equal
to
a cons is equal to
connections a m and Center ID is equal
to M id
M and then uh n Sig
is equal to seek. from
list of uh map
C uh C
Sid p
a
p
h is2 list
cons okay and so I just want to send the
updated State and
the P level and the signal that it
sends
let's
see this is your
bead just P level because sometimes they
don't send a signal okay a
n6 nothing is equal to se.
empty
[Music]
GSP okay
um
nothing and the signal here is not
[Music]
s it's just not
as the signal here is GS
p
okay
in
and
[Music]
P we can actually
[Music]
put these all in the wear
claw as
well I'm just going to make this
code nice and
[Music]
simple ding
ding okay so we update the
state
[Music]
um let me see here we can
say we can just make this a card
also
[Music]
m
[Music]
um let up
a is on equals
[Music]
s
conjunction memory equals
[Music]
me
[Music]
m
[Music]
okay so now we've
updated let's see in it State now let's
see what see guys
go and map.
empty init State and
a
0.0 and se.
Singleton
uh Z comma Z
comma we send a low when we click the
butt
4 4. 8
816 so pressing the
button
once H sends four and
eight let's see let's make this
return uh
in we're going to say RHP so PHP
P
LP CHP
CLP and we are guess going to say uh CHP
minus PHP
comma CLP minus
PLP and we have to
multiply these
[Music]
together let
see let scene SD Prime equals
in
and button press
equals this
in
button
press uh and I need to say whato here
see
p current hules
all
right previous
hypothesis I think I need to
do like this
probably so 32 times button
press
[Music]
um
so after pushing the button 1,000
times
o okay so it's not a th000
[Music]
here I have to multiply by 20,000 times
twice 10
two which is a million
[Music]
actually let's create a new file example
two
[Music]
B
[Music]
key or
output a multi okay this
module the multile connection con with
the both on sends a low PSE to the
output mod
[Music]
well sent to up however now the both f
are on and
gone and untyped module
output okay
so okay so I guess if
um
is the
Cs pity pulse pulse level FES
case
uh
not
CST I am
dot I am.
member
pit go St
[Music]
CST
[Music]
m
go see
St C St
Prime C St CP
Prime
bid
and Cur
SD
in a second pushing button 1,000 times 4
250 low pulses and
2750 High pulses are
sent okay so this one is not
working let's see for the second example
ding ding ding ding
ding doesn't work H so let's see show
[Music]
CP
[Music]
okay
[Music]
um so this is a bit more difficult right
so the thing here is
that
PP so per
[Music]
cycle
[Music]
I just want to see also what it does for
the input if it's slow
or
[Music]
I have the input
here
[Music]
okay certainly take taking a lot of
[Music]
time
[Music]
we get the cables warmed up the elves
have pushed the button 1,000
times including the pulses sent by the
button
[Music]
itself yes we we also count that
[Music]
actually
let's see
um but for the second one so it
took it took four
pushes
[Music]
uh I think I need to
remember how many times I've gone
through the loop let me add
that
here
LP and
uh
um num
presses
[Music]
um
num let's have it
[Music]
there
we're going to start this off with a
zero and then here we're going to
say
um n PS +
one um
PS
[Music]
um n
PS plus
one okay it's not doing the thing let's
see n
[Music]
PS uh let's actually just add
[Music]
it
[Music]
here
this is what we are tracking
[Music]
anyway
[Music]
oh I was pressing too often
um
so this should
[Music]
be okay so after one
press
[Music]
so uhhuh so at one press it returns to
the initial
State and maybe what happens for the
input is that
it's it's always growing
[Music]
the
[Music]
okay so let's uh figure out the
difference here
um so PHP minus
CHP H CLP
minus
um
[Music]
PLP and
um M PP minus
no num PS minus
NPP this should be PLP and
CLP now um so PP was the previous one
yeah so c p c p
p
so for every one press
um we
get okay so then we have to take
[Music]
um the length of the cycle is CL a
is equal to n
[Music]
PS the low per cycle is equal to this
number I per cycle is equal
to this
number in and then we're going to say
[Music]
um
[Music]
P
per C is equal to
um 1,000 mod
CL so we're going to say here a LPC LPC
*
perk times a HPC
times
work
[Music]
oh so this was
NPS
minus n PP
right
oops ah I need to
dite I
think 11
168
75 okay so it's
HPT
plus
um off so and these are the
ones so so and then we do one0 0 mod
C uh
Plus off *
um okay now let's Let Me Wait with this
off
thing let just
see okay at least it works for the
examples
um but it is too slow
for the uh
input
[Music]
M and that's probably because we
[Music]
are it's probably because we
[Music]
are constantly updating this
[Music]
map
[Music]
so Cur
St let's make that
uh let's make that just into
the function that modifies the current
one okay
um
[Music]
f so here we just have
it it we don't want to make any
modification here we Define it as f
a
flipflop
m s
c is equal
to
flipflop m m not
as
[Music]
C so then we don't do this
uh Cur S
[Music]
Prime
[Music]
we just Define the function that
modifies
the uh modifies the
current
um the current
state okay H so
here we update the current one
[Music]
let okay and then her SD Prime is equal
to
um I am.
[Music]
modify
alra let me
see
modify uh in
map
we should have
a uh
adjust
f m
mid
uh change a adjust modify
mid change
mid Cur
SD
okay so
here the
modification is
actually going to be
um f
conjunction
mid
m h m c is equal
to
um
conjunction M M M Prime
C where M Prime is equal
[Music]
to I am do
insert
sit P
[Music]
level
[Music]
M oh I need the M Prime here
also
[Music]
okay it's definitely not
working for the
input
so now we just do the
adjustment so we've kind
of
um we freed ourselves from legs so now
this function does not need to know
about
um they don't need to know
about
the way you store
[Music]
it
[Music]
okay
now I want to change how the current
state has an INT map of
[Music]
modules
[Music]
let me just customize the hash function
here hash
broadcaster
zero Ash
[Music]
output ting
I think I should let the common things
be
first oh I need
to take a Qui quick break
now
actually
um oh sorry
and of course this changes as
well just finish this change
here
[Music]
all right I'm going to take a break now
and help out with some stuff I'll be
back later for finishing this but at
least you know it works on the
example and uh
I'm going to run on the input see what
happens but it's not looking good for
now anyway see you in a bit
bye-bye all right I'm
back needed to help out a bit at the
house but uh as we see
this is clearly
not fast
enough
takes too long
time so let me see
[Music]
here let
me try R
show
oh num
PS
[Music]
import let's see how it's
[Music]
just doing all the button
[Music]
presses
oh
[Music]
let's see case num
PS is larger than equal
to
1,000 then we
do
CHP times
CP case now let's just do like
this
where
let me copy paste
this we don't do it
twice
seen
before so if it hasn't been seen
[Music]
before
then we can just do like
[Music]
this
okay actually um let's just see what
happens if we
just do the
[Music]
loop if it actually just does it
correctly for the
[Music]
examples
no it doesn't get them
right
I think it's because I I should start
at
one so there is one button press
right okay so this is part
one let's just paste that set
in uh this is not the best way to do it
cuz now they're going to push it a
million
times okay continue to part two the
final machine responsible m s down the
to island island has a module attached
named RX the machine turns on when a
single low pulse is sent to
RX reset all modules for the to their
default States waiting for all pulses to
be fully handled after each button press
what is the fewest number of button
press is required to
deliver a single low
pulse to the module named
RX
okay so it's just not at all
um let me
see rxh is equal to
hash
[Music]
RX
nothing I can still do this but
uh just to make it faster
but if in case there a loop I can't do
it what happens otherwise is that it
doesn't never never resets
right the state
keeps
changing which is
okay so we had part one done we were
just trying to we were it was premature
optimization let me see
[Music]
a
case b ID is equal
to
rxh and
P
not P
level is equal
[Music]
to
[Music]
m
he stop at
th000
part one and two
up
th000
so we have
fals do
[Music]
false
[Music]
stop at
[Music]
TH part one and
two
oh dra
walls okay it's taking a long
[Music]
time
[Music]
h
let's just abandon maybe the
caching as for get commit
um day 20 part
one oh get
add add day 20. HS sample sample two
input get commit
[Music]
well I should have okay let's see uh
[Music]
RX so for RX to get a signal uh all of
the inputs to LG need to be
[Music]
true
[Music]
let me see now we're going to abandon
this scene State thing it doesn't
actually
[Music]
work
[Music]
okay now this need some different kind
of
[Music]
analysis
[Music]
h
okay now there's no in it
State let me see
how fast does it do all of them
now
quite
fast
okay let me see
um
just be level
in so the
pulse let me
see let's uh just change
this to just be part one
it would be nice if we had some
um tests for this
right
so we going
[Music]
to say part two
here now
um
and I'm going to remove all
these
now let's
see these are in it
State okay
so rxh is equal to
Ash
RX map.
bang Rex
H
right
[Music]
in my
module map.
bang #
RX
part
two this a string to
a
modu
I I
[Music]
am okay
12114 is not member of the
map
uh okay no wonder the other one didn't
work a hash
RX
[Music]
single to
RX
oh right
okay because RX is not in the
map
[Music]
um
okay let me
see p is equal to Hash
RX
[Music]
uh
[Music]
but SD
1,000
[Music]
let's
see let's just copy paste
[Music]
this and
um let's not stop at
[Music]
1,000 part two
now here I'm also not going to be
counting the
[Music]
pulses I'm just going to say n
[Music]
PS
M mhm
[Music]
oh and here we're not even pushing the
button so it's just
[Music]
NP
[Music]
I don't need tast ratio here
[Music]
anymore h
[Music]
I think I don't need to do this
[Music]
either uh accept
[Music]
um ifp
level
[Music]
then make it slightly
faster but not enough
to do much I
[Music]
assume
[Music]
m
what did I say if it was
output hash output is
[Music]
one let me just make a
hash
RX is equal to
2 so
if so we can check it for the so let me
see
two
PID m
let see PID pidal 1 or pit =
2 and then we can do it with the example
two
right
[Music]
so since the a since the in to
[Music]
con right and I need
[Music]
to
and
con let me
see we have
conjunctions so if it REM High pulses
for all inputs it sends a low
[Music]
pulse
[Music]
NP Cur stage Cur
SD
TS
I just want to see what we get for the
current state in the
[Music]
queue
[Music]
the thing was that it
never never looped in the state
[Music]
so Lo the broadcaster but load to
a high to con High to
con and then in send low to and then
way and then p
and
[Music]
hi to con and
then con sends low to
[Music]
Output however now that both flipflops
are
[Music]
on
[Music]
in
[Music]
map this is
output
[Music]
where skip
equals go C St NP
pulses you see
here
[Music]
then
skip
[Music]
oh now I'm just looping an example to
Great h okay so this will eventually
work but it might take a lot of button
presses hey hey what is the question of
today hey a Felix 3 question of the day
is day 20 we have these
circuit stuff described like this so you
send signals high or low to any of
these and
um
part one was to figure out how many high
and low signals are sent in total if you
press a press a button it sends the
signal to the
broadcaster and the broadcaster sends
you know to its destinations and then
these are either conjunctions or
flipflops so you send
um you send
um
um you send um signals at high and low
so flip-flops only activate if they're
on and then they flip off and they have
conjunction which can combine
signals so now I'm trying to so that was
the first part just count the signals
second part is figure out when
um when you send a signal to a specific
one
so okay I think what I have to do is I
have to figure out
a so in the input I have to figure out
when when does LG
activate so our RX will
activate RX will activate
when LG send
[Music]
um when all of LG's inputs are
high right
so low
pulse so then if it remers high it
pulses for all inputs it sends a low
pulse okay so we're going to do this
differently
and we're going to run it on example to
as
[Music]
well okay so this is the init
State this is
not fast
enough
um so the in it
State let me see
so activation
of a given
gate so St Str
so
mod mod
ID is equal to
um so these are
the con input
[Music]
actually
let me see
so toon
input HH
RX so for uh RX to
activate I need LG to
[Music]
activate
uh LG to be
[Music]
active let's see so con input
hash
RX so these are the inputs
to see con
input
RX so
[Music]
um let see so
[Music]
activates
okay so con input then
map Cur is
so a
modules map let's see init
St I am
dropping
in it
[Music]
State actually to go for a meeting
[Music]
soon okay so
for
[Music]
so for RX to
activate
[Music]
um we need
a high from all of
these at the same time
right I think then we need to kind of
figure out the modulus
right when will all of them be at the
same time in the high
[Music]
stage
[Music]
okay
and
let so to
act RX is equal
to
[Music]
to actr
X
okay then
to RX is equal map in it
State I am
bang do
[Music]
a
memory
I am.
[Music]
Keys memory
to act
[Music]
RX to act
[Music]
RX tox is a list of modules
[Music]
module
[Music]
right now for this is going to be
um map
and I see all of
[Music]
these Okay
so
we have again the case that it's a list
of
[Music]
conjunct to act
act a
to
RX
okay let me see to act
a so we have a
two a
RX we have
two to a
[Music]
RX and then we're going to say
map to act
act to act
a to act
[Music]
ARX
let me see
um l l l l l
l
um we're going to say
[Music]
a map
M we're going to print
L this is just the first
one map M
print
L then uh put
l okay two
map
M map
M print
H and
then map
[Music]
M hey Matt
flash what's up
again they are
all
[Music]
um they're all
[Music]
okay to activate RX I need to activate
this one to activate this one I need
to activate all of
these okay to activate all of
these I need to activate all of these at
the same
[Music]
time let's keep
going
[Music]
map
[Music]
I want to see when do I get to something
that's okay these are all
flipflops all right now I need to map
and
[Music]
so all of these
flipflops are
um send a
message at the same time
[Music]
then it activates okay why do you not
write types just
like Punk
intin uh I mean I do it sometimes
[Music]
right but the power of hascal is that it
can actually infer a lot of it for
me okay H we are almost
there we're going to keep looking at
this in a bit I'm going to take a break
now I have a meeting but so what we have
to figure out is
um how many button presses will it take
for all of
these to
be on at the same
time I think that's a
[Music]
trick all right thank you for today we
saw part one at least
and yeah I'll be back later today but I
have to take a quick meeting see you in
a bit okay he
[Music]
byebye all right we're
back
um oh
about that okay H we're still working on
it
um while I was hacking I hacked a bit
more and we see that on the third
layer it's all flip
flops let me see so I just started
concatenating so let me see here so it's
two act
RX let's so see
a let's just say
h so TAA
is to act
a to act act module to module and we're
going to do conat
map okay
um
[Music]
so we are going to say
here
iterate to act
a take one
iterate to act act to act r
[Music]
x
and then for the
input we are just going to
[Music]
say
um
do h
l so uh
map map
m
LM
[Music]
do map
M print
[Music]
Ms buter
Ln dot part two do
lines so we iterated then we were
basically getting the
same so then we get
[Music]
um so take two take
three
take
four and all of these have to
be
um
sort so all of these have to be active
at the same time so I think we're doing
the
GCM again
here let me see
[Music]
um so now let's
[Music]
copy this one again so the go
again
from this
[Music]
one okay
and so out
[Music]
let's
[Music]
no matching selector memory uh maybe I
need to
do
free
right okay
um so now I'm just going to say
[Music]
here okay and now I'm going to just
map
map M ID on all of
these
um
[Music]
so we have
[Music]
here
BL is equal to all of these now go is
um here we're not going to stop
here we are just going to l
right away
in can put this button press down into
the
we okay
here we are going to so we're not going
to say
rxh let's see and we I want this
skip also
so we're not going to do anything
here but uh let
me
NP I'm going to write this
as
Skip and then we're just going to say
here skip um
otherwise we're not going to need these
121 something's
[Music]
wrong
okay so now I'm going to say here
a um
flop
on
on and the Flop goes
[Music]
on and the Flop goes
[Music]
on when it receives H yeah
so
flop okay so pH P pit is equal
to
flop okay so this is
a let's make this like
this ER
flop it's
still giv wipes okay so I'm going to Now
map
[Music]
a map
go in it
State
um and
then
one we can actually put this button
press I actually have to put
it
here one
a button
press over
um
flops and so this is the number
of button presses required for each flop
to be on
[Music]
and I think I
[Music]
take the greatest common multiple
here I think that was from
day three or
something good crap
G
uh which day was
it not day two day three day two
[Music]
maybe no
was it day
four maybe it was day
five Almanac transform
all
SE
NOP day
six
[Music]
nope let me say get grab
B um C
crap
hold R1
LCM okay what's the
eight yeah
okay so let me see
here
um B
R1
[Music]
LCM
102 so it's saying after
2048
presses
maybe I need to take the
product
your answer is too high
okay it's not the
[Music]
product
[Music]
m
[Music]
maybe I did too many
concats
let's see
um maybe I shouldn't do flops here okay
[Music]
um
[Music]
so this is the first
[Music]
level
okay and now we're going to
map to
a maybe I'm missing
the map map
to act
act
okay map map map M
ID
[Music]
map
m
[Music]
go one is equal to go init
State one button press
now let's
um map
map
map go
[Music]
one
[Music]
so all 248
Cycles this one will be
active and there 24
[Music]
cyes this one will be
active lops
let's see BF
flops then uh we remove one level
here and we moove on
level here I
think so BF
flops so this a right before the flops
let me
see
[Music]
M then is map map map
go
one
flops
took two number presses before they
were all
pressed let see
BFF
flops
uh but that one needs to be
high it's not enough
to
yeah go one
a
HL
[Music]
HL
HL P levels equal to
HL and then we just need to add HL
here and HL
here and HL should be here
also mhm
map go go
one here needs to be
false
oh okay I need so HL and flop
Bo
HL
flop HL
[Music]
flop map M go
one
[Music]
true
[Music]
button press HL okay yeah I need to SW
it here
also so here we want a low
activation here we want a
[Music]
true
[Music]
mhmm
[Music]
okay so flipflops
if
so FPS if it was sof turns on and sends
a high
pulse
okay if it was off it turns on and sends
a high pulse if it was on it turns off
and sends a low pulse so for flops we
want them to receive a low
pulse okay so if it's BF
flops we're waiting for them to send
a high pulse to all of
their
um so in one round they all
send
M I'm not quite buying this me
see
BFF then if
it's
ah cuz here it is
[Music]
um okay so how long does it take for it
to
send a high flop
[Music]
that is the
question okay this is how long it takes
for it to
send a high flops so before the
flops how long does it take for to send
a
low a low
flop cuz then all of them have to be
[Music]
active okay
okay that is
surprising let me
see is it just some
here nice it's just a
sum
okay and I think it's just some here
[Music]
also concat
map a conat
[Music]
map
some concat
[Music]
map
let me see
here concat
map okay it doesn't want
[Music]
that
[Music]
I think this might be the
answer seems so low though
but it's too
[Music]
low okay
um let's abandon this
do we have the sum here
[Music]
again so that's the answer to that
one maybe
for these
ones it's actually
the
[Music]
product
so I want all of these to send a high
signal so
that
b f f
f map
[Music]
m
BFF
fls
map go one
HS
BFF
[Music]
I think it's probably then the product
of
those
sh
all
right we got day 20 part two whoop
whoop took some guess work but
[Music]
um let me
see
see so this is a
[Music]
su and then this was a
[Music]
product here actually a product of all
of
[Music]
these so the idea is okay so this is the
number
of so for the flops you have to
check
um cuz they flip on and off right so
then
um every 3,851 Cycles they all they all
flipped flip on at once right and now
they all have to do this at the same
time so then it is the product of these
numbers
good status get
add which is get commit
[Music]
M day 20
finished okay St is am I forgetting
something example
M get at do day
17
example um
commit add
forgotten
SLE get push okay uh so we did day 20
part one we did day 20 part two
[Music]
takes a while we have
now 40 stars on the ID
code H but
sadly this is the last episode for
now because tomorrow I am
traveling and I have to pack I have to
get ready then that Christmas is
happening um what I might do though is I
might
a I might solve the
things if I have time it's unlikely and
then go over the solutions or otherwise
you know if I'm up for it to do
something in January but anyway it's
been super nice to
uh it's been fun to do the ad of code
it's been nice to have some company for
the ad. C and uh yeah thanks for tuning
in this year and hope to see you all in
the comment
section uh tell me what you think about
the
solutions and uh yeah hope you had as
much fun as I did all right thank you
and
[Music]
bye-bye