 All right Welcome to day 5 of advent of code in Haskell We're doing We're helping the Elms. I don't know what we're gonna help them with but they need help We're here for them Running in Haskell The best programming language What are you gonna say about it? Anyway? Huh little Lanny is here my biggest friend. I'm gonna make the Chat text a bit bigger. Huh? Hi little Lanny The Lanny was not there yesterday, but I think I know where she was anyway Let's get started with day 5 It's a Monday and do you know why Garfield hated Mondays? Garfield hated Mondays because he was a cat okay, and his owner Would leave like he'd be home all weekend and then on Mondays he would leave so Garfield Was sad because his owner left We all put it as a I also don't like Mondays, but Isn't it just because we miss our pets or we miss our partners I Think so See you're not only here for the coding. Anyway day 5 supply stacks Okay, we're doing final supply smart crates Giant cargo crane Let's see You also want to interrupt the crane operator during this like you do, okay? The sorry crates will be at the top of each Okay, so you have three these other three stacks and then we move Stack okay, so this is like I said and then N Stack took contains MC and D And then this is P Then there you are you were just given you said the procedure when your Chris most like do a different stack In the first step with the abrasion of one crates move from stack to the one resulting in this configuration all right in The second step because I want to one stack to one three crates are moved one at a time, uh-huh So the first great to be moved ends up below the second third. Yes Then both crates are moved one stack to the stack one Okay, so they moved here. Yeah, we just got reverse the lists the Else just need to know which crate will end up on top of each stack In this example the top crates are C and stack one I'm in stack two and Z and stack three So you should combine these together and give the LC message C and Z I'm just gonna look at the puzzle input and see how it but Adam to code is being okay So we have nine here And then it's always move and then digit Okay So this is gonna be a bunch of parsing Okay Let's get started K5 new file Example exemplar You paste the example Now we gotta parse this so we're gonna take Basically gonna take until we hit like space and one Right So that's gonna be our stacks Or let's just take until we hit an empty line. Okay, so Read input file path Let's just start going into strings. Let's the strings first three is gonna be IO Okay, read input fn equals Let's do what it do because we're gonna be parsing it as well Read file, let's first split it into Lines that Read file and then a span Break return Read file fn Okay, I'm just gonna see what this does Input example Okay, gd5 gt day five Day five and We're on it Copiling linking I mean sub not okay now So we do get the containers and then the number of containers And then the empty line Okay, so we're gonna say Black day cons these are the cons Containers a insters equals Oh, we don't actually want to so break null f return containers Instars Okay, now let's say Type in stroke. Let's say data in stir So what information so there's a This is a pair. So it's gonna be Let's just say II And it's gonna be a triple and it's gonna be num target Let's say source Driving Show Let's just make sure the start jacks. Yes Now let's say instance read Instra where Import Text dot parser Combinators dot repeat Okay, and now this is gonna be read as Reads Preq is read preq, right? read preq And it's gonna be some I it's gonna be like that. Okay, so I'm gonna read p2s of Equal to do let's say undefined What is it complaining about? Let's just check yesterday. It's reads preq. Okay, reads preq So do So first it says String I think text Parts are combinators Hello, I'm pretty slow with this type of problem. What would you recommend to improve? Do you practice a lot? Do you leave code? I actually do not practice at all except I Well, I write programs in my work, right? And then I do add a little code over here. It kind of keeps you Keeps you fresh, right? Okay, let me do skip spaces then we say Munch one is did import data.char Is dig it Doesn't like this parse function because it doesn't read. Yeah, so I do munch one a so this is a num It's read add int. We have to enable language duty to anyone Okay, so Okay, and then we say skip Spaces and then we get the string from and then we skip spaces Skip spaces And then we get a Target no Source do this again skip spaces and then this emits to and then we're gonna say Test and then we're gonna return I Dot dot We had record wild cards. Okay, so this is gonna parse an instruction now Let's say here in stir Insta Let's see what this says. I can no type errors. Yeah We parse the instructions like that. Okay Now I have to parse a box But did I answer your question to isn't yet like so how do you practice like I think also like part of practicing is just Yeah, just Like like these parser combiners, right? Like, you know, I've been writing I wrote them last year wrote them a year before that and Then yesterday, I just like, ah, how did I do it again? I'm like, okay Now I remember right and the next time I do it and like, okay, I know where to look it up I think that's the key data box a equal B and Then this is just a Char Deriving show a Instance read box where Krs equals So do a Now I don't do string like we do char, right? we do a Char We read this so to read a box. We read about char And you do get a C is get P And then we do char and then we do return B B C Get P. Oh, just it's just get actually yeah, I think that I mean that's also how I felt right That's how I decided to do I Don't have code three years ago. Like I was just doing a lot of research. I didn't feel like I'm not doing so I'm not doing enough research, right? But now I feel like I'm doing a lot more research, right? So let's just check the Okay Now I have to do the parsing And this is a bit annoying right because we can't just skip spaces, right? It's like Okay, so how do we parse these and I know how well I know how to parse a container Let's see does the program state that every stack because all the stacks here I do have At least one but this is that I guess it doesn't matter and Okay, let's actually just split it at so this is like one two three Or I wrote the chunks function, right? Where did I write the chunks function? Those days three or something. Huh? Yeah, J3 Okay, and Let's copy the chunks function Okay We're gonna do chunks and then we are going to say a List the list of strings and we're gonna map for All the containers, okay, so let's see what that does Yeah, see and now it kind of separates all the containers into one Okay so Yeah, okay, so let's say a CC's if this is chunk containers, okay, this is chunks for containers Okay And we're turning CCS here now. Let's actually Reverse these Okay, and see and then we will get like the one two. What is the CCS here? Why isn't it? I feel like it's not reversing it. Ah Ah, okay, so containers. Ah, yeah Okay, it's because containers is originally just It's just one string, right? So it's Ah, I didn't do map chunks. Okay, so now we have the So now we have the containers And they're gonna be reversed Okay Someone wants to promote the channel I don't need the promotion But I would have thought because I have like a spam thing. Oh, yeah, we have a discord now So like nobody's on it, but I added it because Last stream zone was like, oh bang discord. Where's the community? Doesn't take that long to make one. Okay So This is gonna be So this is gonna be the base CCS, okay, so base is gonna be the one two three four Okay, and I'm gonna say a Numcs equals length base and we're gonna say Print numcs. Okay, so then we know the number of containers, right? I'm actually gonna say skip spaces. What if I do skips? spaces here as well Is that okay? Yeah, it still works. So I think if I do Keep spaces here as well That's okay. Like it will it will keep Going okay Now this is a list of strings list of list of strings So I'm going to map To the CCS Let's see who go and this is also a nice function read maybe it's in text dot read Import text dot read Okay, so what we're gonna do is that we are gonna say CCS Map read maybe at box. This is a list of list I Think it's map map and then this will be not a list of strings list of maybe box Let's see what happens Okay, just set mp just be just PC and then nothing ah This is exactly what we wanted ding ding ding Okay, and What are we doing now? Now we know the numcs. Yes Okay, so let's build the stacks a Ding ding this is each layer. Okay, so Let's say Stacks equals replicate empty so numcs Okay, then we're gonna print the Print this it doesn't know that there's no type Okay, so it's three stacks Now, okay, we're gonna say this is not gonna be so it's gonna be the Stack bar forest stacks Okay, so it's gonna be Stacks Okay, and now we're gonna say a Hold L. So we're gonna say a To stacks add to stack a And then we have the a So here we're gonna say Hold L. Arrest stacks and then a Add to stack add to stack Takes in a list of list of boxes and A list of maybe box Returns list of list of box Okay, so add to stack her It's all cur and then And then in okay, and it's gonna be add to stacks That prime a And we are good. I like how we haven't even started the problem yet. We're just parsing This is gonna be add to stack prime her in a And then empty where add to stack Empty empty Okay, so I don't care about the second one and Nothing SF equals SF Add to stack a This is gonna be CCS a Just be SF equals Now we have the first stack and we have the second when we have something and Then we're gonna say that's gonna be a CB So we're gonna add a B to the C We're adding this to a We're adding this to so far Okay, add to stack see CCS Nothing SF is equal to Adding just see to Okay, so what I want to say here is that I think there's something probably wrong here add to stack prime a So the curve is this is gonna be a list list of boxes and It's gonna be a list of maybe box and list of list of box and Returns of this list of box And this is gonna be add to stack and we continue Add to stack Prime a Ready to actually Rest of the stuff here Okay, add to stack and then are To stack prime the s are Probably want to do is No Okay, so add to stack a takes in a B to a to B right This is a list of list of bug in a fold L Takes a B to a to B and a B and then a list of a's ah But first stacks is a list of list of maybe boxes, right? Okay, so add to stack B to a to B. So this part stacks is a list of Maybe boxes, right? It's gonna supposed to take a list of a's Oh Type is wrong. Okay. Let's see what this does. Oh, no Okay, and What am I missing here? Okay? I'm probably missing the add to stack How can the number of stacks be empty and the other one not be empty? I was not doing everything. Hey like this Okay, so we get the DNP is the first Stack DNP Listen to the example I got DNP C&M Okay, something is wrong there. Okay We're having some issues with these stacks maybe I have to Reverse them again. I'm gonna print it differently so DNP and C&M. Okay, this is not Where is set Set MP. Okay, that's the bottom layer And then NC and then nothing and D. Okay, a set MP. So I want to I want those in the first Okay, let's try just let's just map over these, right What if I just do I need to import data on the list for that Then I get a set and nothing MCD and P Which is exactly what I want, right? Okay We're gonna leave it at that Return at maybe Dollar transpose Barst seed at insert inserts I feel like my my Completion is broken because I used to be able to do like Import data dot maybe when this happened set and MCD and P Okay, and then we want to First these Okay, we got the stacks correctly. We've got the The Instructions as well Okay, now now let's write the like run in stir Run in stir. So it's gonna take a list of boxes And then in stir and return a list of boxes Run in stir Okay now Okay, I'm gonna I'm gonna actually Do here a sip sip one with this And then do a map dot from list actually Int map port data dot import a data dot int map Okay, so it's gonna be I am dot from list There's nothing this is gonna be an Not a list of boxes. It's actually gonna be an int map list of boxes, okay Just so we can like directly do the You know map the Like updates, right? Okay, so, um These are stacks now We have run in stir Which which let's just call this type stacks equals int map box, okay Now this is gonna be a Run in stir. It's gonna take a stack and it's gonna take an in stir and it's gonna take us Return the stacks Run in stir Okay sts And then this is gonna be an I Can we get all the Okay, so we say a Where okay, so now we're gonna take a num So we're gonna say a we'll split at num a sts I am dot bang A source So this is gonna be a u is gonna be equal to a It's gonna be a source prime No, this is gonna be source prime and this is gonna be Med and this is gonna be target prime is equal to and It's equal to med So we have to reverse and med actually reverse because it's taking in reverse plus a sts I am dot bang target Okay, and now, uh, let's look at int maps And now we can just do set key, right? um Something like that data dot map Uh, data map. No, we don't have to do that. We actually just do, uh, we do like this We say, uh Adjust and then the key so we're doing adjust so we'd have a function and the key is a target And it's a STS so, uh s Stax prime is a Oh my god Stax prime is adjust so we add to the target, but we also have to update the We also have to update the the source so we say, uh insert source Let's say this is actually I am insert source STS Source prime Source prime I'm going to return Stax prime prime Okay, a No, it's complaining here. Okay. No, it's actually right. I guess, uh, insert source Uh flip source prime. This is gonna be Stax now source prime here is a Okay, I am insert all equals flip. I am not insert source What's the type of flip here? Takes in and Ah, maybe I don't need the flip even So I am just in the target Ah, it was desk Okay, so I think this is how you run the instruction we uh So we we adjust so we put the stuff moved onto the destination and then we We insert the edited source Okay, so Let's see here, uh task one takes in a list of stack stacks and the list of insta and returns a list of stacks Yes, so it's actually not like that, but we will read the top ones later Uh sts insters, this is actually gonna be fold l right and run inster sts insters print a Ask one So let's see what the result of running this is cm set ndp And that's actually exactly the one we get here And then we have to now get the first ones of all of these So, um Now we're looking for int map. So I want us to get the values Ah, I can actually put just fold over them, right? And the keys are in ascending order. So that's okay. Okay, so I want to say a I am dot fold firsts and firsts first of a This is equal to uh First it's gonna be first And then I have an initial value. So actually I'm just gonna start with a empty string Uh firsts sf So if I get an empty list, I don't do anything and Firsts and I have something here bc I don't care about the rest. I'm gonna add that character to the sf I'm gonna return me a string actually And actually I have to reverse the string Now what is the problem here? Firsts is gonna take a List of boxes and string return a string And it doesn't want to do that. It's complaining about this pattern It's just because it is not wrapped Now it's saying that they have different arguments sf Now it's saying that Oh, right. I just said fold up. Okay a kinemax type box with a I you should oh set mc. Huh. All right. Yeah, sorry. I Actually already cm said, okay, let's see what it does for the input This was a lot of parts wrangling, right? I mean Once we actually got it into the shape that we wanted It doesn't seem to be that hard Let's actually see if we got it Uh before we start making claims about it I So it worked for the uh, but now, you know This is task one. All right. We're not done yet. We're just Getting started. Maybe it's super easy. I mean Actually, the actual thing we had to do wasn't that hard, right? Once we had the instructions and we had the The data, right? So tgwsmrbp and let's see if this is the actual Correct. All right. We got task one Huh took a long time today But we did take the extra long to parse everything, right? Can I get a woo-hoo in chat? No um, we did take an extra long time to To parse today, but we like wrote proper parses and everything so Let's see. Yeah Let's see what happens. Let me just We got task one now. What's task two? Okay, as you watch, you'll notice someone was coming The side of the crane is quickly gravity. It's in crane number 9000, it's in crane number 9001 Air conditioning, leather seats, and extra couple on the ability to pick up and multiple move multiple crates at once Moving a single stack Crate from stack two to stack one behaves the same as before Our reaction moving three crates from stack one to stack three means that those three moved stay in the same order Resulting this new configuration Okay So then we just uh, we just don't reverse, right? Run insert two and we especially especially had to Reverse it, right? two Cm, huh All right, I have to say Insert two here. Oh, actually, let's do it this way, right here We also say I should be doing code sharing, right like Okay, we get mcd Ah Yeah, so it should be mcd. Yeah, and then we get for the second one Something like this all right See we did so much work in the beginning and Just to parse it right and get it into this thing uh And then the actual tasks were Quite easy All right, but uh, yeah, we've been here an hour They're starting to take longer. Um I'm not sure they're actually harder or if they're just I think the thing is also that they're um Now we're like writing up these parsers, right? So we're kind of doing it in a nice way So maybe we would have been faster if we just kind of split it With this white space here. That was um That took a bit of riling. Anyway, uh, let's see get status. Uh Get add d5.hs input example You made m and d5 Get push Okay, so We pushed this up to get oh, no I added some stuff It was just a change to read me all right, uh we've got the uh We've done it to date today Discord, uh, this is the discord. I'm just gonna pin it And uh, if you guys want to chat later, that's cool um We'll be back tomorrow same time 6 o'clock Okay, hope to see you there But if you want to check it out, this is all on github. You scroll like below the channel. You'll see the link to the github link to the discord everything like that And uh, you can check it out on youtube as well, right? So first videos of the streams Back during youtube Addable monad you were an hour late But uh, yeah We used int maps. We did some parser combinator parsing and uh Yeah, it worked out the hardest part was we tried what we failed in the beginning. We were like trying to add to the snacks Turns out we only needed to transpose. It does the right thing. I was trying to wrangle it too much But after we got the data input than the actual Uh, the actual program was quite easy and then this was super easy. I don't know how this was like the task one task two difference, right? And yeah, we could we could merge these into I mean, this is the same code But that's the thing with that in the code, right? We don't really write. We like we write We write don't really do the whole uh, let's sit down and Clean it up, right? We just um We just do it quick and dirty and I think that's okay for these kinds of problems, right? Like if you were in that interview right You know, you would just solve the problem. They wouldn't be like Okay, now you've done the programming task now spend and Half an hour more cleaning up the code. I mean, you are submitting the code maybe but like If you're just doing it on the whiteboard, this is fine. I think that's how I think of these things like it's practice for these interviews and Yeah, just to keep fresh, you know, it's all about you know If you keep doing more difficult things and I just bumped the white space a bit, uh Yeah, you know, just keep fresh make sure you don't forget how to program, right? Even if you contribute to ghc or Hack on big applications, you're also going to remember at least a tiny challenges, right? And get to use all these fun intermaps and stuff where you know, maybe you don't get to use that that much um Oh Let me check one thing. I want to check if I can do a language Huh Thank you edible monad Overloaded record dot record. This is the latest and greatest expansion I'm just gonna see what happens. I usually do I like this, right? So now I want to say s s stack Okay, and I'm gonna do Wow You see this, huh We're using the record dot. It's like uh, it's like programming 101, but we can do it in house school now Uh, that's pretty cool. We cannot update it though. You need to like Define the has field and update and everything um Can I do a overloaded record update? Overload record update, um Yeah, I mean it only works for like next year. I wish I could like maybe I have to do Can I do deriving has fields? I don't know Anyway, that's it for today. Thanks for tuning in a See you again same time tomorrow six o'clock a European time that's five o'clock utc It's like noon in the u.s So yeah, thanks for today and uh, hope to see you again tomorrow All right, bye-bye