 All right. Welcome to day five of the advent of code 2020 Yeah Nothing new today. I hope the audio is as good as it was yesterday And let's see. Let's eat last yesterday. We helped Santa with his Passport issues but now we have to help him with Something else, I don't know. I haven't opened the problem yet But yeah, let's look it up just close the window and open up Let's see here advent of code Five right Binary boarding. Okay, you oh, no, you dropped my boarding pass Oh, no All the flight attendants are busy with the flood. I thought we were doing it to bargain, but okay We're doing a flight Oh No, this is this big white thing here. Let me fix this, you know, bitch and Yeah, okay Make it bigger. So And Yeah, so we have to fix the binary boarding. Okay quick program use your phone's cameras can all the night right boarding passes Okay Instead of so in the groups is there only uses binary space partition To see people a seat might be specifically applied like F B F B B F Yeah F means front B means back L means left and R means right The first seven characters will either be F or B They specify exactly one of the 128 rows on the plane Numbered zero to 127 each letter tells you which half of a region they give and see this in Start with a whole list of rows the first letter indicates whether the seat is in the front or Oh right Now I remember what he did He took the toboggan to the airport to catch his flight Let me actually we actually fix fix one thing is Yeah But let's see So we have to partition them So we could consider the whole range then we go the lower half beam and see upper half lower half upper half and then Left and R means right Okay, so we look at the first seven characters. So those are these characters So that's how we determine the so we just binary search The seating space to find the row And then Okay So the last three characters will be other L L or R exactly one of the eight columns on the plane The same process as above proceeds again This time with only three steps L means keeps a lower half R means to keep the upper half so consider just the last three characters of F B F R so So consider the whole range zero through seven R means take the upper half keeping columns four to seven Let me see the roll in a half So okay, we take the upper half take the lower half and we take the R so we get five Okay, so the first thing we see here is that this is very much just binary So F B F F So we're always just partitioning the range Right, so Let's see here. So, okay, so we recode. Okay, so column five We have a seed ID. Okay, so we get the idea from this So we have some other boarding passes, so okay, let's let's look at the test input. Let's create day five An open day five we get a new file test So this is this one and then we have these seeds here And then the final one Okay, and now let's open up day five dot hs And let's see let's see what happens Mod do Okay, so the input it's going to be You want the classic get input function Read File path IO list of strings Get input It's just fmap lines over file Okay, and main equals do main I'll Get input input And to print And now we run this day five Uh So we run ghc day five out it just we want out to be day five and We run day five to get the Starting input These are putting okay. We got the input. So let us look at The first input here So, okay Uh, let's see here fbb fr. Okay So I have suspicions about this right so rlr Uh, so rlr is actually giving us five right But we know that R l r so if you write it in binary This will be Four plus one equals five So we just convert it to binary to bits Second, uh, okay, so this is supposed to be return 44 So I think b is going to be one so zero one zero one one zero zero This is uh what this is So it's two so it's four plus eight not plus 16 plus 32 Which is 44 so We just turn we split it at the seventh letter and and turn it into binary and then we convert that into Integers I think we have some asco stuff in data.bits Let's go Okay, um So how do we how do we go from bit to Let me see Let me see. Okay, so bit i is available with high spit set and all a bit clearer so How do I set the bits? Let's see we're gonna import data dot bits that's for sure import data dot bits data dot bits and then we are going to go For each bit we are going to so we just want to convert these strings so string Uh, two bits, so this is gonna take a string And kind of carry a pair of it's Okay, two bits equal. So we're gonna first, uh, where split at We're gonna split it at the seventh member Let me see Copy this and you say let s equal So split at seven s Yes, exactly. So we say split at 7s. So this is gonna be row comma column Um, so we split that so then we get the rows and columns And then we just want to map that to bits Okay, how do we how do we just we just want to make finite It's I just count trailing to um To integer Okay, can we just convert bits to integers? Let me see Okay, so we want to for the row We want to sip so we're gonna so we're gonna say sip We're gonna sip it with this fbf pbf So we're gonna sip so we're just gonna sip with the index sis Hey freddyk Yeah, we gotta have more Haskell content, right? They say we're lazy, but that's just our programming Yeah, um Okay, let's see I think I'm gonna change the color scheme. I think you guys can't really see the chat better Thank you. Yeah, it's Icelandic used to look Danish But then I got these blue keys and I replaced it. See this is the other one because I have two um So I just like added these blue keys To make it less Danish. I'm sorry fredrik You can get this Danish by default worked for you. I guess anyway, uh, we're gonna sip uh Let's just let's just write the function To int It's gonna take this string and it's gonna return us an int Let's we're gonna use it here. So we're gonna say two int Of a stir. So we're gonna apply this to two int stir and we're gonna apply it to row and column And we are going to say So I I'm gonna say I'm gonna I'm gonna make a zero. I think I think that's how I do it zero bits. Yeah And I don't I've never used this data dot bits before so I'm not quite sure zero bits So zero bits as int Okay, cool. So I will just go through the bits And I will just set each bit um So I am going to I create the list of pairs right sip a sip zero And then string Okay, I'm actually gonna reverse the string and what I'm going to do then Is I am going to I'm going to fold default Defold our Oh, no Full l. So I want to say yeah, this is the set bit function. So I want to fold l So fold l So, okay, so it's going to start off with the zero bits And we're going to map it over the list here We're going to map it over this sip And the function here should be Okay, where f so okay, so here I have the index And then I have the so if I have it If I have f no if I have b so the the b's here are gonna be the ones right So if I have b then it is set a bit I If it's something else then it is just it And this is gonna be f here And then I want to map to intro and to its column Uh, right here we We uh into chart into into actual type Oh, yeah, okay, this is this is this has to be A and okay, let's write b so set bit I Be and this will just be It'd be right Why does it complain? What's the sip here? This is gonna be giving me a list of Oh, okay Maybe this needs to be in Sip here it's gonna give me two ints Int and char yeah, and then fold So it can't match type Oh, okay, I think I want to fold Or no. Yeah, okay. I know what it is. I just I have to flip these arguments That's what I want to do. Okay a split at seven s How is it complaining about Two int row Column pretty much you type it with actual type. Okay. All right. So I this skirt Uh, let's see. Let's let's say Print out map two bits Uh Let's let's just write 253 Uh six seven 38 zero All right I'm also applying it to I want ours to be set bit I don't know bits so This will be two Maybe I want to start off at one Let me see. I'm not familiar enough with that data dot bits interface Okay, let's let's do gc Because the first one here should be should be 44 And then uh Let me see set Uh set bit zero Of of set set. I'm going to set the first bit on zero Okay import data dot bits. Uh, why is Why I just I did this wrong Get contents. What does get contents do? Oh, okay Yeah, but like I Compilation takes like one second. So I'm fine with it. Um, okay, so F B B B F F zero one zero zero one one Uh, so this is I mean so What do we want here? It's okay. Can I so let's import data dot bits Uh five and eight Set a bit. Let me see set bit Okay, so set bit Huh, let me see bit size Five and eight bit size five Oh, I already changed this to be one. I mean changes to act to be zero And then maybe works How do we get 88 here? All right, let's see because I'm reversing the stream So we want the first one here to be 44 And we figured out that, you know, it is So zero times 64 plus 32. Okay Let's just let's just do it Uh, let's just do it old school Now I wanted I want to do it properly But I I just gotta figure out this data dot bits. No, I have to figure out this data dot bits interface data dot bits Okay, so Set bit takes in something that is a bit set a bit Okay of Zero and if I set bit number five if I set bit number one I want to get two. Yeah, if I set bit number zero, I get a one. Yeah That number three I get an eight Okay So if I so, okay, so This is supposed to be the bit number here. Okay, so A B Okay, I think I think I'm messing up the something here So type of fold L So I want to take an integer Yeah, okay, so Okay, so I have okay. I think I think I'm doing it wrong. Right. So it takes in a list of a's Yeah, so fold L takes in a list of a's And a function that takes a B And that thing and then it's it Set the bit B I right. Okay. What if I just set zero here? So F B Okay Let's print the input and then we map over it Okay, so the second one is correct, right? We get five And then here we should get seven seven four So it's five seven seven one Okay Yeah, we we were trying to fix it. We stopped reversing it Okay, and then we need multiply the row by eight And then add the column Isn't that this the same as as running Two int on the entire thing two in Yeah, that's the same as running two int on the entire string um Okay, so so, okay, so Let's just simplify this then to We don't actually need this too. You can just run two bits of int And let's get our input Okay, wait and what now we so we Yeah, we will map the bits and get the max. Uh, let me see new file input And the question we want to answer is What is the highest seat id? on a boarding pass Okay, um Day five You can do max, right? maximum Okay, and then max Compile it to run it. Let's say time here just to see how quickly it goes Three milliseconds nine seven eight Is our answer All right Got part one Whoop whoop Yeah, it's just bits. We just took us a bit of time to figure out the bits interface for But uh, okay now I think part two will be a bit harder. I mean this isn't much code, right? Let's see. Let's see what happens. Uh continue to part two Okay, so the fast and simple signs have turned off time to find your seat Completely full flight. So your seat should be the only missing boarding pass in your list. However, there's a catch Some of the seats at the very front and back of the plane don't exist on this aircraft So they'll be missing from your list as well Your seat wasn't at the very front or back though the seats with id is plus one and minus one from yours will be in your list Okay, so Uh, so all the seats will be full Seats with id is plus one and minus one from yours will be in your list Okay So let's see because I the input is like a thousand 965 inputs So we can just find the missing one Let's okay. Let's work it. Let's work this So let's see So the maximum will be there the minimum Will be there Um So let's see here. Okay. Let bits equal let The sids so this is seat id's equal map Okay, let's let's just get the input Get input and Put map to bits put Okay Maximum so the Mac Ma so let's do min and ma they are going to be the Maximum of seat id's and minimum of seat id's Now let's let's print out like print Uh ma minus mi and print length of seat of minute mom and maximum Okay, so Wait So we have the Uh, how long have I been coding in Haskell? Um I guess I started it seriously In like 2015 or 2014 probably That's when I did my started my masters Uh, I think I encountered it first in 2014 So but then then I've been programming in it heavily since yeah for five years or so That's yeah It's been quite a while Okay, uh So there are so the difference between the maximum and the minimum so I was expecting it to be like one less Uh, but now we have to figure out which one is not in this list And let's let's just print the maximum maximum and minimum Sort Let's sort the seat id's I just want I'm just curious. This will this will return like a big blue list So do you have all the seat id's right, but it starts at 13 and it ends at 978 So we're we were just supposed to find the one element that's not in this list, right? So Yeah, okay, let's do that. Let's say Just minimum to max Uh Let's just do set concatenate and import data dot set I mean, this is like not the most efficient Yeah, yeah I think that's true, but let's just let's just try it like set like we import data dot set set and then we do import qualified Set And now we say set dot from list And then we do set dot from list of the seat id's print this Uh, and it's complaining. I think it's complaining about this probably 727 it's not in the list I mean, we could do this in a more like binary search way, but I think this is We're doing it in four milliseconds, right so Yeah, I don't think we need to let's see. Let's see if the id is correct. That's the only one that's missing, right? All right This is a quick one So Yeah, how did we do it? Well, we you know the I think that the key trick to this one was the fact that We like, you know, it goes into You know, let's go back to the let's go back to the Edwin calendar. Let's go back And see here like it goes talks about binary space partitioning La la la, right? But that's just that's just a fancy word for binary, right? binary Is just a binary space partitioning of the integers And then because we are You know, and then we noticed here. Okay. Yeah This also, I mean, we could have spent a so I think yeah, so I I agree with by that in my I don't know how to pronounce that but You know, if we had gone into the whole, okay, let's go left. Let's go right. Let's do binary search, blah, blah, blah Then we would have had an issue, right? We would have had a lot of stuff to write, right, but um We don't actually need So because like and then we try to just figure out that it's just bits, right? And then we just find the bits and then they're just literally asking about the bit I think the key insight was like, okay, wait RLR Is five, right? So then like, okay, but 101 is also five in binary, right? So Yeah, I think this is Just like the easiest one so far. I mean we we didn't spend Almost any time on like the actual issue This time around We spent almost all of our time just Trying to figure out how data dot bits work Um, and now I know how it works, but I think but it's cool Like you can you can just treat numbers as integers or it's or like bits and just set bits um And that yeah took a bit of work, but I think we could even like do this in a one-liner, you know Anyway that it That was fun Yeah, so I guess it was a short one today, which is Good, I think We all have our Saturday to check out and Yeah, you can catch the episodes so far on youtube. I think there's a I don't have a link on my channel. I should probably put a link, but you know, is there going to be on youtube? um, the previous ones are already there and Yeah, but you know I think yeah, I think I think ballad amaga is correct in the sense that And I don't think he is correct in the sense that I mean you don't need clever algorithms you just need to Figure the trick to the issue problem, right? You don't actually need to go. Okay. We need to implement Super efficient binary trees here to do the partitioning, right? We just Just go to bits go back from bits. That's all Anyway, yeah, that was quick today and uh Yeah Yeah procedural. Yeah, I mean that is true. I especially like I especially last year We had this whole Virtual machine thing we had to implement right and then we wrote code for that and that was like a virtual that was like a I mean, it wasn't a functional functional virtual machine, right? It was a Like it was a it was a touring machine, right? It was an imperative, you know, it operated on symbols and had instructions, right? And then we had to like simulate that in a functional language which which meant a lot of uh Ray operations, but I mean I think I was I was a very proud of my solution That year I probably have it somewhere like github treat low Code um, let's see. Uh, these are these are so this is so bad at Like it's not google see here if google finds my github repository No, it just literally ignored um You can see all my repositories What did I call it? AOC 2019. Yeah, here we have the code for Last year, right? Uh, I lasted nine days And then, you know, this was the like machine we had to write, you know, we had these operations Uh, you know, I had to use I used states and then we had these frozen state arrays um And then it's all read mem write mem read mem write mem, you know Uh, and it was super slow when I used lists But then I I I went into these unsigned arrays in the state monad and they are They are just optimized so hard by ghc Like this is because, you know, you need to do this a lot, right? You need to have arrays and Operate on them and this is even while. Yeah. Yeah, I think it's yeah, it's unboxed array. Yeah, sorry. It's not It's not unsigned. It has signed integers, but yeah and uh This was like very fast But you know, we we did have to deal with a bunch of Of these state issues, right? Write st revs You know thaw We had to thaw the so I mean so we we initially always like took the inputs We initialized this array And we modified we wrote st revs. Uh, I had thaw somewhere here, right? Thaw Yeah, we thought we thought the so it's like And it was it was a bunch of It was a bunch of it was a bunch of work to get it working initially And uh, yeah, it is a bit hard to get started. I I don't disagree, I guess Um, but it's also like it's a new way of thinking. You know, it's a new language You know, whereas I you know see and java And javascript and all of those they're essentially It's essentially I I want to say algal or something like that. Like it's it's all the same language Except there are like they're different dialects, right? And then there are there's a bunch of differences, of course Like, you know java runs on the jvm But you know java runs on the jvm And then it has some package manager stuff Like an object. I mean it has I mean it has a lot of stuff built in but it's still It's still just essentially see You know, it's still just yeah, it's it's still it's a dialect of c with a bunch of stuff added in by default, right? but uh Yeah, but with with haskell and functional programming languages You think about things differently, right? So so like here, you know instead of You know iterating over the string and walking one step check Turned it a bit walk knock step check turned to a bit like instead of saying let's do this this and this we thought What is the transformation we're trying to do here, right? So So we we like we we we we change it to be a like a flow of data Throughout stuff and that kind of compositional thinking Like by the marriage talking about like that that Helps you a lot and the cool thing about haskell and ghc, especially like it it optimizes all these so it it fuses things and then Once you get into that groove of thinking about things this way So instead of thinking about recipes and like do this tip do the next step and so on you're thinking about You're thinking about data flow Essentially, and this is kind of it's data flow programming. You're thinking. How does the data flow through my app? And it it is it is a bit like The difference between no sequel and sequel In some sense, right where like in no sequel You construct a database in such a way that you know, you're thinking about what are the operations? I'm going to apply to it You know, so how am I going to sort the table? Well, what operations and am I going to run on it? And that that changes how you think about databases because then you're thinking about Not just how the data relates to each other, but you're thinking about how How will things flow? Like what am I going to be querying right? And that's why that's why you get like very very fast document databases Because they are structured in such a way that you're thinking about the queries you're going to be making And So how does this relate to functional? That like when I when I dug into document databases I got a new Feel for how to work with data and databases, you know, so instead of just saying, okay That's where it's very academic, you know, you put in the ER diagram and you Make all these relations and you say this one relates to this in this one This one has to be a foreign king this one and you kind of set up the whole system And then once you have to run some queries, that's You know, you you have to hope that the query optimizer in postgres Uh works a lot works very well, right? But in document databases, you're thinking about the operation from the start So you're thinking like what what's the index? Well, what are the operations I'm going to be applying? So, yeah, and uh, yeah, so that's all For today, I'm going to I have to get back to work actually You do some research and teaching and uh stuff like that But I will see you all tomorrow at the same time six o'clock European time five o'clock Icelandic time and Whatever time it is in the u.s. Right now. I think I think it's No No, no, it's not new. I mean, okay. It's also depends on where you are in the u.s. Right? anyway, if you're checking out the youtube, um, you know Make sure to like and subscribe That's what you're supposed to say, right? and because you know, then I I know what what's like if I'm doing yeah, I like just like Because I want I want feedback right because like in the past few days my sound was off But I fixed that um I changed where the keyboard cam is. I think I really like the keyboard cam. It's mostly So I can get away with having a super loud keyboard But yeah, the music could be a touch lower. That's That's true It'll change it right now. Oh, yeah, it's Higher than it used to be but yeah Anyway, thanks a lot and see you tomorrow for day six of the advent of code All right