 Here you one, and welcome to day two of the advent of code In Haskell So yesterday we had a lot of fun, right? We We were doing some depth measurements from for Santa so it wasn't code. It was some sliding window stuff And we got two stars which is not bad and And what I liked most is that, you know, we have a lot of active chatters. That was nice, right? And we had and We could like we really dug down into the core, right? We really looked underneath the hood of Of Ghtc in Haskell, right? And we really saw what was happening so Yeah, let's see what happens today Hey, you can see that I've got The calendar open where we left it yesterday. I haven't looked at today's exercise, but I've seen on I've seen on Twitter that there's some parsing involved, so That's gonna be fun All right, let's get to it. So we'll go back to advent of code and And Yeah, we got a new subscriber like One minute ago. Yeah jumbo Jumbo holiday. Good evening. Cool. I called it Yeah, I'm Icelandic It is the best country Right Alright, we got two chairs Hello, okay So, okay. Yeah, so I think the first one was like we were on a submarine and Then We found some depth stuff, okay now We are Let me zoom in a bit more Here we go. Now. Yeah, and now we are Doing the next step, right? So how to pile this thing seems like a submarine can take a serious commands like four to one down Two or up three So forward down or up. Okay, we're going down and upward report. So yeah, we're driving a submarine. Okay Down and up the vector depth and now so they have the opposite result of what you might accept Yeah, okay. Yeah, we would dive deeper. So we go. Yeah, that makes sense Okay, note that since you're on a submarine down. Yeah, okay Somebody's all day of land of course your personal input. You should probably figure out where it's good. For example Forward five down five forward eight up three down eight Forward two Your horizontal position and depth both start at zero the steps above would then modify them as follows Forward five adds five to your horizontal position a total of five down five Okay, after following these instructions you should have a horizontal position of 15 and a depth of 10 Multiplying, yeah Okay, that seems Seems pretty good So let's Get over now. I can see everything I have open. All right, this is from yesterday Let us make a new folder make dear day two and we Open that folder How we did we do control K and no Day two We're gonna open No, this is with that core we were doing yesterday. Okay. Hey Yeah, I have to connect again to the instance. That's fine. All right new file text file Module main where? You may need to make this bigger, right? It should be seen putster Okay, well see it detected it was Haskell But it suggests a Very bad name actually did two dot eight checks. Okay. Hey Yes, we have a bunch of suggestions. Let's just ignore those for now right, so let's Let's see what we did yesterday, right? for the parser and stuff They wanted a jazz What we did was we had this map reading every file, right So I think the first thing to notice today is that We don't actually so we just sum up the forwards, right? And then we go up and down up and down We don't actually need to We don't need the order right we just need to find out yeah, the sum of the forwards and then the sum of the downs and a Minus the sums of the ups, right? So let's say here Let's see GCI Yeah, okay now we're in the wrong territory actually go back They too Okay. Hey, let's make a new file and it's just called example pasted and We do a Example is going to be a list of strings And it's going to be a example is going to be lines over read file Example, okay, let's load this And see what happens they do that. Yes All right example All right, so we got the example Okay, now we're gonna parse it So we could write a proper parser here But that is too much work So we're just gonna say data instruction Okay, it's gonna be up And now yeah up in Well, let's have it in a different order forward int Down int up int Deriving eq or whatever Okay, and now we are going to write a parser that takes a string Instruction a parse Okay parse in the stir So parse in stir so, you know, we could write a proper parser here with like blah blah. Yeah, but a That is just a lot of stuff, right? So what I'm gonna do is I'm gonna just cheat. I am going to say in And say a Take so if it starts with forward Hmm Now actually it's let's do the let's write a proper parser And read we're gonna write a read parser. We're not gonna install parsec 3p Haskell Because they are pretty nice actually the read P It's in base, right? So we don't We don't need to install any packs. I mean, it's a bit slower than these parsecs micro parsec all those but this is Nice, right? So we actually say here import text that parser Combinators that parse know read be right. Yeah, I have some packages installed because I was doing some experiments Hey, Janet in welcome back to the stream He's been with us since day one Right now so read instance Yes mega parsec is better but It means you have to You actually have to It's more I so I like this because you can just say read P2S Which turns it into a reads parser? And that's exactly the instance of read, right? So I can say here reads preck or read preck, right? So we can we can actually do this right Let's see. So this is going to be parser read P of instruction parser equals Okay, and then instance read Instruction where reads preck Don't care about the We don't actually care about the The yeah, we're not gonna care about the presidents. I mean it matters later when you know, we're doing actual health draw stuff But as we know, there's only one instruction per line. We're not gonna care about it. So we are gonna say const and Then read P2S Which is nice read P to Instur parser, okay now we just have to write our Instur parser and then We our you know Instur Goes to file path to list instruction is just Instur's equals map read at Instruction Dot lines over we are clicking to like this fmap this Start read file and now it's complaining that yes, we don't have type applications All right, I Think it was a strong choice for the first Yeah, you're the one that they are using the there's an AOC 2012 Hey little Annie She was here last year as well Even longer than janitin. Oh, so there's a like a a racket thing that allows you to submit AOC 2021 solutions So there's a tweet about it right and if you say no it calls you a coward And I kind of assume will boom and wrote to that but yeah All right, we're drinking chai today, but wait, you know why? Because it has a Cardamom right so it It's a super Christmassy drink And it's a new moving up. It's got moving papa. I like it. It's a different one from yesterday Try to figure out how many moon cups I have from this room. All right back to parser Cardamom is good. No Okay, so Right, how do we write this? So yeah, so we have so we have a choice. Okay. Yeah Yeah, so I always forget how we do this so we have No, we have like a pf It's gonna be this is going to be okay, so two parts. Okay, so we're gonna take the parts one dream Okay, so what we're gonna do is that we're gonna have a Okay Parts one string. Okay, so this is gonna be a choice of a map R's one over Like forward Up and then this one is undefined Okay, a a Masala Jai is good. Okay. Now type of R's one is done one It's gonna be a string to read the instruction Okay, so I forget how we How do we change it again? So I think it's like because it's a monad, right? So I think it's So we let's just do like this we do and we do Let's actually make it a bit clearer import qualified text dot parser company Combinators P Okay, so we do a parse dot string stir and we ignore the output Okay, and Yeah, it's complaining that We're not finished right and then we skip spaces and then we We parse an int right and to parse an int We can we can you can use a reads track so we can do Reads track at int And we just give it some whatever Now this is gonna be read s right so we do read s to read Read s to P Right, it was at read s to break. Okay, so then we Now we do the string again, right? So we do case stir of if we got a forward We're returning forward of num, right if we got up we return up up of num and if we got a Down We return so this one should do something That's actually just Move it like this And these are wrong now. We reload. Okay, parse one All right. Hey, so we also need show here actually show And then we need to reload and then so we want to parse one up five Now actually we want to say read add instruction Up five set x type applications. Nice. See It just works Because we stole the parser from hints. We can just Do whatever what is there a read s to P? Read s to Prak what's called read s To okay, it just does It just does the same Okay, doesn't matter So now we can parse one. So let's say a Instance Example right, sorry Okay, so this is our input Forward down forward Yeah, it makes sense all right, and of course we could have just We could have just manually put it in right but Yeah, this is more fun. Okay. So now we have instructions and what were we going to do right? So we want to sort Import data list Sort the eyes It's coming like this sort eyes Okay, a Look is a group, right? They're not equal, of course hmm Now we need to kind of group them by the So we sorted them right so now we want to grab all the forwards. Let's just do that, right? So Let's define his word because now it's kind of like a Trinary thing right something like that. So is forward is going to take an Instruction and it's going to take As kind of a pool right and is Let's actually do it a different way, right? We just say from forward and we go to maybe in So this one I'm gonna be forward Is equal to just From forward He goes nothing if there's something else Then we copy paste these and we see you know Forward up These and we say up down Yes, up down Okay, and so I mean so what we could do is like we can make sure yeah We only walk the list once blah blah blah, but that doesn't really matter So we're just gonna We're gonna have so we're gonna have the the sum of the forwards is gonna be some dollar map may be from Forward right Yeah, okay import data dot maybe so the sum of the forwards is 15 So, okay, let's just do the problem right so task one is going to take file path And it's gonna return IO hint The task one Equals, okay, so we first Get the instructions so ice is going to be instors Of the FB Okay, then we're going to say a for some Let for some equals Some map maybe from forward Yes, and then we have Up some which is gonna be from Up and then we have down some We'd like to get down some With from down And of course here we are Walking the list three times which is not the best but And we also could make it like a set or whatever but Let's actually do that right. Let's just say it's fours. It's ups and Downs and then we just do the sum later. Okay, so For some it's going to be some of fours and So we add the ups and we subtract it down so it's gonna be With what it what is it called can horizontal Okay, so it's called a horrors Gonna be some of the fours And depth is gonna be the sum of downs minus sum of ups and then we Return a horrors time depth and yeah, we have to import data dot Maybe nice Okay, and this is dip What's wrong with this? There was nothing wrong. Yeah, we don't need this bracket. Okay now Let's load it and let's just call Main is do Task one Example Into print okay, so we do are and then we just run main and we get 150 which is what we expect Okay Exactly, so we get our puzzle input copy paste it Save it Okay, and we are going to say input And let's run it 1670340, let's check if that's correct All right first one done That was too hard And it was very fast, right? We didn't even I mean we didn't even have to wait for it to compute It just kind of happened when I pressed enter Let's see if the so sometimes like you do these things right and that and you make the second one super easy because of how you did the first one But sometimes it's the opposite right you are oh Yeah, okay Based on yeah, so let's hope our our way to doing it Wasn't too bad basic calculations the plan course doesn't seem to make any sense You find the summary manual and discover that the process is actually slightly more complicated In addition to horizontal pressures unit a man and you also need to track turn value aim, which also starts At zero the commands also means something entirely different than your first thought Down increases your aim and up decreases your aim It decreases your depth by your aim multiplied by X Okay, so five Okay, yeah, so down five. Okay, so forward eight Pass eight to your horizontal position a total of 13. Yeah, because your aim is five, right? So you're so we have now we have to now we can't just sort them and do it, right? All right, let's say let's get to it so task two takes in a File path and IO int ask to Fp do is This is the same insters Fp now we're gonna say a Return process Is so the process is going to be it's gonna kind of take in the depth aim and And is okay, so where process and the depth okay, so it's actually depth aim Is okay, so it's actually zero and zero So if we're on the empty list we just return depth times All right, so we need to actually have our depth and aim and for us It all starts at zero and depth and we could multiply that for hours This is just a depth times horrors Okay, we don't do anything there now. Okay process depth aim Okay, if the current instruction is Let's just do the up and down because that only changes the aim So we have up and rest that's equal to process Depth depth minus and aim Okay, so it doesn't actually change the depth right It just changes the aim and let's say minus end here and For down it's gonna be Plus right Let's aim plus now What happened for forward then our depth? That's two changes. Okay, so the horizontal position It's gonna change by and horrors plus and and our Aim stays the same But the depth is gonna be depth Plus aim Times and right so what is it complaining about? process, this is supposed to be an int to int to int to Instructions instruction to it Right. Oh, yeah. Oh, yeah, and I have to Pass the reds here. Exactly. It's not too bad, right? It's just a I mean, we're essentially just writing a very basic right after we brought a parser and Doesn't seem too bad Let's see Now, let's do task two on example That was 900, which is the same as here Let's do task two on the input Very fast again, and we get something. Let's see if it's correct All right That was quick and easy Yeah So I got them both quite fast I don't know what to say. I mean, there were no tricks really Well, this is these were just straightforward And Yeah, I mean so like you need to know about these parsing things But the not really nice thing about Haskell is that you look you have all the monads and applicatives and all that stuff, right? So parsing just it's it's just a monad, right? So you have all these parsing functions, but because the repeat is actually a monad you could just use do right? So we just here we just kind of parse the string Right, and then so as we just so we're we're looking at one string, right? We say parse string We say a skip spaces and then we read the number, right? And okay these reads practice It's actually gonna be Yes, so So actually we can we we don't even need to ignore this, right? We can just say Now okay, we actually need the Presidents there But let me see we can do it here, right? You can't ignore it at this point So we just say like this Okay, and then we ignore the int, but we could probably Kind of parse it here, but we we have only one per line, right? So let's see how fast it is Yeah, okay. No, yes, we do task two on input. Okay now. Let's close it a GCO 3 They do that HS D dump simple. Let's just dump the code right away, right? Yo, what I wanted a D dump file. What did we say against today again? bash history grab D dump D dump to file to file and we need to change the flag so that it actually compiles it right now let's run it again and Let's just see how fast it takes day two Yeah, okay, so 10 milliseconds that's not bad That's not bad at all Look at the file and we generated what happens But so here's all our parsing stuff, right? So we have the forward. It's all like strings Read instruction. Okay. Let's see far as one string So it's it's just checking the EQ strings, right? What's one is this? So this one is read instruction 17. Yeah, exactly. So you can see that a Like the parser turns out to be You know, it's literally just checking like is the is it equal to the right string and then we Okay, so we if we have an empty list we do this otherwise if we have What does this mean like a string concatenated to? It may be like the first character So we end up with like main forward. Okay, and it So it checks right it checks if it's equal to the string And then it gets goes up or forward or down And then it keeps going So I'm guessing that this Kind of is the front of the entire list, but how does it know like the length? I'm not sure about that, right? Okay, so this is the Go go function So this is like the Okay, so this is like after we've kind of broken it up Yeah, so this is the thing right like once you start using these libraries. I Mean it's a bit hard to read all this, right? Will you skip spaces? Yeah, I It's I can't really explain what's going on here. Let's check right What happens If we don't have o3 that just oh right so it doesn't do all the super optimization stuff We have to close this and we open it again Okay, it actually doesn't It actually doesn't change that much It still has this base ecu string And it's just checking that This vs3x is equal to the string. Okay. Yeah, okay. So this is a function that actually grabs the string And then it checks that the string is equal to The thing right so what I don't understand is like parse one Like this is a string like what where where does the parse one get this string from right? So here we have another parse one And here it's still being passed this string. So this one is Okay, wait. So here it might be right. No, this is just making it at the right type. This is just a type Where is this invoked? parse one, um Okay, I guess this is just the top level function. Maybe there's something more here parse one is main equals parse one. Yeah, okay Now let's see But if we kind of at this point we kind of already have the String right it's already kind of split up So this is read instruction 11 That is the original string. Oh, right. Sorry. This is not the string that we split on This is actually the yeah that like that as a forward or off. Yeah, like the original one so This is just the string That's doing this right But this parse string here, that's something else Let's see. Sorry. Yeah, that was Not as mysterious So that's why we just check the string Okay, but how do how does we do the actual thing? Read instruction 13 Um, I think it tries to parse Yeah, okay, I think it just calls some of the read stuff. Yeah, all right Not so much happening today actually not as much as yesterday Um, it's just harder to diagnose There is less going on in chat this People more excited the last yesterday, I guess right it was more of a oh, that would have code is starting Now it's just more of a okay. What is this? Anyway, I think we solved it quite nicely. I mean we kind of avoid doing the iteration here But here we actually need to do the iteration because it matters like in which order we do it Um, and we can probably I could probably do it faster, but like, you know, this took No time at all Uh, so I think we're just gonna be pretty happy with right. Let me actually Add a git here It add day one star dot It just gets status It commit um, and let's throw up the code, right? We might as well Have it up there. Uh Now you'll be able to see all my private repos But that's okay near repository 100 off code Go 2020 is already exist Yes, okay create repository Get remote add Get add github Dot com Days one and two Get push Get push set upstream origin master All right, then the additive code code Oh, wow, it's slow That's not Oh, okay It's not git pub Git remove Here we go Now it's up there And you can check it out I'll add a Let me add a license actually Add file license Choose a license template my t Good stuff Commit new file. No, I didn't want to commit a branch That was a lot of buttons for one file And I want to create doc git ignore And we were going to use Haskell All right, now we committed directly. Yes Good stuff All right, now it's licensed and all Not bad um Yeah, all right So thank you for today. Uh Wasn't sure long this time, but uh will be longer Uh In the coming days, I mean it gets harder and harder This was just I think it was particularly easy because it's so easy to parse in Haskell, right? So Haskell was a good choice for this one. Um, I won't be streaming tomorrow. Uh, I'm getting with it from mom All the way from Norway And uh, yeah, we're gonna go to a amusement park I mean you have to receive, uh Mom, right, you cannot You can't tell they're like, no mom. I got my string So we're gonna do a double feature on saturday instead um Or we do day Three and day four at the same time Hopefully they're not super hard And uh, yeah, and then Thanks little Annie It'll be super nice. All right, and then I'll upload day one To the youtube channel like I did last year, right? So we'll have all of them there Uh, so if people want to you know, check out some Haskell coding It's gonna be good stuff all right Thanks again for today and Catch you saturday I'll I'll put it up on my twitter like tritlow on twitter This is a links like below the channel where you can see uh Yeah, my my twitter and that's where I put all my links and stuff and Tomorrow Uh, let's see time is So I'm gonna I'll I'll probably know on saturday. Yeah next stream. I'll know if my paper got in But it's anywhere on earth on december 3rd Which means that it's not gonna be uh for another 18 hours at least But yeah, thanks for showing up today, uh, and uh See you. Hopefully. Yeah, so just Before we leave, right. Are there any questions? Do you think This went well or Also like feedback on the stream, right? So now I have the christmas music and I think this is a fine setup, right? You know, you can you can see me you can hear me well um I think there was an issue with a vod yesterday actually because I had it set up so that it wouldn't um, it wouldn't Like the christmas music wasn't allowed to be there Right, so I would upload it but without the music channel Which is good if you're streaming, you know copyrighted music um But what I'm listening to is called stream beats And they are like copyright free Lo-fi christmas music that's super nice to have for these streams, right? And they have like a huge election, so Uh, I like them. Uh, they're not sponsoring me I just like them use them for your stream. It's nicer with music, right? All right, thanks again for today and catch you Saturday. All right. Bye