 All right, it's day 12 of the advent of code. Sorry for that redo. I forgot to record But now we're recording and we are ready We are ready for day 12. We're almost halfway through so tomorrow that we do task 1 will be halfway through but These are getting tougher and tougher So let's get started right away, right? Let's see day 12 Okay, so yesterday we were doing some Some monkeys. We have to do some modular arithmetic. What are we doing today? You try to handle it. Okay. Oh, right. So we fell into the hole. Hi, little Lanny My biggest fan is here. That's all I need to do these streams. Anyway So we fell into the river. There was a rope bridge and then We had to repair our device And then you walk into the forest and the monkeys attacked us Quite an eventful journey so far What are we trying to do now? Okay The river probably must be too little to get a decent signal You ask a device for a height mouse in the running area Seek a locust seller with a slowest elevation Bs and X loads and so on to the highest elevation said Also include a high mark for your composition S and the location That you get a back signal E the composition S has a location A Location that you get the best signal E has elevation said You'd like to reach E But for to save energy you should do it in as few steps as possible During each step you can move exactly one square up down left or right to avoid needing to get out your climbing gear The elevation of the destination square can be at most one higher than the elevation of your current square that is if your current elevation is M and You could step to elevation and but not to elevation and Okay, for example You can move exactly one square up down left or right Okay, so we just kind of compute the graph and then we find a path What is the use as you're going to move from your current position to the location that you get the best signal okay? Let's just say start with the example input Example now are we also surrounded by a's in the So if I understand it correctly We can get we can get We can go from a That's okay. Let's just We can go from Avoid needing to get out your climbing gear the elevation of the distance square can be at most how and higher Okay, so it doesn't have to be much lower than yellow so we can always go down But we can only we cannot go up so it's a to said now let's read the input. There's gonna be string Let's just go There's gonna be no reading put a Put a what is the So what my question is what is the elevation of the destination square right? Okay, that's just Okay, we're gonna do read file We're gonna go lines Okay, let's just not have this to be in this is gonna be listen to means Okay and we're gonna say where Let's just look at GCI Okay, so let's say that we can always go one higher up Which is map from in Just Let's just see what this does for the example GCOD12 Okay, so these looks like kind of like kind of connected component so I can go anywhere from here to here Okay, okay, let's not worry too much let's just have it be and Let's make this into an int map of int I want to have this be an or maybe I could do like an unboxer a GG array a pencil using array. This is not good Okay, let's see now. I want to connect convert this to to array Or like a an int map of int map maybe okay to array and Let's Or did it out in this so you're gonna map sit with in the eye and the row, so it's gonna be the Row number and then the row to sip Okay, now let's say print okay, so now we have a kind of coordinate list and now we are going to Do a You're gonna convert this into a graph Haskell graph Build a graph from a list of nodes you need to identify keys and list of keys Okay, and now so what are the connections here? So it can go from lower to higher. We can always go from lower to higher. I feel like if we connect the whole thing So we can go from a to C And and okay, we cannot go from a to C We can go from a to B, but we cannot just like join together completely Everything so it would be very nice. We could just be okay. This is a here You can just kind of join it together, but we can't because Say f of s Well, so we're gonna have s be the So this is gonna be the highest number Let's just do it a thousand and f of e is going to be zero And f hc is gonna be Rom emm see Just so we know where we're going and can always Go from anywhere to the edge, right? We don't need that for the edge. Okay, but so let's see here Okay, now let's construct the graph from this Okay, maybe we can just to get the Yeah, okay, let's make this into like the agency graph Okay, so we let's see here XO neighbors and map So we're taking in bad we're taking the coordinate int comma int And we return List of in comma int equals okay, so we're gonna say here graph equals so we're gonna say Yeah, maybe F map over minus one the chord is a list of now we can't maybe and Okay, so now we're gonna go here I is equal to I actually is graph Y and then X look at back come on why Okay, we take whatever gets out of this and we're gonna go F map Why Had maybe Okay, look up chords map, maybe and then we're gonna look up the XI well is equal to Graph map, maybe and then filter and then we have the core Or Is less than or equal to XI valve map, maybe chords map, maybe Okay, we had to map maybe Okay, and then we do So this is the like the connected group neighbors of the ground Okay, so to array map and print to array now. I'm gonna say a not map and print. I'm gonna say print Neighbors Let neighbors Zero comma zero just to see what it says for that Okay, and it's saying that Zero zero is a neighbor. Okay, so we might be be not equal to Say it's here come one is a neighbor and we can just go up down left and right Okay, let's not do it this clever way. Let's just say We're not gonna do it. This is so cleverly. We're just gonna say Mine is one comma zero This down one comes in this left right Down Okay, so we can go to the left and to the right. Okay. Okay, let's see to array Reading but so we're not going to do it like this. We're gonna say actually We're gonna let's find s we have to find s. Okay Is equal to We do have the adjacency graph That's just this neighbor stuff. Okay, and we're going to like breadth first search We need to know where we're going and where we're starting Okay, so let's do Foldle folder with Okay, so let's say I am dark There we go f map a find with default and so it doesn't work. I'll just do a zip Okay, let me just fold over this one. Yeah, then you and s x comma s y So let's just s x comma That's why just it was fine. I can just map over the axis Okay Just I so let's say map find index map find index of 1000 find index Just s y is equals map find index Okay, now this one is gonna be s y is equal to Maybe just s y and so this is going to be the column is equal to this add Just s y it's going to be at only one place And then just s x is equal to Just s y this is Okay, and the same for the e s x s y comma ex Okay, now So it doesn't find anything that's equal to zero. That's funny. So this one will work. Let's see just s y Get me Okay, and now I know the Start and I don't want to go. Okay, and I'm like going to two five Thinking of it. So this is the s y. So this is the uh, maybe this is the s x Yeah, this is the s x Ex e y five two. Okay. Hello. What are you programming? We're doing a bit of code They we're about to do breath first search So how do we do breath first breath first search? Okay, we have the We have a graph Okay, we have the List of those we've been to We have the Cube target you here and what will it return? It returns Bfs graph Visited equals So Bfs graph visited The empty list This is empty list and then a q. So this is gonna be next up And q's Okay, now where n equals neighbors graph q Okay, now first of all If q equals target Then we just return q Bfs graph visited q q s Target equals. Okay. Now we're not doing anything. We're just adding bfs So I'm missing some kind of parent here, right? Okay, and now filter set dot member. Okay, so we take the neighbors We filter out the ones who are in visited visited Now we need to so how remember the path bfs graph Does this work? I don't think so se bfs g Visited is q is s Target is e Let's actually do it like this If there is a path from this one set of incumbency Either we kind of continue because either we kind of found it or we stop And then case of left Yeah, because this path is not correct, right? So then we go from zero zero to zero one two zero zero zero zero zero one zero two And zero two to zero three And zero three to zero And this path doesn't even go to Doesn't even go to We get that graph right like okay, so let's check it out a And then zero four, but it's getting a right left. What if I just do ns here If it doesn't find it And do bfs graph Be prime prime qs Now, let's just check if the neighbors thing is correct. Okay print neighbors Let's say print the neighbors of this one. This one should be zero one two three four five Point neighbors g 1.5 Five is not an element of the map really Uh, so here we get 1.5 and that should be giving me So I'll be looking up I'm always looking up x and then so the neighbors of 0.5. Yes 1.6 Let's not worry about the visited here. Let's see here Visit it a target. Okay. So if q is equal To target graph visited q q is certain member But it should never be added to there. Let's not do that Okay, I might need a kind of a I need a parent map here try a Incoming to Procure the visited I have the parent map and I have a q Okay, now bfs And this is just going to return maybe that bfs equals bfs graph bfs prime graph target And I need the starting point as well. Okay, so we don't actually look at the graph Start target Now we're going to change the graph Return this maybe path. Okay. No, this is going to be bfs prime Of a set.empty map.empty So we're going to map maybe give Start as a initial queue or bfs prime Okay, so if I have If I have an empty queue Then I return nothing Thank you. I return nothing Okay, let's see if I have a queue then return nothing bfs a If I have a path like if I have that queue is equal to target Then I will reconstruct the This is a parent reconstruct path and queue target reconstruct for parents Just path Okay, reconstruct path parents start and I'm going to say here We're going to be the current Is a big node and we're going to have queue as a target and start reconstruct path a parent Reck path need this so Reck path a And I need that here Reck path a cn equals a cn n column Reck a cn k cn equals queue equals a queue Reck path cn otherwise is a c n and then Reck path a let's start The first argument of Reck path bfs prime has the type set income a So this is the visit it it has the type it has the queue Now Reck path here. I think I just messed this up Okay, so if we found it we just reconstruct the path Okay That's kind of the base case Okay bfs prime Visited parent us equals Don't do a Do the same neighbors Okay, and this is going to be Neighbors graph so we can find the neighbors of the graph queue we take away those are not members the ones we've not seen before Really we map dot update and we could do a map dot insert prime equals full the hell map we we can do the new parent Okay, and Also, we built a new parents map bfs v dot insert vq fbs prime There's one to work And see what the parents are here at the end. This is nothing on the zero zero one point zero Okay, so it went from zero zero to one zero And here if this one does not have any neighbors into way too long just this Easy path for a search. I feel we're always kind of inserting into the map actually add to the queue zero zero one zero So I explore is a zero zero and then explore is a One zero and here a And then explore zero one Let's see trace show qns Okay, so zero zero it looks at one zero and zero one Okay, here's here. Let's add one zero and zero one. Yes The one zero looks at two zero Okay, zero one Okay, it's already been here and it's already been here. Why does it not get there? Okay, zero zero So I was looking at one zero finds two zero Uh, and then zero one it just has no neighbors There's something wrong here Hmm Okay, uh So it's a for one point zero. It's a zero zero. It's a neighbor And then one one is a neighbor To do zero is a neighbor and one one is a neighbor Uh, this one should not be messing equal to xiv xiv minus one I think right so, uh xiv v is less than equal to xiv plus one Now we're at two four and it says two five and it's a Okay, and let's I think this one is not correct. Okay. We look at two point four a lot of times Okay, so two So this is our what this is Okay, a neighbor is one point zero Yeah, so the neighbors are one point zero is two point over We don't want to go back to the start. Yeah Okay, so zero zero has one point zero these two okay Then one point zero from one point zero we can go to Two point zero or one point one. Yes Do we zero okay for and then from zero comma one We go to one one or two comma two Okay, then from two Two point zero we cannot go to q but we can go to Um, uh, huh. I'd say we can't go to Yeah, I think I got it correct again. Sorry This one should be a y and x then this should be y and x and this This should be y and I don't know. So this one should be x and y this one should be This x That's why Okay, and then we start off at zero zero. Okay, and we get So sir, sir has one point One one point zero and zero point one. Yes now from one Point oh we can go to two point zero and one point one. Yes Okay for zero point one Go to one one one and we can go to um zero point two. Yeah From Two point zero we can go to two point one. Yeah, but not this one. Okay From one point one one point one No, it seems to be doing Some of them multiple times, which is no good. Ah, they do get added multiple time Okay, this is not should we should not be visited should actually be seen Okay, and scene prime is a third time union Seen daughter third dot from list Okay, so now we should only see things once. Okay Now uh zero zero. Okay, we get these two and then zero one we get One point zero we get Okay, what is the target s comma e? Target is two point four. Ah, okay. I didn't flip that target yet. I thought I flipped it here Um, let's print the graph as well the graph says zero It says two comma five Okay, so that's the one we want So this one is still wrong So this one should be two comma five This one should be x and this one should be Why this one should be x and this one should be y My 12 up. No, it's only finds a path Okay, and it's saying okay. I think this one is just wrong. All right PS prime so we're gonna say This is map So new parents Ah It's gonna be ps map dot union a ps and map dot from List uh repeat q and s sip repeat q How can you not be in the map? Zero one zero two Go zero one and then zero So look at it. Okay This one's actually correct. It's saying that zero one zero two Now why doesn't it see any neighbors from here? One of the top is all the team, but it's usually zero comma three, right? Maybe went around somehow right So here I'm always saying So, okay, so I want to I want to build a path backwards, right? Okay Right path a path start. Let's wait over here. We do a and and the gpc and equals start equals q And I want to reverse the path in the end Okay, and now It's over building it up from the back. Okay, but then this one is not like the scene, right? So I'm actually gonna say Can we build it up backwards? Yes We get something here Let's use the length of this Just 26. Well, that's not the same as the jmb, but okay 30 bfs and a t uncurry bfs uncurry So let's make it do it like that. Okay int map Come up. So it's gonna be an uncurry uncurry bfs to array I seem to be finding a shorter path Okay, so I go from zero to one zero Okay, I'm from here to one one another two one Okay Another three one c to r. No, that's not legal. Let me see here So it's g g comma uncare uncare print a uncurry uncurry bfs of g print neighbors G and I want to see what the neighbors of so it seems to go from uh Zero zero to one zero to one one Another two one and then what's going to three one So three one two two. Why is this say that? Why is it saying you can go from c to r? okay, so the value here of cohorts of graph Uh, so that's gonna be a Okay, so the xy value Let's see what this gives us for for a 2.1 I'm sorry. I'm screwing up this garden. It's somehow uh Pretty one So two one has value 99 Okay, so this is we're thinking this is two One it has value 99 now gci from in see Okay, yeah So two one has value 99 okay, um And it's being a little this length of two nine has value 99 now b as value 98 Look you go there two two This two two has value 99 as well. Yes now a One three has value 114 uh Wait, so one one has value 98. Okay. Three one is value 99 now. Okay. This one is Doing something wrong the x y It's almost like it's one coordinate system on one way one coordinate in the other Okay, but okay. So, uh, we're looking at a two. Yeah, okay. It's definitely So this was to be y and x. Okay, and this one should be y that's how it is in the So it is in the here So s y so let us say it's gonna be the This is gonna be the s axis And this is the s y axis Oh, it says loop s y just as well. Yeah, and what is the Three one hundred and fourteen this one makes sense. That makes sense Make it make sense. Okay print. I'm still getting 26 What the fuck? Okay. So what is the path it giving me? That's the link. Okay, so it goes a 00102021 2224 3444 5464 64 7473 6 0 6 0 5 0 4 0 3 0 and then go through 1 3 2 3 3 4 3 5 3 4 2 Okay, and this is the same It's the same This path reaches this one steps the fewest possible Let me see. So 1 2 3 4 5 6 Is there something we skip? We go 0 0 0 so okay 1 0 2 0 1 0 2 0 2 2, okay So we seem to end up here. Okay, but that doesn't matter. We go 1 2 2 2 And then we end up at a 21 So 21 20 to 1 2 2 And we're here at the same spot Okay, and then we go 2 3 2 4 2 3 2 4 2 3 2 4 a 2 3 2 3 2 4 3 4 4 4 4 5 4 6 4 7 4 1 2 3 4 5 6 7 Yeah, 7 4 And they also go here, okay and then we go a So 3 7 2 7 1 7 0 so we go all the way up. Yes Okay, and then we go 6 0 5 0 4 0 7 0 6 0 5 0 4 0 3 0 7 0 6 0 5 0 4 0 3 0 3 0 Yeah, okay So this is 0 1 2 3 3 0, okay 3 1 3 2 3 3 3 1 3 2 Ah 3 1 3 2 3 3 Okay 4 3 5 3 Ah 4 3 5 3 Yeah, and then we go directly up, but so there's something wrong with the So there's something wrong with the Ah can't see your video only It's being said Hello, hello Give me a second. I'm gonna go out and check the video status. The video is not coming through. Let me check This might be related to that crash earlier So I can see the video Like on my app So maybe The ones who can't see the video should Ah working now funny very funny Okay, so I said we could go directly to e but that was not true I think we can only go from z to e Let me confirm that in this big one. Yes. Okay, so we have to go from z to e A B z z z z z Z So this is one is going to be from enum A minus one It's going to be from enum Zed plus one Now a let's go a s val E val these are our hands S val equals from So we're just gonna go like this e val equals S val e val Dush. Oh no sx. Yes. Because now we didn't do here. We should say S val E val Okay, and we print the length of this over Just 32 Okay, so we just we don't count the final one, I guess Now let's see then a Let's do a read input input over do print dot minus one dot length F map length dot a Anker It's going to be minus one here actually Let's just see if this works for the example I'll try shows Let's see how it works for the save it first Okay, non-accessive patterns in sx is here again E is here Okay, so they're there but something is wrong here Cat maybe sxs Okay, yeah, so it's just empty Access is empty here Ah, but ah, okay. I haven't saved this Understandable. Have a nice day. Okay 449 All right We got part one finally Two hours if This is like they also want to turn To maximize about how you can start as low as possible. Have a nice day. We'll see what happens Are The tracks you should be direct taking the few steps to reach its goal So I'll need to find the shortest path from any square on elevation a To the square marked e Okay, now it's any ea Okay, let's see. Oh, good Maximize the quality of our the right way back Any square at elevation a I suspect the best way to do this is to start from e Flip the whole thing around and just try and find something that has value a Because you can see there's a lot of a's but there's only one A There's only one a That screws up the whole How many a's are in this? Okay, that's gonna be crazy, right? Okay. Um, let's just throw it. Do you see I 1635 a's it was still only take a minute and a half to do all Mm-hmm But so what we can do is we can be smart, right? So kind of every time we see a an a Just start again But that's gonna depend on where we start at least read it here a G comma s comma e Is to array Okay, and now a pause Or check all a's if you're only looking for The closest one Because we don't know which one is the closest one, right? a So what you want me to do you want me to find the so the thing is that like the On the path from the s to the nearest a it might not be So yeah, so what I'm saying is the the right way to do this is going to like start from e and just Walk a Take a valid walk like back steps until you find a An a but so we're just I'm just gonna see how How bad it is if I because I've already been here two hours lazy today a Now let's say a We didn't put a read file example Lines a Ls Okay, now we're gonna do a Add chords to all of them. So we're gonna say a dip with Okay, and then I'm gonna take the row number And I'm gonna do a Line and I'm gonna do a tip with zero Tip with a okay, and then here I'm gonna take the character And I'm gonna say a a c The a so That's this be this be this is gonna be y and then this is gonna be x c a x more y Okay, now let's Print lns Okay And let me see is e in the correct place here It's in five two. Oh, okay print. So this is correct. Um Okay, and then we're gonna go print a conkat concatenate then These are all the a's Okay, so I can do a bfs graph G and I could probably you know Break earlier something a We're gonna take a minimum of this. Okay, and it's gonna be if we Turn this into input. We'll take some time Oh, that is annoying Uh Intmap bang key eight is not an element of this map. I need to change this to input as well Mm-hmm could finish in three seconds. Let's see if it's correct. Okay see We could have done so much work But it took three seconds Okay I'm not gonna spend I'm not gonna spend an hour extra to Make it work And I think it's because you know, it's just it's quite fast I mean binary search Oh, I like that. Okay. Uh, but now we have an implementation of binary search, which is quite nice, right? um We had like sparse arrays and all like that Okay, anyway, I'm gonna let it stand like this. I'm gonna say a Task one Is gonna be They're gonna link them to an int to an int Take this as some ints to Uh, an int. Okay print task one. I should have to have that right? Okay, so that's task one task two equals now here. I'm just gonna go And then this is going to be going to sip the imp and we're not going to take equals zero we're gonna say And then this one is going to be minimum Dollar map maybe minus one bfs ga g g comma anything equals two All right, and we've cleaned out the code We wrote a breadth first search Which I'm guessing is gonna come very very handy later Because yeah, a lot of some of these yeah, I mean this is a classic all you have to do is breadth search And we could have like tried to rearrange the q So instead of always taking the beginning of the q Uh, we could have prioritized right so and that would be like and so try to go in the direction of the Queve and then let's see here where q equals Nice priority prioritize by guess q is q and it's the same here Just q comma q s q comma q s equals as we say The default one is a q comma q s prioritized equals q comma q s this is a Standard bfs now we can also write prioritized q s equals Something like a sort data god list. We're gonna say here here q q s equal sort on a metric you come to us a sort on metric metric equals so this is going to be that distance from It's going to be pretty in distance is Square root we're going to have said here a ex comma y equals and metric a t of x minus e x equals y and then we do square root of x1 times y1 No, it's not a funny end. Okay, and then we do Okay, so here It will do a priority q instead But it doesn't give the same It's not good. I guess this metric isn't stable Ah This one takes actually It takes a lot longer Okay, yeah, we can't do it this way because we have to kind of prioritize the total cost So we would have to look at like the cost of getting to the parent and then Okay, but But there we can write it. Okay. Let's say this is get and day 12. All right Uh, this was quite long today It would have been shorter had I not been confusing myself the whole time you teach you kids Don't have ins comma ins have like x chord y chord all like that Anyway, thank you for tuning in I hope the recording and everything worked and uh Yeah, see you tomorrow same time And yeah, I hope you're nice. All right. Thank you and bye. Bye