 Alright, welcome to today's Android of code gonna be a short one today I'm gonna give myself like 30 minutes So Because I'm gonna go for a visit. Hello sobo so budged Don't know how to sobo CJ. I don't know how to Finish that. Alright Yeah, we're not gonna be too long today. Let's just get to it. I hope it's not hard problem today All right, a lot of I mean, this is supposed to be low-fi music, but it's actually It's going hard, right? Day 9 not to just Day 9 I remember that guy module main where Okay, all right, so we've have the Kind of the base skeleton. Let's look at problem 9 Okay, the case is a bit lava tubes parts are even still all conical like a small hydra on variance Release smoke okay love it. Oh, that's cool Parts are even still will can't be active small hydra thermal vents release smoke into the caves. Okay If you can model how the smoke flows through the caves You might be able to avoid it by and made that much effort to have my turn is a height map of the floor The nearby case for you Smoke flows to the near point of the area. So for example consider the following height map Okay, each okay, so we have this height map. Let's see new file Example paste it Now we're going to read the example we're gonna say read input It's gonna take a file path and it's gonna return List of list of in's Over in IO, right? So read input It's gonna be Read file and then we're gonna do the classic F map a map F over lines and now So map line where line Takes in a string and returns a list of in's Let's see line Okay, so we are just gonna go Line equals map C to D Where I see to these this is gonna be a character It's equal to read add in There's no just read So see We just make it into a string right for read. What is the problem here? All right, we didn't do a dot. All right, let's read input Example and pipe it into a Print 03 9.hs and We just time it we're just gonna run this command again again All right, we've got the height map looks correct. Yeah Okay, where nine is the highest and serious the lowest location be you first of all Define the low points and the location that are lower any of its adjacent locations. Oh, no, we have adjacencies It's gonna be a graph algorithm Most locations have four adjacent locations up down left and right location is on the edge or corner of the map Have three or two adjacent locations Okay, so let's let's just write that right okay, so let's say a Task one, so we're gonna get that like the side neighbors right and then we're gonna transpose it and get the side neighbors Of each and every one so we're taking a list of hints and We return a List of so each number Do we need to like return coordinates? What's the sum of the risk level of all low points on your height map? Yeah, okay, this should be enough for for now, okay, let's see task one Okay, so nums, okay, so We're nice Okay, so I have the so that I have the I have the corners So I'm trying the edge. Okay, so So let's say here a nays Defined for now so nays Let's say So we keep we do it the nays of nums Okay, let's say just say Nays of nums plus plus the nays of transpose nums Okay, so here we get So we're gonna get it like this. Let's see Nays equals undefined. I think I'll have to import Transpose from data.list It's a very basic Function, right? It just takes the list the list some transposes it Okay, and Alright, so we have to actually get the Coords or we have to like cleverly set them together because like it's not enough to just get the yeah, let's see The nays of the first one and the rest Nums and then and two and then are so this is gonna be a N comma and then we have in the end to So we just get that like the edge one Plus the nays prime of And and to our Okay, so Nays prime that's gonna take the second one. So it's gonna take n and Two and three and are And this is gonna be equal to a N2 and comma and three Plus He Prime of N2 and 3 are they will do like this but nays Prime now need to do the other edge case So we have n2 and n3 and then we have an empty list This is going to be N2 comma Just a list and two comma N3 Okay, so this should work because we will never get because of the gas cell that's just how they work Yeah, okay, so this one is gonna be let's just check what we get right here for Map nays now, so it's gonna be a line list of lines, right? Let's just check what we get for the First one, right? Oh, no, it's complaining because right, so it's gonna be a list of these Why so It's a neighbor two is One let's just take the head of this list, right? Okay, so we got the first one. So it's two one Nine nine nine right so two what one one as a neighbor nine and two nine as a neighbor Nine and one and nine nine as a neighbor nine nine and then we get the nine as a nine four four nine okay, so we have This is like one and now we need to We need to Let's say here map and Let's just print all of the lines Okay, but I still actually I actually have here like the two one nine nine nine four three two one one Actually have the original structure, right? So if I take this Okay, so there's gonna be a So I have Map so this is gonna be the horizontal nays Okay, so map nays nums Okay, then I transpose this so And then I'm gonna get the other list So two three nine eight nine right two three nine eight nine But I already have the yeah, okay, so right nays two Up n and to R is equal to And here we're actually we're adding to the n ones right so n comma L And then we into comma Right, okay, let's just do it like that so n at then something and to add Something Is it gonna be something and then nays prime and and to R It's gonna nays to prime actually Right so we we're adding to n here, so we have the we don't care what the We do care what we don't care what the n is actually. Okay. We care. We have we want the a the envy and the N neighbors and Then we have the end to and they were just care about and we too. Okay, and now this is going to be a Envy Envy to an end Right, so we add me to to the thing And then we just continue There's a nice To prime nays to okay, so we have here and to and now we're adding to and to so add and we to So we have any too. Let's call this just a and and and to and at Yeah, we actually don't so we don't actually use the in here so it's So we say and to and then whatever They were not recurring right so this is and when here we have the envy and the end and And then we want to return the envy into an end and then the same here Okay, so here we actually just call about going care about the envy and we don't care about the neighbors Here we care about the envy to and the end neighbors Here we care about and we three now but not the neighbors and This is gonna be a Envy Envy three Plus let's just say like envy envy three and then Nays to prime what is it complaining about now? perfect, oh right Wait, okay, so I have here the This is the envy to right. Yes. Let's see Yeah, we're on part one Fabian and let's actually Transpose this again So we have three one and then the one here has a nine to nine. Yeah, and this nine here should have one eight nine In one nine Seven nine nine and the zero here should have Oh The one here has two two and zero and the zero here. Oh, this should be zero, right? What did I do wrong? The one two two zero is correct. So before we did all this nice business We had the two one nine nine Nine four three two one one two one nine nine nine four three two one Oh, I think it's something wrong here So if we're at the end All right, so we're at the end. We don't want like we want like this and three and two So then we want the end value And then and then And then we want and two Envy and then so that yeah, then we got the zero one one here and eight nine seven and then One two two zero still. Yeah, okay. We built this one up now, uh, we find the So now we built up the neighbor map, right classic No need to graph algorithms. Okay, so we have this one This is a list of lists Now Uh end map equals this Okay, so if we need to map Uh, do we have the end map? Now we want to return the sums, right? So we say map When we're mapping a list map Uh F map sum On end map And now we should get the sums of the elements for Yes And then uh The first goal is to find the low points locations that are lower than any of its adjacent. Oh, okay So it's not sum here. It's a Is low where is low takes in n and r and Is equal to uh All A Locations that are lower than any. Okay, so it's a n less than r But here we have a Let's just put in here and then we find the low points We get the one here And we get the zero here And then nothing this row and then the true for the five here Okay, then we say a All right. Okay. Now we're going to uh map Filter SMG To get just the low points map at first And this one should give us List of list of ints What's up? No map map FST over this we get the But instead of mapping we just do Some And then we do the sum Okay, you know we can't map here anymore. So we just print 11 which is oh this one should be 15 That's not good Where do we go wrong? Let me see. So we get the one and the zero We get the five And we get the five Oh, and then we didn't Risk level of a low point is one plus its height Okay, so it wasn't the sum map here. It was actually map First Then we have to say one plus No, I get 15. Okay Let's run it for the input. Oh, we didn't change for input 417 in 64 milliseconds All right, that was the right answer your one call started finding that slate keys. Okay, cool Now let's check out. So we did part one So as I said, we're gonna do a short one today So I'll see if we can kind of figure out how to do this right away But if we can't it's gonna be I'm just gonna yeah, and literally sorry about that. We'll finish this then tomorrow All right next you need to find the largest basin, you know what areas are most important to avoid a basin Is all locations that might to flow downward to a single low point. Oof Therefore every low point has a basin although some basins are very small The locations of height nine do not count as being in any basin And all the other locations will always be part of exactly One basin. Yeah, this one is gonna be hard Because I mean it's not hard, but it's like We didn't do the We don't have the actual adjacency list, right? We just have The list of the values of its neighbors Okay Let's do this so we still have the uh the the adjacency lists But let's uh, let's see what we can do Real quick. Okay, so here we have a transpose and map a transpose Map nays nums. Okay, so instead of saying map nays nums, we're gonna say map sip zero nums So we sip zero with nums Transpose Sip Sip So map nays. Okay, so what what why doesn't it want to cure us? Probably because I use n map here. So why doesn't it? So instead of so nays you take a list of list of int comma int to um So first. Yeah, so what I'm gonna do is I'm gonna give them all an index, right? I think that's probably the best So we have the numbers index indices and Geez Yeah, actually I because I have uh, I I have to actually go Now To I'm gonna go for a visit to a friend So, uh, yeah, so I'm thinking I'll I'll let this be for today Um get add day nine dot hs And get commit task one of day nine And then what I'm thinking is I will do um I'll do a double feature or triple feature Or maybe a double feature only this this weekend Right, so, uh Yeah, so I won't be available tomorrow either a So I'm gonna leave task one now here, right? Then do a double feature of a day nine and day 10 like the rest of day nine and day 10 on a Saturday And then day 11 and day 12 on sunday, but no exactly a The approach doesn't help at all Right, I mean I find the low points And then I can kind of Okay, so I know what the low points are. Okay, that's a good point. Let's Let's look at that. Yeah, because like I didn't model it, right? So I know that the low what the low points are So I guess I could kind of uh start from the low point and then a Find um, let's check it. Let's let's think about that Yeah, that was task one, right? We know which ones the low points are so we know that You know, that's the one zero five and five Right and those are lower than all of their neighbors and every all of these are exactly in one basin so you can see that the low points are the lowest point Of each basin, right? Yeah, so if I had an array, I could do this easily. Let me check Uh, so we have the low points now. Let's uh, let's go back to Okay, I'm gonna give myself 15, 16 more minutes and see what we'll see what happens I really need the uh We have the neighbor map Let's the list to see event. That's what I'm trying to do. So this is uh, some nums so indexed Okay, and then we actually gonna So this one is going to take a function here Uh, we're So what is this? What is this type? So it takes on the int and a list of integers. Yeah, yeah, exactly so we take the a The y coordinate And we say a map a x comma v to a x comma i comma v And then this will be like this And then we have this n-map is going to be indexed Now let's print out the indexed n-map How did you do it, Tannop? So I think this is easier in a imperative language, right? It's not good Right, so let's do the map m Okay, so let's look at the first one right so it does a zero zero and it has two zero one is three one zero is one Okay, and Then we have to define a new map is low. Okay is low It's not gonna take it's gonna take in uh It's gonna take like a A pair right which is its coordinate and its value and then a list of pairs And it's going to a So this is gonna be a c v n add Okay, and it's gonna return n and all v dot a And this is gonna be a list of pairs v less than dot s and d So now this will is low operates On our n-map So let's say map Map is low. Okay, so now we can find the Low points again But we want to keep the um So we want to keep actually the Neighbors except if they're nine. Okay, because the nines are barriers And so we have a map Um So we're mapping over Something over the r I'm going to map maybe here No a map is low. Okay, and now I'm going to take the r's and I'm going to remove So Filter so I'm going to remove an r a so Not equal to nine dot s and d Okay, let's see here Okay, that's the first step now. Let's filter also out the ones that are nine Because you don't care about the basins Okay, a Okay, so this is gonna be a Map filter Okay, and what I'm going to say is uh, I'm going to take in something here And it's actually a triple I don't care about second. I don't care about the third. Uh, and it is on x A x Not equal to nine Dollar did I do too much? Yes No So I want this one and then I want this one Yeah, okay, and then I want this to be mapped Over all of them. So now we're moving all the nines Oh, let's actually do A map and now we're gonna map Over the map So the first one is like zero two and zero one, right? So we have and then we find the low point in that Basin right and then we're gonna find the second basin based on the zero But now I've screwed it up right because yeah, I can't remove the nines because then the coordinates Aren't valid anymore So I can just ignore the nines though, but yeah, okay, so now if I map map, okay, so is this like a Let's just call it something. I don't know VB check is equal to This one VB check, okay, so LPs is equal to map filter And now no x To x over the VB check filter Not no so LPS so we get the list of the low points with their With their neighbors, okay, and We filtered out the nines Okay, so for each of these low points, uh We have the end map. Okay, so let's Okay, so Let's say basins Of empty is equal to Empty Now basins of a low point and LPS is equal to Find from LP LP a Okay, I'm yeah, I'm actually gonna say basins equals fold R Um, oh my god basins equals map find from LP LPS Now find from LP. So a flow point is of this form, right? and How about I actually just do Because we don't actually need the neighbors afterwards. So let's do it like this This is actually still work. Yeah, okay Then it's just going to be a pair there Um So we filter not null. Let me map my baby check Maybe check map is low Yeah, we can have LPS Okay, so, uh basins is equal to Um map find LP so Post LP is equal to uh Okay, that's just the Yeah, and map right. So we give map map FST Maybe check because we still want the filtering So post LP it's going to be like without the without the low point Yes, but we have the adjacency list that are not nine Okay, and now We're gonna have the Basins equal. So these are going to be the connected trees, whatever map find From LP like this is a classic. We're not finding The forests in a tree whatever find from LP. Um Find from LP And we have the root and we have the LPS Okay, now our Let's put all of this at the top Actually from LP's So how does a rope point look like it has um So we have the cohorts and we have the neighbor We don't actually need the coordinates So we have the Value and also the chord and the value And the ends Equals um So let's just return ends here to just see Um basins Find from LP And the map right so because the yeah, yeah, okay, so I do um So LP says this And then we do Let's do actually Can't oh, yeah So we have the LP's and it's gonna be A list of lists so we just do Concat Oh, we don't actually need this. Yeah, okay, so we can just do Concat map here So map and print All right, this is the neighbors right so we don't ignore those and this is the NS And we're gonna print the neighbors of all them basins Okay, so um, so for each low point right A we're gonna say So these are the neighbors Okay, so we're gonna say find from L map and find from LP um So we're gonna take the neighbors So we have the uh Chord C and the V and the ns Where and So we have the chords so we're gonna say here until no new So we're gonna say until No new A And then we have a set dot empty and ns Now we have to import Data.set as set Where okay until no new and then we have found and then we have ns Now this is gonna not gonna be set dot empty. It's gonna actually be set dot set dot single ten C um and um Let's just say where until no new is equal to So C and the ns is equal to C Let's just say that Just to see what we're getting So then we just get the set One zero. Yeah. Yeah because the Basin here is one zero because it's x comma y Okay, um So where? Yeah ns and then we have chord is equal to um, this is a Post LP bang bang So this is x comma y Post LP bang bang y bang bang x And and then post LP so this is gonna be A a list of blah blah and the so that it's gonna be the second one Okay, uh, so get ns That's gonna lead really turn us a list of neighbors You get what extensions are you using from Haskell? Uh, no extensions really. This is just the this is the Haskell language server so, yeah um Okay, so let's just say and then um map fst So, okay, uh until no new ns. So we have ns Mm-hmm Okay, so we get the neighbors here and then okay, so if we have until no new of empty is equal to c Until no new um See and then we have a neighbor Uh, what is the neighbor here find from LP? So this is a list of Yeah first ns. Yeah, so this is gonna be just a chord a found found chord chord rest a We're gonna say a get ns Of chord Okay, so we get all the neighbors of the chord And then we go Yeah map filter set dot lm map, so this is gonna be filter not set lm Found right we're gonna take those we haven't found already a do do do And this is uh, oh, I think it's set dot member a so this until no new found so this is set dot member All right, so this is gonna be Where new equals? Okay, so we just do um So we filter those uh that we haven't found new is gonna be a list of Int comma int Okay, so we kind of look and we find and then we say we're new and Then we just do Until no new found until no new and then we say found set dot union set dot from list new And we say rest plus plus a new So this should give us the This elements. Oh, no Oh, no, this is gonna just give c. Okay, so we get that we should get found here Okay, so this is the first basin zero zero zero one one zero Then we have a second basin the third basin and the fourth basin Now we are going to say a Get these x comma y Is you call map second dollar second Post No, right. Did we we're not map. This is gonna be the first uh this second of the first Of post LP bang bang y a bang bang x So this is gonna be uh, this is gonna return the value Okay, so we have a set Uh find from LP Okay, so we found the set Now we could do here, uh, we found the basins A map the set dot to A just just to do to List I don't I don't care which one it is so now we should get Sets and then we Map get these So two three and one for the first one two three and one exactly then zero four three two one zero Yeah, and let's map get the Some Okay, let's see. So this is three nine fourteen nine Oh 16 what? Oh size, this is not uh, okay, so we're we're taking the sum If you take the links so three nine fourteen, uh Oh, and then we have one more here Okay, let's see So three nine and fourteen and then there's something wrong with this last one So one two three four five six seven eight nine So find the three largest basins and multiply their sizes together. Oh, we don't actually need the Values Okay So then we don't need this one But let's uh, let's Figure out why So we get the zero one zero one Okay So, okay, let's look here. So zero one. Okay. This is zero one two three four Okay, so we should have a So I think this is nine by four. I want two three four five six seven eight now. So this is nine So we have the eight Yeah, you have this one we have the seven, uh, let's that's actually Map Let's still have the get fees here. Okay, uh map C to uh c comma get v s c Just so we see the values as well Okay, so we have the eight the seven and the six the eight the seven and the six And then we should have the five We have the five. Okay, but we don't have the um Five four, let's see for the three we have the seven And the six we have the seven and the six Maybe I'm adding like too early to the Let's do that. Let's just change it Okay, so if a core set dot member Found equals until no new found rest All right Otherwise it's equal to until no new found and then found set insert Oh Core set dot insert A found We add the core to found and we say rest plus plus A Get ns core This should terminate, right? Until yeah until no New Okay, now we get more elements here at least and Let's map it set dot Size is what we care about So three nine fourteen nine. Okay three nine fourteen nine. That's correct And then we take the top three sort And the three largest reasons. Okay, sort reverse And take three And then some And it doesn't oh, okay Let's see. Yeah. Okay. This one is just a filter s and t actually Okay, let's check it. Uh, no, there's nothing to print So it's just task two So we'll get 32. Uh, no, it should do take the product, right? Product, so let me sorry. I was just like I forgot chat. So do you need to find? Yeah, okay Okay, so tana probably did it better than us Because you know actually doing something in st revs And like actual Stuff, but he says it's unconventional, but I like this right like you should probably do it like this Because it's a very imperative problem, but now we're here. Just you know this This look up here is not This look up here is not good right So we should probably actually yeah, but we're actually we're only looking up Once so it's going to be fine and we could replace these things with rays if it was If it was behaving bad if it's super slow, let's check it up. So we get 11 34 And now let's run it on the input and let's just see how long it takes But like I don't think we're doing anything super crazy. Okay, so 143 milliseconds Taneb, how long did it take you? I like we don't know if this is correct. It's correct. Let's check All right, we got it Okay, I got nerd sniped a bit there. Uh, I was gonna go Leave early but Whatever Hey, yeah, I mean, okay, but like this is just a it's a lot of Lot of manipulations, right? And but like yeah, like I said, I think like if it takes under a second We're not gonna worry about it Right, this is under the under the 100 millisecond sometimes, right? So we are uh, we're doing fine Do-do-do Like the flame valve stepping number versus it Okay, uh Get stages get add update task Day nine task two Get state. Oh, I got get add update get commit am day nine task two I mean, I think we did pride well in that sense that It's not a lot of code Like we're not reusing anything, right? But uh Yeah, okay. This one is a good point. So I map map filter dot snd So filter snd And I can remove this map and then we are also mapping again here This is just one big Map screwed up here. So this one works now. We are going to take the whatever is in this map here Paste it and a dot Remove the And still compiles. Okay. I think we uh I think I should really get going I got nerds type a bit when Fabian was like, hey Didn't part one helped you at all and I'm like We do know the low points And that was what was All that took here, right? a because We don't really have to find anything, right? We just need to like because we know where to start We know where to start. We start at the low points And then we find the tree in the adjacency graph And there are probably extremely fast algorithms to do this But we don't use them and a another thing is like, yeah We can use functions to do this like we had this add-in functions probably like a combinator, blah, blah, blah And like this is not pretty like this nays thing. I mean we did screw it up First right with with this but yeah Uh, but it worked out right and it's not using anything fancy and it's uh Okay, and if I get lucky It takes 71 milliseconds, but uh, apparently not all that I don't know 76 okay, so it's uh, it's at least it's pretty good Do do Uh Yeah, but I'd like this algorithm, right? It's just like walking the neighbor graph adding the new ones And this is like it's computing a fixed point, right? Because it's just compute compute until like it it doesn't find anything new and then Yeah, all right Get status, uh, did I keep You kind of am smaller It push okay. We push it again. All right. Hey, thanks for tuning in today like I said earlier And then I got nerd sniped I'm not gonna do a stream tomorrow um But I'll do one on Saturday like a double feature probably and then another one on sunday We'll see how the double feature goes. Maybe it will be like a 1.5 feature Uh same one sunday. We'll see We'll see how well like we will do it eventually, but we will see like how fast we do it Right, we will have to wait for like a fast one. Do the little one All right, but thanks for today. Thanks for tuning in and I hope you enjoyed The Haskell content. All right until uh saturday Thank you so much Bye. Bye