 All right. Welcome everyone to day 20 of the Advertisement of Code in Haskell. We blazed through 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 gonna do day 20. See if we can blaze through it, but this is gonna be my last stream for this season, I think, because I actually had to start packing. Preparing Christmas. That's what we're here for. So what's the problem today? It's about flip-flop modules. Okay, conjunction. Okay, so we are connecting cable and they have modules and they communicate using pulses. So they have higher or lower pulses. Destination modules. Okay, so flip-flop modules are either on or off and they're initially off. And it flips. If it receives a high pulse, it is ignored. If it receives a low pulse, it flips. Conjunction modules, they remember the type of the most recent pulse. We see it from each other connected input modules. They initially default to remembering a low 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 pulse. There's one broadcast module and it receives the same pulse. And you have the button module that you press and sends a low pulse. Okay, so they're always processing the order that you send. Okay. We're sending a bunch of signals and then... Okay, we can look at this later. What the end goal here is, we press a button. Basically, what's going to happen is that we have this state. We're going to say, oh, Merry Christmas. Thank you for tuning in. So what's going to happen is we're going to... Right, so let's see. We send signals again and again and again. And then in the end, and then we complete a 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 pulses... How many button presses it takes for one cycle. Let's get to it. So here is our first input. We write this CD. Day 20. Make dear day 20. Day 20. G-C-O-G-S. G-C-D-20.HS-O-D-20. O-3-D-20. We're going to time it. Okay. Start language, G-C-21. Module main, where? Main. Main equals return. Okay, so now we are going to say... Do read file example... print.parts. So what is the state? So a module... Dear module equals... So we have a... So flip-flop module. Flip-flop. And this is going to be... On-off is a pull. So whether it's... is on. So this is going to be a pull. So it's on. They're initially off. And in order nothing happens. Parts of flip-flop is resettled off of 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... So this is a... There are cables between the... between the modules. As you can see here, connections... It's going to be a list of... I'm going to use an IntMap here. Okay, so this is one kind of module. We're using some types and... road types. I think they're called... Anyway, a conjunction module. Conjunction. Is... So they don't have this is on thing. Mm-hmm. Conjunction. Remember the most recent impulse. Received from each of their connected input modules. They initially default to remembering low impulses. When a pulse is received. So it's going to be a memory. A conjunction memory is going to be... IntMap pull. Let me import... data.intmap. IntMap... Qualified.intmap. So they have a memory. And they also have... connections. Mm-hmm. Okay. Then if it... There's a single broadcast module. When it receives a pulse, it sends the same pulse to all of its destination modules. Mm-hmm. Broadcast connections. Int. Mm-hmm. Mm-hmm. Let me see. So I think the... Broadcaster is always going to be first. Oh no. Okay. Mm-hmm. Broadcaster is not always first. But I can actually... I can just move it around. Mm-hmm. So we have connections and there are ints. Mm-hmm. Let me actually... Mm-hmm. Okay. Anyway, let's say here... Broadcaster. Okay, so instance... show module. Where? Show... flip... flop. Is he... is equal to... Let's have it remember the... Okay, I'm just not going to show them. I'm just going to do a deriving show, okay? Deriving... show... EQ. And we are going to say... parse module. Taking one line and... we are going to say a... give it a module. So parse module. String. Let's look at the first character. Mm-hmm. So if the first character is B, then we are defining a broadcaster. What does a broadcaster look like? We... we say a... we say a... mods. Okay, we're going to need to split on also. Mm-hmm. Mm-hmm. So we're going to say a drop... length... HDR. A drop length... broadcaster... string. And then we are going to say a... mod list... is equal to... um... split on... um... comma... of mods. Mm-hmm. Mm-hmm. Okay, now these are going to be... let me see. I'll just throw out the spaces. Filter... not equal to space. And then we are going to say... um... Mm-hmm. Mm-hmm. Is there like a hash function? It's... string to int. Mm-hmm. Let me just write the hash function here. Um... We can use the ORD function. Mm-hmm. Then add an ORD... string to int... hash equals a... hash prime. And let's start at zero. Mm-hmm. Um... where... and then the multiplier, so this is the... power of... okay, so hash... so this is cur... and this is a power... empty is c... hash prime... a c... p... uh... let's say cur... cur... c... r... is equal to a hash prime... um... cur plus... or c... times... 10... because I think they're all... to the power... of 2p times p. I think we can do it like this. Mm-hmm. Let's just do it like this. Increase p by 2 and then r. Do that at char ord. Let's see... um... map hash... a... to z. Mm-hmm. Let's see... let's create all the combinations. a... b... a... b... Mm-hmm. Let's see, and then I think we have to do... Okay. So it seems like it does go... longer. Let's make this 3. Mm-hmm. Mm-hmm. Mm-hmm. 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 inf... Okay. Okay. So parse module... this is gonna be... broadcaster... um... hash... a... modelist. Let me just... do... do... Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. Okay. So this is for the broadcaster. If it starts with... string... it starts with a... percentage thing. Then we are defining a flip-flop. Then we're gonna say... um... we're gonna say here, NM is... span... not equal to space... of string. Okay. And then... we drop... this... split on... filter... not equal to space... of... uh... drop length... space... narrow... space... of string. Now, this is rest. Okay. And... this is equal to flip-flop... Mm-hmm. Uh... fall, so it's off... map hash... modelist. Mm-hmm. Mm-hmm. Mm-hmm. And exactly the same for the... conjunction, except here we have the... N sign. And then we're gonna have the... conjunction. Mm-hmm. It's gonna have an am.empty as the initial memory. Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. Read file, example print par is a... let's say print. And also in the input, like, yeah, nothing is connected to the broadcast. It's just on the left-hand side. Dot... map par's module... dot lines. Mm-hmm. I forgot to save it. Okay. Let me see. And let's say... map and print. So we got the... broadcaster connects to A, B and C. And then... B connects to... B... B connects to 9. And C connects to... Mm-hmm. Mm-hmm. Let's actually have the... let's have the ID here also, just so we have it. Uh... MIT is an int. Mm-hmm. Mm-hmm. MIT int. Okay. And then we're gonna say here this is gonna be a module ID zero. It's gonna be hash of NM. And this is gonna be... hash... NM as well. Mm-hmm. Mm-hmm. Okay. I wanna make it look a bit different. Mm-hmm. Mm-hmm. Mm-hmm. Ding-ding-ding-ding. Buddy. Okay. Now let me... drive 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... Mm-hmm. Mm-hmm. And that's just then print broadcaster. Mm-hmm. And then I'm just gonna show the connections. I'm not gonna... I'm not gonna recover the name. I'm just gonna use the digit. Okay. This is a... and... show... and the same here with... laptop. But we are gonna have the... center sign here. Nice. Okay. We've done the parsing. Now... Mm-hmm. Mm-hmm. Now let's create the state. Uh... which is just gonna be... let's say part one... takes any of these strings and... okay. We're not gonna say that either yet. Part one... is equal to... Mm-hmm. Where... modules is equal to... mapParsModule.Lines... Stairs. Part one. And now let's just print it. Mm-hmm. As though I am... not from... the list. Um... map... we have here a module. We just wanna go... we just wanna go... m... Okay. And now we have all the modules in the list. Mm-hmm. Mm-hmm. And now... I would like to initialize the conjunctive memory so that we don't have like a map.empty. Mm-hmm. Mm-hmm. See, for... do we even need... Remember the type of the most recent files received from each other connected. So they remember low pulse. When a pulse is received, the conjugal first update is memory for that input. Then if it remembers a high pulse before... remembers high pulses for all inputs. Okay, so we need to know all the inputs to that... to that module. Okay, so let me see. Mm-hmm. Mm-hmm. Mm-hmm. So we have the... we just... we have the modules here. Mm-hmm. Mm-hmm. And now I wanna say... Mm-hmm. So first I wanna know the IDs of all the conjunctive modules. So then I'm gonna say is... conjun... conjun... conjun... iscon... conjunction... through... um... iscon is equal to false. Let's say then con IDs is equal to um... mapMID dot... uh, MID on filter iscon modules. Mm-hmm. Mm-hmm. Okay. Now for each of these con IDs I want to say um... connected con input takes in a con ID and we get all the inputs. So you say um... let me see. So it also it also sends to all of them in order. It finishes sending all of them, right? So I think these connections should actually be a set. Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. Mm-hmm. So we do here IS dot from list IS dot from list IS dot from list um... import qualified data.inset inset as IS import data.inset inset What happened there? 964 So con input con ID is equal to um... filter and then we have for each module and we have, we're going to say connections um... and we have IS dot member key inset uh... con ID dot connections and then we just want the map map the NID of those mm-hmm mm-hmm. Okay, so now I'm going to say I'm going to say map con input so then I can tell which ones So now I'm going to say um... add inputs um... for any module it's not a conjunction we just return the thing add inputs a conjunction let's see let's forget how things are defined so add inputs conjunction um... mm ID and we don't care about the current memory we want we don't care about the con we have to keep the cons around IS equal to conjunction um... mm ID and then I want to initialize um... I'm just going to say IS dot from I am dot from list uh... zip con input mm ID with repeat false cons okay, now modules let me see here when I show conjunctions I want to show just in between this arrow here show not between the arrow let me show this show mm ID and I want to do this and I want to do uh... show let's also show the state here okay, now let's see modules add inputs map and now this is initialize the false initialize the false and and now we have the memory here that 99 the last signal was low mm okay, conjunction models they initially 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 mm mm mm mm mm mm mm mm mm okay, so this is the current state except we are also gonna have the IAM dot from list um we're gonna map we want all the okay, so this is the state init state is equal to now I want to say go and so I'm gonna have the current state, current state and I am going to say we're gonna have the accounts of high high pulses high low pulses low pulses is equal to mm mm mm mm mm mm mm the two I have to memorize this also at some point let me see so then I can basically have like a scene of the current state scene st so if I don't have any pulses I just return high pulses low pulses mm mm mm mm mm so a scene st uh it needs to be scene st curr st chp, clp I don't care um no, so here I should actually loop this is where I should loop should loop okay let me see here so I'm gonna say case curr state scene st curr state of nothing then it's go I am map.insert curr state high pulses low pulses um curr state high pulses low pulses and the initial one is a pulse to zero I need map here also import data.map import qualified data.map smap mm mm so go is a takes a map uh of intmap module to int comma int this is the memory the memorization to an intmap of module oh my god this is really bad module uh and int comma int a list of int comma int so a list of pulses uh and I need they need to be I think yeah so they need to be higher low oh and they return uh return a string map.insert curr state scene st let me import qualified data.map. strict actually and I think it's actually mm mm okay and I also wanna okay now if I've seen it before just bhp plp plp I want to uh I want to say let's do here just cp just r just wanna show r comma cp okay now this is the loop with the scene and then we just figure out whether we I mean we're gonna look at the numbers as we see the example this again the figure of the so it will take a while to so it might not get into the initial state but it might get into a repeating state right so let's see go hplp okay and then here we have a lost pid and p level if it's higher or low it's equal to mm mm and then we have the rest of the pulses they should actually be a sequence because we're gonna be adding to the end of it okay and so now we're gonna say we're gonna look up the what it is let me see where mm mm so cp prime is equal to mm if p level it's high then hp plus 1 lp else hp lp plus 1 equal so we're gonna say case case and then we're gonna say a ker im.bang pid let me see hogo data.sequence mm mm let me see so for now or not and let me see empty and then we say here okay now it's a sequence case, ker state of imp pid of so if we if we hit the broadcaster mm when you see the pulse it sends the same pulse to all of its destination modules PID of broadcaster I don't care about the connections cons so the logic from here is a is.from to list cons and how can I mm mm mm mm mm mm mm let me see I can just append is to list and seek I'm gonna map a comma p level to this okay and seek.from list so then I'm gonna just let n6 equal this in a go scene st car st cp prime a pulse is impended with n6 that was quite nice okay now let's see what we do for flip-flop flip-flop rather on or off if a flip-flop module receives a high pulse it is ignored and nothing happens it flips between or off if it was off it turns on so let's see a flip-flop mid so if it's st s cons so um if s if not s then it is simply a go scene st car st cp prime pulses so then we just ignore the pulse okay we just ignore the pulse it is ignored and nothing happens if a flip-flop module receives a low pulse let's see so p level and not so let me see okay so so flip-flop modules are either on or off they are initially off okay it receives a high pulse it is ignored and nothing happens okay so however if a if not p level then if s so if it's on then they are either on or off they are initially off if a flip-flop module receives a high pulse flip-flop module receives a low pulse if it was off if it was off um then let nfl is equal to a flip-flop made not as cons I'm realizing now that this is going to be uh this is 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 anyway car st is equal to im.insert mid nfl nsig is equal to if it was off so this is going to be map not below not as so if it was off it should send a high pulse it was on or off turns off and sends a low pulse okay and let me write this nsig in go scene state car st prime cp prime pulses im nfl okay this is for the flip-flop now for the conjunction so first of all let nc equal flip-flop we're not going to change the flip-flop we're going to take a conjunction mid man prime cons now man prime and i need to remember who sent the pulse pid sender id let's just say that the button sends from itself that's okay a c c, mid c, mid, p level so here it just does this here again c to c, cid, not need to have the sender id as well okay so man prime remember they have the most recent pulse received from each of their connected modules so it first updates its memory mem is im dot insert sender id p level mem in undefined i just want to get the nc i forgot to update the type here mm-hmm man prime is a new, okay we update the mami, we update the module mm we're going to positively update its memory and then if it remembers a high pulse for all inputs mm and then let's update the current state nc and a pulse is equal to mm dot lm mem prime so the new pulse, so if if it remembers high pulse for all inputs it sends a low pulse otherwise so let's see not and and then nc nc nc always mm nc i could probably do this in a nicer way but mm-hmm let me see let nc comma 6 comma r prime equal and then i don't have to do as much repetition then n6 in so here i don't do anything here i also don't do anything and here we are gonna say also n6 rst prime and then in go scene st ker sd prime sdp prime pulse is n6 mm-hmm and let's see they're always sending the same signal to all of them mm-hmm mm-hmm mm-hmm mm-hmm mm-hmm now this should be mid actually mid so what i wanna say here is i want to say let m equal m ker sd i am prime eid and then i wanna say i wanna say here that cons is equal to cons is equal to connections m and center id is equal to mid m and then n6 is equal to seek dot from list of map c c cid p eid p is dot to list cons mm-hmm mm-hmm okay and so i just want to send the updated state and the p level and the signal that it sends and see this should be gsp level because sometimes they don't send a signal okay a n6 nothing is equal to seek dot empty gsp okay nothing and the signal here is not s it's just not s mm-hmm the signal here is just okay in m p mm-hmm we can actually put these all in the where clause as well i'm just gonna make this code nice and simple okay so we update the state um let me see here we can say we can just make this a cart also mm-hmm mm-hmm flip flop a is on equals s mm-hmm mm-hmm conjunction memory equals mm-hmm mm-hmm mm-hmm mm-hmm mm-hmm okay so now we've updated let's see unit state now let's see what you guys go and map dot empty unit state and a 0.0 and c dot single 10 0, 0, we send a low 44.8 816 mm-hmm mm-hmm so pressing the button once sends 4 and 8 we'll see let's make this return int we're gonna say r-hp p-h-p a-l-p mm-hmm c-h-p c-l-p and we are just gonna say c-h-p minus p-h-p comma p-p-l-p and we have to multiply these together and see let's seen st prime equals button press equals this in button press mm-hmm mm-hmm and I need to save also here current hypothesis alright previous hypothesis I think I need to do like this probably so 32 times button press so after pushing the button 1000 times okay so it's not a thousand here I have to multiply by 2000 times twice times 2 2 2 2 2 which is a million actually mm-hmm-hmm mm-hmm-hmm mm-hmm mm-hmm mm-hmm-hmm mm-hmm-hmm let's create a new file example 2 winky mm-hmm fitting multi okay this module a multi but connection con if it both on sends a low pulse to the output module well it was sent to output however now both of them are on and gone an untimed module output okay so okay so I guess if is the seems to be also the pulse pulse level is case not cst im. im.member pit go st cst mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm seen st curr st prime curr st cp prime pit and curr st in the second example I've pushed the button 1000 times for 250 low pulses and 270-50 high pulses are sent okay so this one is not working let's say for the second example ding ding ding ding ding doesn't work so let's see cpp okay so this is a bit more difficult so the thing here is that pp so per cycle mm-hmm-hmm mm-hmm-hmm I just want to see also what it does for the input if it's slower I have the input here mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm certainly taking a lot of time because the cables warmed up we also pushed the button 1000 times including the pulses sent by the button itself yes we also count that actually mm-hmm-hmm mm-hmm-hmm let's see but for the second one it took 4 pushes I think I need to remember how many times I've gone through the loop let me add that here LP and uh num num puruses num let's have it there mm-hmm-hmm we're gonna start this off with zero and then here we're gonna say num ps plus one num ps mm num ps plus one okay it's not doing the thing let's see num ps add it here mm-hmm-hmm this is what we are tracking anyway mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm I was pressing too often so this should be okay so after one press so uh-huh so after one press it returns to the initial state and maybe what happens for the input is that it's it's always growing mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm mm-hmm-hmm okay so let's figure out the difference here um so php minus chp and clp minus um plp and um num and pp minus no num ps minus and pp this should be plp and clp now um so pp was the previous one yes so clp, cp pp so for every one press um we get okay so then we have to take um-hmm-hmm the length of the cycle is cl a is equal to num ps a low per cycle is equal to this number high per cycle is equal to this number aim and then we're going to say um mm-hmm p per c is equal to um 1000 mod cl so we're going to say here a lpc lpc times perk times a hpc times perk mm-hmm so this was num ps minus and pp right? I need to divide I think 11687500 okay so it's hpc plus um so and these are the ones so so then we do 100 mod cl plus off times a let me wait with this off thing and just see okay at least it works for the examples but it is too slow for the inputs mm-hmm mm-hmm and that's probably because we are mm-hmm it's probably because we are constantly updating this map mm-hmm mm-hmm ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding ding m s c is equal to flip flop m m not s c so then we don't do this curve as the prime we just define the function that modifies the modifies the current the current stage okay so here we update the current one let okay and then per sd prime is equal to i m dot modify alter let me see modify uh int map we should have a adjust adjust f m i d change adjust modify m i d change m i d curve sd okay so here the modification is actually going to be um um f conjunction mid-mem m c is equal to um conjunction m m prime c m prime is equal to i m dot insert sit p level mid-mem prime here also 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 like so now this function does not need to know about um they don't need to know about the way you store it okay now i want to change how the current state it was an awesome int map of modules let me just customize the hash function here hash broad cluster zero hash output i think i should let the common things be first oh i need to take a quick quick break now actually and oh sorry of course this changes as well let me just finish this change here all right i'm gonna take a break now uh 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 gonna run it on the input be what happens but it's not looking good for now anyway see you in a bit bye bye all right i'm back need to help out a bit at the house but as we see this is clearly not fast enough it takes too long time so let me see here let me trace show oh num ps import let's see how it's doing on the button presses let's see case num ps it's larger than equal to 1000 then we do chp times clp case now let's just like this let me copy paste this we don't do it twice so if it hasn't been seen before then we can just do like this okay actually let's just see what happens if we just do the loop if it actually just does it correctly for the examples no it doesn't get them right i think it's because i should start at one so there is one button press right okay so this is part one let's just paste that in this is not the best way to do it because now they're gonna push it a million times okay continue to part two the final machine responsible women sent down the to island island has the module attached named rx the machine turns on when a single low pulse is sent to rx where is it all modules for their to their default states waiting for all pulses to be fully handled after each button press what is the fewest number of button presses required to deliver a single low pulse to the module named rx okay so it's just not at all um let me see rx h is equal to hash rx nothing i can still do this but just to make it faster but if in case there's 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 a case pid is equal to rx h and p not p level is equal to stop at thousand one and two stop thousand so we have falls to falls stop at thousand part one and two throw falls okay it's taking a long time okay let's just abandon maybe the caching let's first git commit um day 20 part one at day 20 dot hs log for example two inputs git commit i should have okay so let's see uh rx so for rx to get a signal all of the inputs to lg need to patrol let me see now we're gonna abandon this scene state thing it doesn't actually work um okay now this needs some different kind of analysis okay now there's no in its state let me see how as does it do all of them now quite fast let me see um just be level in so the pulse let me see let's just change this to just be part one it'd be nice if we had some tests for this right so we're gonna say part two here now um and i'm gonna remove all these no let's see these are in its state okay so rxh is equal to hash rx map dot bang xh right in my module map dot bang hash rx part two there's a string to a module okay 120114 is not a member of the map uh okay no wonder the other one didn't work a hash rx signal sent to rx right okay mm-hmm because rx is not in the map um okay let me see it is equal to hash rx uh but mm-hmm that's the 1000 let's see let's just copy paste this and let's not stop at 1000 part two now here i'm also not gonna be counting the pulses i'm just gonna say non ps mm-hmm oh and here we're not even pushing the button so it's just np mm-hmm i don't need this ratio here anymore i think i don't need to do this either uh except in fp level then make it slightly faster but not enough to do much i assume mm-hmm what did i say if it was output hash output is one we just make hash rx is equal to two so if so we can check it for the so let me see two pit does it pit pit equals one or pit equals two and then we can do it with the example too right so since the a since the n went to con right and i need to and con let me see we have conjunctions so if you remember high pulses for all inputs it sends a low pulse mm-hmm np current state current st ts i just want to see what we get for the current state in the q the thing was that it never never looped in the state so it sends a lot of the bar caster but then low to a high to con high to con and then it sends low to a and then con to sideway or then b sends um high to con and then con sends low to output however now that both flip flops are on mm-hmm hit map this is output where skip equals go current st np pulses you see here then oh now i'm just looping an example too great okay so this will eventually work but it might take a lot of button presses hey what is the question of today hey if you like three question of the day is day 20 we have these circuit stuff described like this so you'll send signals higher 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 in the broadcaster sends you know to its destinations and then these are either conjunctions or flip flops so you send um you send um you send um signals that are high and low so flip flops only activate if they're on and then they flip off and they have con structures which can combine signals so now i'm trying to so that was the third part i'll 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 sends when all of lg's inputs are high right so low pulse so then if it had remembers high impulses for all inputs it sends a low pulse okay so we're gonna do this differently and we're gonna run it on example too as well okay so this is the in its state this is not fast enough um so the in its state let me see so activation of a given gate so str so mod mod id is equal to um so these are the con input actually let me see so so con input hash rx so for uh rx to activate i need lg to activate uh lg to be active let's see so con input hash rx so these are the inputs too con input rx so um i think so activates mm-hmm okay so con input then map curr s so a modules map let's see in its t i am dropping in its state partly to go for a meeting soon okay so for so for rx to activate we need a high from all of these at the same time right i think that we need to kind of figure out the modules right when will all of them be at the same time in the high state mm-hmm okay and let so 2x rx is equal to 2x rx okay okay then 2x rx is equal to map in its state i am dot bang dot a memory i am dot keys dot memory 2x rx 2x rx 2x rx is at least modules mm module now for this is going to be a map m i don't see all of these okay so we have again the case that it's a list of conjuncts 2akak 2ak rx okay let me see 2akak uh so we have a 2ak rx we have 2akak 2ak rx and then we're going to say map 2ak 2akak 2ak rx mm-hmm let me see i'm we're gonna say a map m we're gonna print l this is just the first one map m print l then uh put stir okay two map m map m print hello map m hey mad flash what's up again they are all and they're all conjunctivists okay to activate rx i need to activate this one to activate this one i need to to activate all of these okay to activate all of these i need to activate all of these at the same time let's keep going map i don't see when do i get to something that's okay these are all flip flops all right now i need to map m so all of these flip flops are send a message at the same time then it activates okay why do you not write types it's just like funk and tint uh i mean i do it sometimes right but the power of haskell is that it can actually infer a lot of it for me okay we are almost there we're gonna keep looking at this in a bit i'm gonna take a break now i have a meeting but so what we have to figure out is a how many button presses will it take for all of these to be on at the same time i think that's a 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 all right we're back okay 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 let's see a let's just say a so t a a is two act act two act act module to module and we're gonna do concat map okay and so we are gonna say here iterate two act act take one iterate two act act two act rx and then for the input we are just going to say um do l so uh map map m l m's do map m print m s what's the l n dot part two dot lines so we iterate it then we were just basically getting the same so then we get 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 and so now let's copy this one again so the go again from this one mm-hmm okay and so now let's no matching or electric or selector memory yeah maybe i need to do three right okay and so now i'm just gonna say here okay and now i'm gonna just map map m id and all of these um so we have here blop is equal to all of these now go is in here we're not gonna stop here we are just gonna loop right away in mm-hmm you put this button press down into the where okay here we are gonna so we're not gonna say rxh let's see and we want this skip so so you're not gonna do anything here but let me np i'm gonna write this as and then we're just gonna say here skip and otherwise i'm not gonna need these 121 something's wrong okay so now i'm going to say here a um flop on on and the flop goes on and the flop goes on when it receives a yeah so flop okay so ph p8 did pit is equal to flop okay so this is a let's make this like this a flop mm-hmm it's two given vibes okay so i'm gonna now map a map go in its date um and then one i 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 press is required for each flop to be on and i think i take the greatest common multiple here mm-hmm i think that was from day three or something get grab gzm which day was it not day two day three day two maybe nope was it day four maybe it was day five almanac transform all seats nope day six nope let me say kid grab folder a kid grab folder one lcm okay it was day eight yeah okay so let me see here um boulder one lcm day 20 so saying after 2048 presses maybe i need to take the product your answer is too high okay it's not the product maybe i did too many concats let's see and maybe i shouldn't do flops here okay so this is the first level okay and now we're gonna map to ag-ag maybe i'm missing the map map to ag-ag okay map map map m-i-d map go one is equal to go unit state one button press now let's um map map map so our 2048 cycles this one will be active and every 2048 cycles this one will be active flops let me see bf flops and we remove one level here and we remove one level here i think so bf flops this is right before the flops let me see so then is map map map go one bf flops took two number presses before they were all pressed and see bf flops uh but that one needs to be high it's not enough to go one a hl 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 mm-hmm map go go one here needs to be false okay i need to so hl and flop oh hl flop hl flop map map go one true button press hl okay yeah i need to swap it here also so here we want a low activation here we want a true okay so flip flops if so the flip if it was off 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 mm-hmm i'm not quite buying this let me see bff then if it's similar because here it is and okay so how long does it take for it to send a high flop that is the question okay this is how long it takes for it to send a high flop so before the flops how long does it take for it to send a low low flop because then all of them have to be active 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 also conkat map a conkat map some conkat map let me see here conkat map okay it doesn't want that i think this might be the answer seems so low though but that's too low okay and let's abandon this do we have the sum here again so that's the answer to that one maybe for these ones it's actually the product so i want all of these to send a high signal so that mm-hmm bff map mid bff flops map go on holds bff i think it's probably then the product of those mm-hmm all right we got day 20 part two whoop whoop took some guess work but let me see okay so this is a sum and then this was a product it's actually a product of all of these so the idea is okay so this is the number of so for the flops you have to check and because they flip on and off right so then every 3851 cycles they've all they're 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 still is get it because you commit um day 20 finished because there is never getting something example um get add dot dot day 70 example um you commit and forgot um um did you do did push okay uh so we did day 20 part one we did day 20 part two takes a while we have now 40 stars on the effort of code and 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 at 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 do something in january but anyway it's been super nice to uh it's been fun to do the editor code it's been nice to have some company for the editor code and uh yeah thanks for tuning in this year and uh 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 bye bye