 All right We're back again. It's day 10 of the advent of code We've done nine days so far, right? That's not bad Not bad at all We're stringer early streaming early today. I've got some visitors coming in But ah little Annie is back Whoo-hoo As you can see my T-shirt is a bit see through today, but it's great. It's supposed to be a Christmas t-shirt, but Now I just blend in a bit more All right, let's see what we're gonna do for Day 10. Okay. Yeah, we were on some bridge. We wanted the ropes. They all fell. Okay Back up river. Okay, but the route finish crossing bridge if you okay So we got the communication device pull it out Okay, the vice is video system. This is some kind of catholic rate to That's green and simple CPU. They're both driven by precise clock circuit the clock so you could take that to constant rate. Oh My god, we're gonna be making another in code machine Damn Okay, the CPU has a single register X which starts at the value one It supports only two instructions at XP takes two cycles to complete after two cycles The extra registers increased by the value V No op takes a one cycle to complete It has no other effect see we use these instruction in pros and input to somehow Okay, let's just start by a writing the parser, right? Okay, new file example, okay Paste it save it load it Module main where are you main? Okay, and we're gonna have a data instruction pulse Add X Okay, now a let's put the right language here language you see 20 one and Let's write the instance read Insta where a Reads preq. Let's import import They don't know this is tax dot parser combinators Reads read preq. This one is gonna be read P to preq Okay, so read preq to P Preq to P Okay, let's see. We're not gonna use that read preq now. We're not ignoring the text and parser combinators It's I think it's just the same. Oh, okay, that's it Let's just lift it reads preq. Yeah, okay, so we're gonna say read preq to be of lift parts to P import text P Where pars equals so we're parsing one instruction do okay, so That's right. It's gonna be a choice of a Parts no up cars. No up. It's just a string No up cars a add X it's gonna be a string no off and Then skip spaces and then we are gonna say a Read read as to P So where's his reads function from text on read a reads preq Right, this is actually gonna be read with these reads Hey We have to return up and then here we say Add X over and then read and now it doesn't like this a What is read this track here? This one works, okay GT and let's see the day 10 GT oh Okay, it actually turns now let's do right show here deriving Oh Into where read input is just Map Map read Over F map map read dot lines over Read file like this. It's not gonna go to any It's gonna go to IO of instra Really that read no pars Let's look at the Parts no up. Oh, this one is supposed to be addicts. All right. We got the instructions Into usable format. It was quite fast, right now. We have to write the virtual machine, right? And I figure like tomorrow. We're gonna be adding more stuff to this. Okay, let's see. No up takes one cycle xv takes one. Okay, so Cycles instra to add X is two cycles and Cycles Okay, these are the cycles for the instructions and we read them, okay Now let's you find the virtual machine data VM It's gonna be some state right state state Okay, Monad state so what is the state? Okay, let's say here type Type VM, okay, let's say data VM State Is equal to okay, and we definitely need to keep track of the current cycle BMS Curse cycle And so the current value of the X register and let's say registers Is a map from Char, it's map from char to value and let's just say it's an int, okay and Now we have to import data.map Okay, now VM state. Okay, then we're gonna say Type a VM This is the VM monad. It's gonna be a monad state Monad state class. Okay, let's just Call it the monad. It's the state T Uh, okay, so the monad state here VM. This is just a state monad of VM state This is from control monad at state. Let's qualify this Qualified And then we see here Because we want to be using it. Okay, now Uh At the start of the first cycle now, please The initial state Initial state VMS Use a VM state. The initial state is equal to VMS Curse cycle equals one Registers equals It's gonna be a map dot from list X and Okay, um VM state is really small Okay, now let's write the execute function Execute takes a list of instructions. Let's see what we have to do Maybe less than the value of x drag is already going out to execute you For now consider the consider the regal strength the cycle number multiplied by the values of x during the 20th cycle and there's 40 cycle after that I'm wondering if we should like manually just do like a cycle and then you halt for a little time or What we should do Okay, let's just uh, so execute Is gonna take a list of instructions Let's say here a task one List of instructions to a list of uh, we're gonna have a list of Time values and current Exes okay task one And we are gonna say that this is like the This is gonna be like it's gonna return this Can I just say here type is a VM monad If we do return No, it doesn't work like that, right type state the state Termite by type as of the current function list is they don't change well Okay, but why can't I I would thought that this one was Okay, so this one is actually gonna be it's not gonna be the type. So yeah, okay It's the state here. Let's say task one. This is gonna be state We're gonna actually have the vm state and we're gonna have the kind of the The values here Um, and it's going to return The uh end comma it eventually Okay, now it says Return Expected a Actual maybe it was okay before type Okay, yeah, let's actually do it like that. So just to say vm You're trying to list if it's going Okay, now what are we gonna do? We are going to say a so xx state initial value of the final state run state uh return Value and final state. Yeah Okay Um, okay, so I just want to just get the state and we get the run state. Okay, uh, so task one No, okay, so xx let's write that a It's just inster And it gets the a so it's inster and a and it takes a Vm a to vm a Exek No op So I take no op st is equal to um Uh, and we say modify Because I don't actually return anything. So it's gonna be modify And let's go for the overloaded record dot here Language Overloaded record and that's the other one, right? overloaded record update Resume your journey No, this is the new thing all our record update record syntax and updates Works, but it's hearing the dot update expression to one of the functions. I feel and get feel a Let's just see. Okay. Let's just not do it. Um Then let's say, uh, okay, so this one is do and yet so, uh, uh, we have a Vms Dot dot Yes We get the current state and then we do set Vms Dot dot, uh, but Cur cycle equals car cycle And it doesn't like this because You know, it doesn't like it because I thought record one because it wasn't gg 21. Okay, so we get it and then we set it Uh, oh Exek, okay now add x of I don't need the state here actually get and Okay, but I can actually just write a modify So it's actually Yeah, okay, let's just write a modify function, right Vms Only modify it. We say Vms goes to Vms Cur cycle Exek add xv equals Modify and we go from Vms to Vms Cur cycle equals cur cycle plus two And And then the registers is going to be equal to modify so map dot motor map dot Adjust Adjust Adjust map just adjust Uh Plus v we're gonna do for x of Registers Okay, so task one instars. Okay, this is gonna be So we run one instar We can't do map M. So let's see. So task one. Okay for the if we don't have any instruction, we just return empty list Task one of uh, we have an instar Instars equals okay do So we exec The instar and then We do Vms yet And we actually say that And then we recurse and we say uh, we so we add to this one We say, uh, we say that it's the cur cycle And uh, it's registers bang bang x x and this is Going to be f mapped over task one of Instars. Okay, let's just do like this Rest Instars Return Here is it Registers good match Type in trap map This is a cur cycle Isn't it like I still think it should be we should be able to Do like this That's one instars Okay, now we just have to figure out what's wrong here a registers as Okay, uh, let V equals we just get the type straight here V has type int comma int. We need to say V here Okay now, uh, return Okay, so task one Let's write this as observe A Instar So this one let's call this one observe and we execute the instruction and then we kind of Execute observe the rest of the ones. Okay Observe, okay, and then we want for task one We take the instructions And we get the list of income in then let's just say that for now task one is equal to Observe So you get the instars Instars Okay, we observe instars And So this is going to be a VM operation now and then we got to go xx state, right? xx state xx state and then we uh runs no eval state. Yeah We do eval state And whatever this is and the initial state is initial state Initial state. Let's see what task one gives you task one For the first one then we get the So that was first the first the signal was zero zero Okay, let's actually I feel like we should be doing like initial But okay Let's keep it like that. Okay. So two one four four six minus one. Okay That's how it went, right? So it went from Then we have example large So, you know, we could also just say off three no option to roll We just bump up the signal, but I feel like we're going to be getting a lot more instructions. So let's just say example large Okay, and now let's run it for example large Okay, so then we get the It's still used to work, right? Okay, then and during the 20th signal cycle the registrar's value x at the start the first second no The first cycle x is one After the first cycle the novice recognition So then at x3 During the third cycle is the one during the start of the fourth cycle. I might be against execution During the fourth cycle x is still for Okay, a So the start of the first cycle During the first cycle x is one after the first cycle novice I'm trying to do nothing Start at the second cycle two Yeah, okay. So at the start of the second cycle two And then it's still one, right? During the third cycle x is the one after the third cycle the other x is the finish x is x and x is the fourth Our start of the fourth cycle. So we start always starting And during it still and then it's not until the next one we hit when it's actually been updated Okay, then let's see So on the 20th cycle a Okay, so during the 20th cycle. So that's gonna be So that's gonna be like the the first value. That's a No, the last value that's Less than or equal to 20, right Let's see So these are the values and then we're gonna say take while okay So because it on 20 so when it's 20 starts it's still 21 Yeah And then when 60 starts it's 19 yes 100 it's 18 Yeah, okay, so it's doing it correctly. Okay, but we have to figure out this Let's see that. Okay, let's get the interesting interest interest Takes in the list of int comma int Okay, so it's gonna be It's good. It's gonna take a list of Ah, just a list of ints uh interests Inter Estes interest Interests Okay, we start with the It's gonna be interests Let's start on zero and then we uh, and then we so we take So every every 40 cycles right so 20 and then every 40 cycles Okay, so interests. I'm really bad at writing this for interests zero where interests Oh, we start with 20 interest and equals So let's say here. This is gonna be span We're gonna say that like this is the cycle and the signal And We don't care about the signal here actually so span so we're gonna take a we're gonna say a c Less than equal to a c is less than or equal to Less than Yeah less than or equal to n Interests span is c is less than or equal to n And it starts at 20 and yes, we're always safe the first time of a Vals, okay, so span Let's say a okay. This is gonna be where a Before and after okay equal span of valves Now we are going to uh, so uh, this is gonna take a while I'll go while it's And then we're going to say a So the interesting valve is going to be the head of the list reverse bf Okay, so we're going to say i and then column a interests prime On a n plus 40 af Interests 20 What is what is I'm just gonna call this f. Okay F and what's wrong with f here? Uh, it's not f What's wrong with f prime here? Pretty much type into a number of the types are wrong So it's gonna be it's Let's Let's call these signals. Okay I'm better at writing that word signals prime This is gonna be f prime pinch to signals signals prime has type into income i to List of inch That's the problem It's gonna be the cycle, but we don't care about that. It's gonna be the value of the signal here. Okay Now let's print task one. Okay. So task one You're gonna eval state initial state and we're gonna take the Signals of that. Okay. Let's see if this works and No one accepts the pattern All right, and we need some base case. Yes signal We don't care and an empty list is gonna be 21 19 18 21 16 18 17 So 21 19 18 21 16 18 During the 20 200 to 20 is like no, so what is okay? And then it goes up to it ends up being I can see But it ends up being a Why do we have that 17 at the end? So that's the final one is 70 Because we take while and then we do the last one Let's do c here Okay, and let's say case c is Larger than that is equal to n if c is equal to n so I want to kind of In that final case, I want to say, what do I do in the final case here? So the final one is basically If c Is less than equal to Larger than equal to n Yeah, so so how do I say that because it might like start an instruction and then be like a really long instruction um And so the final one doesn't say okay, but to 20 uh, okay So yeah, because I because I think this is wrong right because I I mean for this one, it's okay because I know the last one will not be exactly But I could have like a really long instruction that takes Like more than 40 cycles, right and then the value at that point until it finishes will still be 241 um Okay, but here we also have that's gonna be everything Uh, let's see if that works. It probably won't work. But let's see Let's not have it here. Let's have it because of the danger is that the final one is like exactly the one Uh, okay, but no we can't do better. We can't do better So the final value, uh, so at the value at the value at cycle 260 is actually Still this right because if we just keep it running Right, but we're not we only want to mention measure If it didn't stop Okay, let's uh, have this be Right this has I should be smart about it like afterwards Okay, and now let's a Maybe we'll pay for it later, but Let me just get the input. Let me see what the input ends up in like whether the final one is kind of the same case or what? That's only 110, okay It's not that hard. Okay, so short the input is yeah, okay, but it's the same So let's keep this and let's just say So the product of the example Part of the example large is four three four six seven ah some uh, okay No, okay. Yeah, I have to During the 60th cycle register x has the value 19 So the signal strength is 16 times Okay, so I have to take the I actually have to take the uh No, we do have to look at these. Okay, the second here Okay And now let me do Some trick here and let's do a map and let's set this with list 20 40 What is it? What is the problem here? 20 and then 1 2 3 that doesn't work. It works, right? 2 4 I feel like I could uh do the step Let's go List in enum from Okay, maybe I can't do it that way Enum from then Okay, maybe there's not a comma here. Let me infinite. Okay. So we just have to do so 20 40 80 120 No, it's 20 and then it's 60 and then 20 60 100. Yep 20 60 100 140 180 220 And what if I don't do the in it here now we get 260 so I'm gonna keep the in it. I know it's a Now let's say map s and d Oh Before we do the sip And now let's do a sip with multiplication And then we do 420 111 40 and then okay, then we do the sum of this this here So it's one three one 40. Uh, let's see this 14 80 60 All right, we've got task one Let's go I hope part two is not too bad Seems like the x-ray control the horizontal position of a sprite specifically as by the jubilee So why are these we should set the person but in the middle of that sprite Sprite is depicted. This is the moment. It's not setting up a position Like the cp the crt is closely tied to the clock circuit the crt draws A single pixel during each cycle representing each pixel of the screen as a star as a hashtag Here are the cycles during the pitch towards the pitch and rise first and last pixel in each row are drawn So by carefully timing the cpu instruction the crt drawing operations, you be able to determine Whether the sprite is visible The instant each pixel is drawn the sprite is positioned such as one of its three pixels Render the image given by a program what eight capital letters appear on your crt? Can you see anything here? I can't Is there a In this let's see if we get something from the input But we should be getting this from the Larger example, okay sprite position Okay, start cycle one rigging execution add x during cycle one CRT draws pixel in position one. Okay precision zero. Yeah It's position zero This is one position two position three Wait, so when does it draw CRT draws a single pixel drinks everything each because Okay, so we do one to 40 and then 41 to 80 81 to 128 Okay Cycle 240 we end up at cycle 240 How do we Okay, anyway, I think we so let's start with task two. We at least do got the task to a go in stir in in stir in e in stir to Right, so this is we don't have to actually look at the instructions, right? Because you already know how to run them the task to equals this Observe, okay, so these are like all the value changes Okay controls a horizontal position especially the three picks of white And the extra I get your sets a horizontal position of the middle of that sprite Ah Okay, so that's why So it starts off at one and then we draw like Hashtag. Okay. That's why it starts off like this Hey, okay, so it's always like drawing and then we're like moving the stride during drawing Okay Now, uh, let's split this up into so it's the cycle zero to 40 Then cycle 41 to 80. Okay, so it's like always at 20 40 cycles of 40s Okay, so let's uh re-evaluate the signals here Let's uh Signals Now we're gonna start at zero Oh, we're just gonna do So zero is we're gonna get the signals from zero to 40 Okay Signals, okay, let's just actually have the signals I'm watching here Okay, and then this one just becomes Signals Signals needed Uh, and now we're gonna go signals zero So we go zero 40 So we go zero to 40 Okay, now we need to do something for zero Oh, it doesn't actually start at zero. It starts at one. I'm sorry doesn't work still because the first one is Okay No, okay. This is not the same function at all. Sorry Uh, so between so between Takes an int and a Incomite Between And equals between And then the end is this is like a breath link between prime of zero between prime X is equal to Of of valves between to go up Vowels, okay between so if it's empty It's empty If however, it's not empty if it says between prime And so this is the current value c Vowels equals take So you're gonna go span bf af equals span And this is gonna say This is gonna be curve Okay, so this is cycle and value. So click on whatever goes to so C is less than equal to curve. So it's like the Uh She is less than equal to curve plus and this is going to be bf bf and then a between between prime between prime of a between and then prime and then we have curve plus and Uh Will this like hit an infinite thing or not what takes until it ends. Yeah. Yeah Okay, and then a f And then this it goes into good complaints because It takes a list of instant chunks it between 40 No, and this is Map Okay, so these are the changes until 40 And then the changes between 41 and 80 and these are the changes between 81 and 120 and then These are the changes between cycle 201 I don't get starting so now we have to we have to Okay, let's just now let's write the draw function. Okay And then we'll just chunk it up at 40. Okay. Uh, now let's write the draw function. Okay The draw That's actually Let's not do it like this. We don't have that between the draw. It just takes a list of int comma int and returns string So draw This is the curve. Okay. This is the current value And we're just going to go curve mod 40. Okay current value And then this is the empty list Is just So draw now draw Cur current value on top cycle value Like cycle when the next change will happen And we start with zero and one will add zero and one to this Draw function pixel. Let's call this pixel. I mean I actually care about here Rest Now this is the current value of current value. Yeah now Let's call then we have to have the current value as well CV No, let's go Cycle and then this is up the a Yes, cd Okay now Let's see. So we're with the current pixel Curp p. Okay And we want to see so we're drawing the current pixel. Okay um Now we take like the Where okay, let's say where Screen pause is equal to current p modulus 40 Okay now So that's the screen position And then we're gonna say To put Is equal so if If a abs. So it's the difference between screen pause and a the Current the grid and the current value is a So if it's like at the current value or it's One to the left. So if this is less than two Then we draw hashtag Else dot Okay, and so they do Okay, uh, uh, so let's see here. Uh, wait, let's let's be so this is not actually current value. So this is gonna be CV prime CV prime is equal to uh if Curp p less than equal to If let your curp be So if we're if if we hit if we're hitting the update Then we do if curp equals cycle Then Upd else cv Okay, now and now we check the screen plus and the current value prime and language. Okay uh cycle Curp p less than upd else, okay And then a nvs Is equal to if Curp be equals Let's call this cv Here Yes, a cv prime. Yes prime is equal to if curp p. So if we're hitting the update Then uh, this is uh Then it's new values upd and this is rest Else it's cv Yes, and the output Is that And then so this is going to be output concatenated with draw cv prime curp p plus one Okay, and then the uh, we will have the current value plus one And we have cv prime nvs prime just complaining draw My three arguments, but it's uh, okay here so You have the current pixel and the current value current pixel uh current pixel current value and then the list And this is uh output So i'm not entirely sure that i'll get the uh That i'll get the uh Output let's actually make it Okay, so if then Okay Okay, and we also want to add a A kind of line breaks if screen pass Is equal to 40 you have output we have uh next is equal to uh If screen pass Equal zero if screen so this is going to be Output and then if screen pass zero then dash slash and Okay, let me now see read and put Okay And then we're gonna say put string ellen we're gonna say task two and we're gonna say a putster ellen Of draw and we're gonna throw the current pixel zero current value one instructions say yeah We do get the same picture Oh, we seem to have like one in the beginning Seems like we're off by one maybe Oh, I think we're off by one a new follower All right, verano's Is the best and No, this one's completely wrong doom doom doom doom I feel like i'm a i was more correct before Maybe it should be screen pass equals one and then we do it I think it's definitely zero Maybe I shouldn't start. Maybe I should start at I'm not starting at one here. I feel like I mean it's the same picture almost except I think maybe I should not Up so I should not update the cycle Uh, maybe I should know I probably should check cb here So it's going two and three and Let's actually just I want to see what it says for the input the pictures don't match So like what is happening here right like why is this thing I think starting at one is correct So it's like hashtags dot dot But then it goes like uh, you know like what's going on here. This is something about the The During cycle, okay crt draws pixel in position zero. Okay begin executing at x15 The girl precision zero. Okay. Maybe it's supposed to be cycle minus one. Yes, okay because the current Pixel was shifted by one Okay, so now we get something that looks like letters, right But the letters at the end are still wrong. So Let me see this one should be Rock begin drawing at zero. So it says c set to g Z e h u r k What eight capital letters with g said e h u r k so and the first one are Ah, okay. So all of these Should actually be here Now how do I What do we ship? So we are we are actually We are shifting one to early Yes r g said e h u r k r g said e h u r k all right Just found some nice blockcharts Ah so Apparently you can compile check for a lot of our subscriptions That is exactly correct Verano's We have a We have a lot of This is print now. Let's go to the background This is why we have the stream, you know We get we get extremely good feedback Oh, okay. All right But what if I just have space actually Okay, now, but this is kind of more like a All right, thank crazy wolf, okay All right Ah space looks nice Crazy wolf is the one giving us the He's the stylist of the channel Uh, yeah, this is quite readable, right? Yeah, this whole hashtag thing was Not the best But I wish we could make them like full width. Let me see full width Unicode block to do to do to do to do to do for This one maybe Because see this one is like half Yeah We're almost there, but I still like I would not like I don't want any space between uh Unused space with my editor swimming it, but you're right Nice looks good. See we have official Uh, we have official confirmation from the Unicode box our channel. We have official confirmation from the stylist Ah, you think okay, but anyway now we have the draw function And it it works, right? Hopefully so So we've been working with this signal processor, right? And now we've been uh now we've been working with it Two days in a row a couple days in a row, right? So I think what we're gonna end up with You know what I would have done though. I would have enjoyed Something more meaningful here in the example I can't see anything from this picture Um Okay, uh, so we're because now we have the cycle counter. We had the reading book now We have the VM state and we did a nice parse modify exec observe We observe changes, right? I think that's also one of the tricks We're just observing changes in the state Because those are actually the ones that matter. We could have also just Kind of drawn while we were executing, but yeah, this seems fine All right, let's add this get day 10.hs Input example star star Get status get commit em day 10 Get push this is all available on github. How long does it take to solve one advent of code? A Angular developer good question. So like I've been recording the streams, right? Um, and I put them up on a youtube playlist and We can actually just have a youtube treat. Love Let's have a look at the channel Playlists I don't know code 22 You that the growth has plenty of fruit too much already I Yeah, see also Yeah, day nine is not actually out yet But oh, I will be out in like half an hour Because I have to delay it by 24 hours. So we see here that it's a you know video one Is like half an hour right and then it's like half an hour half an hour And you know and then an hour and then an hour and then 40 minutes and then two hours and then an hour And then two hours yesterday night and today we were like an hour and a half, right? so It it's really different. It's really it's like sometimes it's just really hard to parse the problem to get working on it And then the solution is easy sometimes you just have to work and then um Sometimes Yeah, you just get stuck, right? So you're like doing something like that was like the two hour one yesterday we were We had the right solution for the longest time, but what happened was that We uh, we took a We took a long time to convince ourselves that it was actually And then if we ran it we got the right answer where we didn't run it We just kind of yeah So yeah, I would say like between half an hour an hour and a half two hours if you're not chatting if you're not like Constantly monologuing about it. Maybe it takes uh I think it takes not as long. It's also fun to be able to Talk about what you're doing. Now, you know some interaction. All right, but that was day 10. Hey I got some visitors coming in so I will leave you Yeah, I'm gonna leave you now get ready for visitors Tomorrow will not be as early We're gonna stream at like Six o'clock european time Because yeah, I have visitors they come now they leave Set everything back up. Boom Anyway, thanks for tuning in today Hey I have a feeling like it's gonna get harder and harder and harder Now we're at the point where it's like, okay, we can do it in an hour But then you know, we usually like day 14 or 15. It's like boom extremely hard And here we got away with kind of not modifying the state too much But like, you know, we did do like a map of registers because I know they're gonna add more registers That's what's gonna happen tomorrow That's always how it happens Anyway, thanks again for tuning in And hope to see you tomorrow, right? ask questions Why do we do it this way in Haskell? What is the intuitions? I try to Give you some hints, but I hope it's uh, I hope it's enjoyable. All right Thanks a lot and yeah, hope to see you tomorrow. Bye. Bye