 All right Welcome to today's stream We streamed well We worked on the problem for three hours yesterday Day 10 part 2 we couldn't quite crack it, but I Yeah Had a hard time sleeping without it solved. So I think I yeah, I solved it Let me just show you What I wrote up on my Kindle scribe This is the one of the shapes. This is the shape of this one and whoop-whoop And what was happening is that? We were counting So so I expanded it right so this is like on a so each square here is Four squares here and what we were doing before is that we were You were measuring the Area of this whole thing, right? that's where that 26 came from and the Like the four you're like here, right? So there was a whole area But we needed to actually deduct The area that the curve occupants So a Felix 5 Who commented a lot yesterday he mentioned this right, you know this minus 0.25 plus 0.5, but we didn't quite manage to get there So then I sat down and wrote, you know, okay, so here this L here This is gonna the area of this L isn't gonna be one right and the area of any north or south is just two And then the areas of these So outward facing corners have an area of one but inward facing corners have an area of three And that was the trick we were missing yesterday. We needed this Distinction between outward and inward facing corners So I wrote some more code From yesterday you may recognize this is the green area code and It's quite similar, but it's just saying that okay now we're counting in force instead of 0.25 I didn't want to involve the folks here So then we add, you know four times why so just add everything above if we're going east But if you're going If you're going east into this and then you go up, okay You cover like half the area above that because you go like half east in this case and Similar here in the seven you're going a half point east, right? And then similarly for west If you go west we deduct everything above that right But for F and L we're also we're going a little bit west and then Same with north and south actually, so it's not just So if you're hitting the pipe directly, you're not adding anything But if you go if you go into this F here, you're also take going a little bit east and If you're hitting this seven here going a little bit West right so they add here and we Subtract here So we really follow the curve We make a shirt Thank You Kredlin This is my Indian camo shirt the kurta actually Anyway, I think we figure this out So then this calculates the area correctly But then we had to figure out the area of the curve itself, right? So this calculates the whole area now. We just want to see how much the curve itself occupies, right? So, you know like this here, right the curve itself Occupies some area So then I wrote that down here, right? So here this one occupies this so that the inner part It's gonna be two here Two units of this we could kind of buy So if you take this little J corner here So I looked at this picture basically, right? So if you're going if you're going From the scumming from the north You're going south into this one. You count one, right? If you're going West East into this when you count one and then so and then if you go You're going west into this L here. You count three, right? And similarly here if you go east into the seven you count three So that's just basically saying you're right eats into the seven. We're taking this kind of corner And then this one is going to be outside And that worked so It works for the examples. Okay, if we run this now on the examples it does give us the Right numbers for the examples and there was one more trick we had to do We had to also So what would happen sometimes so you have to go clockwise For this to work, right? You have to that's a Green's theorem Positively oriented, whatever you call it And what would happen in this big case here is that The s is somewhere in here. I think it's I Don't remember where the s is. It's one of these and Oh, yeah, it's here Okay, and what the s here would do is that it would pick this down here instead of going west So and then we'll go the other way around and then every all the area will be negative Which is fine, but then the Area of the curve changed because we weren't doing the right in the right order Basically, so what we basically do is that we just From the starting point we we get the two Paths it could possibly go and then we just compute the curve, right? We're going one way going the other way and then in the end if the area is Is a Positive then we went the right way around But if it's negative we want the wrong way around And then we had what we had to do is we have to just take the absolute value So the area is the same but to calculate the curve area. So then these threes and ones don't match up We have to go the other way around. So that's what we do And I think it works. So I'm very happy that the green steering approach works. We didn't have to do flood fill or anything So I run it Takes two 88 milliseconds, which is not bad. I think let me if I remove all the Running on the example, oh That was the whole code remove all the running on the examples and It takes 200 milliseconds, but it's a lot faster than doing some quadratic flood flood fill stuff I think because it's just a It's just greens here. We're not even counting anything. We're just going around the curve Now, let's see if it works And then we can move on to day 11 351 come on submit Oh What you don't seem to be solving the right level did you already complete it? Huh? Your presence or 351, okay So we got it. It's just some weird thing in the submission All right day two day 10 part two done now. Let's move on to day 10 But yeah, so the lesson of this kids is If you're not getting it right Don't keep hacking and coding just uh write it up. All right, let's see today to do Let's do day 11 I haven't looked at it yet. I accidentally clicked it earlier, but Eh I think so the word of this radio is that this one is not as hard as day 10 part Day 10 part 2 so hopefully We'll do it Won't take too long, but We can also look at the stats And we can see that It's dropping down fast, but a lot of people Not many people did did only ah, yeah, okay. Sorry. We can't see here that A lot of people did day 10 But a lot of people also didn't complete part 2 Same as here, right? With part 2 of day 5 But most people are completely pitted part 2 of day 1. So I think we're good Let's go for day 11 Anyway, I like this puzzle and I really liked that we could use a actual math Green serum Uh, it just took us a while to discretize it correctly um, but As I said, this makes my Three years of Hardcore calculus Bachelors all worth it because we solved one Added a code in a slightly faster Which is not bad Okay, so Now we're looking for hot springs come across some observatory Okay, he's studying cosmic expansion Okay, he's just working on his research process visiting. Okay Okay, maybe you can help me out with analysis to speed things up The researchers collected a bunch of data and compiled the data into a single giant image The image includes empty space and galaxies A researcher is trying to figure out the sum of the lengths of the shortest path between every pair of galaxies Wow Okay Okay, 36 pairs For each pair find any shortest path between the two galaxies only steps that move up down left or right exactly one Star or hash at the time Okay, so the third path between guys allowed to pass to another galaxy For example, here's one of the shortest paths between galaxies five and nine Take a minimum of nine steps to get from galaxy five to nine Okay, this seems to be doable. Let's say Let's look at the input the input is going to be crazy big With a lot of Ah, okay. There's not so much empty space Let's copy paste input doom doom doom Day 10 good stuff. Let me just commit get at day 10.js get at x and x star Get that example Get that input I'm missing something now get commit them Day 10 part two Let's go to day 11 Day 11 Oh Touch day 11.js touch Now let's just paste the input here and Let's also paste the example Okay It's like a classic path finding problem, right? Okay, uh, let's catch the universe bend in time to deliver surgery Okay, any rows or columns that contain no galaxies should actually be twice as big Okay Doom doom Up down left or right? Okay, let me close this picture boom boom boom Now let's close Others. Let's see Okay, example. So what I really want is just a Strings to list of pairs of ints And I want I want As I want to know the bounds Okay parts example Equals okay So first of all, we're gonna see we're gonna do the empty lines Which is Where is empty? For all Equal to dot Okay, let's say Let's not give us a type yet Let's just say a map is empty Um main IO main equals Read file example So this is going to be okay like a solution where it's going to be easy to do it the naive way But hard to like optimize it I think print dot bars example bars Example ring doom boom Dot lines right now gc day 11 Okay, 11 time 11 false false false true false false. So those are the empty So let's just see a so we could do some hacking and just say if it's empty then Counts as two steps but Let's process Add empty And add empty Row if empty then If the row is empty then rr R then a rr Add empty rsls R I think I can do this Maybe I Shouldn't But ah, okay Better do it the other way Is it and then Add empty Okay So we added a copy of the empty row now um Add empty and then Transpose dot add empty And then we add empty again variable underscore transpose It's actually gc 2021 Uh import data at all lists Okay, so we added the empty And then let's transpose again Just to make it so we can print it nicely Okay So we got the same Okay, so we've done the first part of the task Now Let's see I have a feeling I think that they're gonna say, uh They're gonna say that Like part two is going to be like, oh actually um Actually the it's It grew a lot faster or something so the empty rows are going to be Not just one extra one, but like a million Anyway, um Find indices So these are all the galaxies And now we're just going to get the coordinates zip with So we're gonna take in the index and the list And we're gonna map they were the list Okay, and now we just Concap these So parts example List of strings to a list of Okay, now we have all the stars This path is like nine because it takes a minute of nine steps to get on galaxy five To galaxy nine Zero one two three four five It's this one Seven Okay, so six one to get to 11 five Now we want the Distance on a grid because a I think it's called a taxi cab distance Exactly You know, I'm really I'm really uh I'm really working my I'm really working my math degree here Okay, let's say The Manhattan distance x1 minus x2 Dist x1 y1 x1 x2 y2 to abs x1 x1 minus x2 minus a plus abs y1 minus y2 Okay, let's say, um Part one where equals So stars is equal to a where uh Chords is equal to parts example stars Now let's say see a Chords Five Let's just print print dot aren't print dot part one Right. Okay. So you want number four here? Chords nine eight Okay Hey Felix Good to see you again we solved Day 10 using green's theorem Let me just show you the explanation again We use green's theorem We counted the area but What we forgot to do was We forgot to deduct the area that the curve occupies And it's not just It's not enough to just say okay the corners take this much So the sides take 0.5. That's exactly like you said But if you're taking if you have an outward corner it only takes 0.25 And if you have an inward corner it takes 0.75 So by Using green's theorem to compute the area of everything And then going through the curve again and looking at Depending on the like the orientation of the actual curves Whether they were inward pointing or outward pointing we would calculate the area of the curve And then We could make it work So we took some time I had to sit down and write it up But we got there eventually Which is good by the way, I hope my Audio is still going strong Anyway, and we turned out and then we had to do another trick. We had to So what was wrong in the big example was not just the curve. It was also the fact that we were going the wrong way around So to make this calculation make sense you have to go clockwise Because for the if you go counterclockwise, you know the There's a difference between the whether it takes one or whether it takes three depending on the direction you're going So we just calculate and figure out which direction we're going through Figure the area of the whole thing using green's theorem Um and then deduct the area of the curve Et voilà, it worked I'm quite happy That we could use my math degree to search good rules But okay, let's see and now we're doing day 11. We're finding distances between points Um Actually, I'm curry this And I'm thinking we can do the taxi cab distance Between two points um So between galaxy and galaxy one and galaxy seven is supposed to be 15 let me see So this was five and nine So let's just see We don't need to do any path finding, right? Uh, this is galaxy one and Seven Three and six eight and nine so three and six and Eight and nine 9 15 17 zero Oh, that's not correct. Let's see 9 15 17 5 so we are just taking that distance taxi cab distance That's good Um, so we need to now take the So to find every combination Of two things I think in haskell you can do something like This so you take a and b come on b Slide over uh one two three And then applicative four five six And let's see fmap I think this needs to be why is it not doing the applicative ggi Did it out of applicative What is going on here? It's in the prelude What oh, uh, okay a b a Come on b So one one one three Two one two two two three three one three two three three Yeah, so exactly so Uh, 0.5 inward Exactly what you were doing this but your solution was probably easier to program because you only need the previous interaction for the corners Yeah, I mean so I did I did do that I explained that earlier that a So I started counting everything as a it's a block of four Because we were and then we imagined the point, you know zero zero it's going to be in the middle of the zero zero block, right? and then And then so whenever you go down you're adding two blocks like half right when you go down your When you go up you're you're you're adding as well Um, so it worked out Okay, so now we're just gonna do um, so what we're gonna do is we are gonna We are gonna say and We're gonna say this and we are gonna fmap that over chords over chords And this will just work Because the distance between so we don't have to like filter out the galaxy to itself Because it's going to be a zero do do do seven four eight Ah, and now we did everything twice Because the order doesn't matter. That's true. Okay, so we just divide this by two three seventy four exactly As it was supposed to be Now let's Do this on the input And let's see if it works All right What extension is deducing the type? What do you mean deducing type? ah This is just a hasco language server HLS And it's in You can make it work for whim or emacs But it's very nice for vs code Because it just has these nice That's what kind of that's why I started using vs code actually Because it is quite nice Or the hls extension because you can just put in like Hoverers and it can do autocompleteness stuff. It's quite nice Okay, now let's see Uh-huh. Yes, exactly. Now instead of the expenditure before make each empty row or column one million times larger And it's each energy will be replaced with one million empty rows and each empty column should be replaced with one million empty columns Okay, so, um, let's see a We just have to change the parse example And because we're using taxi cab, you know, it's not gonna It's not gonna take it's not gonna be very bad so, uh part two is gonna be let's see Part two is going to be the same Part two except we're going to parse example two and here parse example Two Okay, and we are going to We're not going to add empty Uh, we need to add the empty columns That's just um Let's not add empty. Um Do do do do Let me just do this again. Okay read file Example and we're just going to print Uh map m range dot parse example two And uh, okay So is empty is the same so let me see, um So a so empty rows is equal to So we're gonna say a sip empty in sip. Okay, and we're gonna say a filter second and then Is empty and we're gonna map first so we are gonna say a empty call rows equals empty ins Okay Let's leave this for now empty rows, so let's see empty ins Stairs and empty calls is equal to empty Why is it like this? This is all wrong empty rows calls empty ins transposters And this should be a function. So it should look like this Camel case mixed casing. That's good. Okay. Let's see. Okay Now let's just print empty rows empty calls Okay, um We're getting two five eight. Well, how are we getting that? Um We should be getting two things, no empty Uh, we're map and printing. Oh my god. This is that stupid change that gave pairs like a thing Man empty calls three seven two five eight Okay, now let's find the original indices Uh, which we do with exactly Exactly like this Oreg in the Stairs Okay, so these are the original indices um So zero three one seven two zero Now, um, we're gonna We're gonna add to the The empty rows. We're gonna add to these Um, so let's see Hey, let's see add Add two So for everything that's after three we want to add a million And for everything up after seven We want we want to add Two million Okay, so let's just see here. Okay add two It's just a So if we're not adding anything We just do so far. Okay add two, uh, so far x axis add to Roll so far x s is Okay, so we're gonna say here Um map so far So far we're gonna say x comma y Let's see Okay, x comma y We're gonna say If x larger than e c then x plus one Million y else x comma y Okay, and this is gonna be so far prime where So far prime is equal to so we map over them You modify all of them But now we also have to Say Add to row So far prime and then we also have to say Plus one million To the rest of them map plus one million x is Because we want to because I don't know all of them are gonna be like they're gonna have the they're because they're gonna be Yeah, so because we also modify in this off the empty row, right? Okay, and I had to call so far prime this is just if Why is legal larger than I see then? Let's see here. Let's just And add to call Let's write them out actually or egg or egg ins So the eight there is a faster way to do this, right? We don't actually have to modify all of them. We can just say if they are they are in a certain location that that We would add to them there Let's see add to call or egg ins a Empty Rows This is why we also name it add to call so add to row Okay, and add to row of this Add to call column This of a Empty calls So, uh, yeah, so it's after the first time it was there's a million there Million Yeah, I think we're good Mm-hmm Okay, now let's just make sure That this is all a Part example like this And it's a list of integer comma integer Coordinates just so we are not Integer integer or egg in's Find indices Okay Do integer And now it's all integers And we do Part two it's gonna be an integer and Dilly-dilly-dilly-dilly-dilly Let's see Let's let's first check on the example actually Oh, this is the example. Yeah, okay Hey, so and let's also take in this number D Let's just One let's replace this million with D Okay, and Part two D Yeah Now let's say part two In the example above each of each number will merely 10 times larger The sum of the shortest path between every pair of galaxies would be 10 30 Is it because we Do maybe it's 11 or nine Okay, we got something wrong Here, um, did I do a larger than equal do? Mm-hmm We are quite close though Right, so we have to add D minus one D equals D minus one D prime And then we should add here 10 10 30 and then let's print it again if it's 100 and it should be 8 4 10 now So the input one Million Let's say All right We did day 11 Not too bad today I think we figured out We were good at finding the generalization. So it worked out quite well And the trick was using the taxi cab distance right if we would gone If we had been like, oh shortest path, let's do this path finding It would have been a mess actually Anyway, uh Thanks for joining in. I hope you liked day 11 and let's see how day 12 goes but again I'm quite happy with That we made we were able to do t 10 part 2 using green's theorem Check out the youtube video Like so the beginning of this video for the explanation if you want to see it It's going to be on youtube tomorrow in 24 hours and It will I mean the video on demand will also be here so All right, thanks for tuning in today and see you tomorrow Or day 12 Okay