 All right Welcome to today's Item of code sorry about the late start What we were doing I Just kind of forgot I was into I was deep in thought and then I'm like, oh, no it's a Oh my spot if I moved over to my phone Which is not what I wanted right should be good But let's get started with today's problem. I Haven't seen anything on it. Why is it playing? Sorry about that All right make dear day seven He did a let's just do code day seven to that work The meantime, let's figure out why my computer is so slow today Oh no, I close the wrong one Like 100% CPU oh, it's Wow, okay, let's close this one. Let's minimize this one quick talk our desktop. I hope Oh, I probably have skipped 1% of the frame so far That's not good. Let me just Work on this a bit this is power toys In form voice meter All right, everything seems to be in order. I hope the audio is good And I hope we don't get too many interruptions And we're all right. Let's look at day seven Hey little Annie always there for the stream. That's good Let's see. They're treacherous whales day seven a Giant whale has decided your surmises next meal as much more pressure. Okay. Oh, so we had lantern fish last time And now we have a whale all right, but it's just warm of crabs And Okay, they're in a tiny summer. Okay They seem to be preparing to blast. This is a bit farfetched, right? They seem to be preparing to blast a hole in the ocean floor Senses indicate a massive underground cave system just beyond where they're aiming Perhaps if anything, it'll be a line before they have enough power to blast Okay So we have a crabs and holes and something However, it doesn't look like they'll be aligned for the whale catches you. Maybe you can help There's one major catch crab submarines can only move horizontally. He can quickly make a list of the horizontal position of each crab Crab submarines have a limited fuel so you need to find a way to make all of their horizontal positions match while requiring them to spend as little As possible. Okay For example consider the following horizontal positions This means there's a crab with horizontal position 16 a Crab with horizontal position one and so on Each change of one step knows of physical okay You choose any horizontal position to land them all on But the one that cost the least fuel is horizontal position two This cost a total of 37 fuel is as cheap as possible outcome more expensive outcomes include aligning a position one position three and position 10 Determine the horizontal position of the crabs can align to using the least fuel possible Okay, so this seems So we can see that like for the real input. There is a lot of Like a very big space, so let's just see if the naive way works for now and then roll with it, right? All right New file Module Where and let me say example is a list of ends Example equals Okay, so the naive way to do it. It's just to go over all the positions and For each of them. Okay, but like we don't have to do it for every number, right? We can at least save us that So let's So what we're gonna do is that we're gonna do GCI GCI day 7.ages Right All right, let's see. Let's reload this now. Let's say example. Okay the group, but then we sort example Group Import data at least Module Let's do task one takes me list events One equals Define where G is equals We're basically doing the same as last time, right? We do We do map G equals map X So we have We're gonna list here list add X Goes to X comma length Over group sort example Yes, so let's print this Let's just do that right the sort input and this is gonna be in And then we're gonna say print task one on example And let's see ask one example Okay, so we have zero number ones one number two and Then we say Okay, so and then for each of the positions in that list We'd let's do that. So pause This is equal to map first Yes So those are the positions and then we say a pure cost of P It's equal to map and P prime and then to ABS of P for the difference between the two right P minus P prime on Pauses yeah, so let's and then we map this P prime. We're gonna map that There'd be pre prime comma L We're gonna say L times So the DC these is We map this over pauses But we're mapping this over GS. So here we say so now we have okay Modest the type of fuel cost. It's a list of int P comma Okay, and then we are gonna say map fuel cost pauses And we're actually we don't actually need this so it's just gonna be that some Not this map so it's zero to 49 one is 42 Position three is 13 precision Position three Right, so we're not necessarily We're not yeah, okay It's not necessarily cheapest. Okay, so then we say So it's probably gonna be in the list right so min MP is gonna be mip. It's gonna be min pauses Max P is it gonna be Mxp is gonna be max pauses and now we're gonna map the fuel cost over mip to mxp Zero one. Okay. Now we get all the positions What if we Maybe we just sort it sorts on that probably sorts on the first one. Yeah, sort by a Sort a Compact on D okay, so it's we can sort by comparing on We don't need that we say By 237 which is the one we want Determine the forest on the position that the crafts can align to using the least fuel possible How much will miss is meant to align to the message the answer here is 37? Let's grab the input again. We're just gonna paste it ask one compare on map fuel cost mip max Okay, so how long does it take for the input? Okay, didn't take too long actually, but Did take some time, right? GT oh Oh three We could so what we could do is do like a some sort of sliding Window where cuz kind of it like if you know how much it takes to move to this position, right? It's gonna chase not gonna change too much, right? But you know, we're at 25 20 20 milliseconds, so Not too bad Let's try and give it three five two two five four, let's try to give that as the first answer All right, we got it now the crap sounds even interdicted in the proposed solution. Perhaps you must understand crab engineering Okay, they don't so instead each change of one step and also the position costs one more unit of fuel than the last The first steps cost one the second step costs to The third step costs Three and so on So it's like the sum So because of the the end step costs and so we're summing from One to n Which is n Times n minus one or two, right? So as you grab more moving further becomes this change is the best for some position Okay, so that's it. Let's go back to the Okay, so this is the one that we want Dtd dtd I don't think this is gonna be too much because I think we can just use Okay, so now we're gonna say a we're gonna go back to this one And and we're gonna run it on the example ask to example So, okay, this just becomes a five. Let's see So here instead of being the length of the abs. This is actually okay, so f of p Fp at p prime comma l So the function is going to be l times ABS p minus p prime so this was the distance that they have to move Where D equals and the blank now becomes C times D minus one. I think it's D minus one Over two divided by two. Yeah, so let's do it like this Let's just make sure all from our trusty Some one two and yeah, it's oh, it's Plus one not minus one my mistake. All right, and now we are not mapping the Fp function. Okay, let's see So position five is 168 exactly and The old one to cost two hundred six two hundred and sixty. Okay, so we are still We have the right one and we just changed one thing And it's like it's not even It's not that bad. I forgot to take me. Let's try this We got day two as well. Let's go back Okay, so today's was pretty easy And you know, okay, we could probably have done it way faster Using some dynamic programming things But you know at 25 milliseconds And like We don't really need to write I Wish there was a function that did this Let me say. Oh, no, I changed it, but I yeah, I think you know, we did it But I think like we really Is sort by group Had really changes it right because I think like there's not that many unique crabs So yeah, so we need to compute it for a lot of positions, but like we're not computing it for that many elements Right, which is Which is what is important here Yeah, sometimes you get lucky with that data distribution, right? And you don't have to do that much Sometimes it You become you're unlucky, right and like your approach would be good with another distribution, but because it's Well, this one turned out to be really long, but you can also say like if these numbers were massive Then we would be in trouble, right? It's really what's really saving us here. Oh my god. There are too many numbers What's really saving us here is that they're not very distributed because so like it's at the at the order of We sort them that's fast grouping them it becomes fast also You can also tell like, you know, these numbers are very similar, right? So we can probably Like by knowing one we can probably kind of keep track of something and figure out in the second one right away In fact But this one was too fast. So, you know, there's really no point Do we need this? Yeah, all right, I think We'll just call it quits today I mean, we did it very fast And I you know, there's not really much that to explain, right? I think I think that's what we should say, right? You know, if it would if it takes us like the last time or two days before or something Yeah on Sunday, right it took us Like it was taking us 20 seconds right and that's that's a bit much, right? But How did you do it Fabian? What was your How did you cheese it? You just computed it because I think you can Yeah, I think you can get away with it here Yeah, exactly. It's also like it's not that hard of a problem and I think like Yeah, especially if you know this formula for summing numbers one to n, right? Then it it it becomes very fast, right? But yeah, I mean, yeah, let's make this a Let's make this generic task. It's gonna take an Into a function right and then there's the int It returns an int comma int task dt and int equals dt and then Task two is just a task And then we just put this formula down so this one and instead of doing this we say dt I'm gonna minimize the code here. Let's see and Task one is just a Task in like we don't change it and task make sure we didn't They a little Gauss Why do you mean a little Gauss you call it? there are What is what is big Gauss here is our stuff the same office, right? I guess we Know it takes a bit longer, you notice, right? It's not 25. It's like 50 and that's because a Yeah, I think it can't inline things this hard, right, which is a bit funny, but okay, and Let's see how can we make this can we make this faster even now But yeah, it's not gonna speed it up, I think oh Yeah, yeah, yeah, that is true. I remember that legend like is He was like three years old. He was supposed to be helping his dad Do the accounting first company something like that dad Gauss man He crazy Crazy smart, okay, and All right, now we do both of them in 97 which is worse than it was before I think like I said It's because of the inliner can't be as aggressive But you know, it's pretty good, you know Yeah, I don't think that's much to dive into today, right? It's just Group sort And then we kind of count the number of each group Why we trace show ID On this I'm just wondering like how much we're gaining here Trace Yes, yeah, okay, so we take it down from how many inputs we have We have a thousand inputs and we change it into 657. Okay. I guess that That sorting thing is not what's saving us here Maybe it's just the Gauss formula. I mean we go from O of n to 01 Just using this Gauss formula Yeah, I don't know. Yeah, we cut the time down in half, right? but right guess we could also we could also like memoize the function because Instead of like yeah, so just like when you see a number you can be the before just return it instead of Compute it again Yeah, okay. This is so fast that we're not gonna we're not gonna bother. I think We get both of them in 79 milliseconds Yeah, and I think it's because we keep it at like all of n Or all and log n right with group and we sort and then No, it's not all of them log n sorry It's the O of And log n a plus Then well, no and then O of n Like a reduced n but not that much Times the difference between the smallest number and the largest number Right. So if we put, you know One million here it's going to take a lot longer Binary search could also work. Yeah, that's true We could do binary search on the list, but yeah, I can see that it's really dependent on The numbers not being too big. How would we do binary search here? That's also the thing right we're in a we're in a List lazy language, right? So it would be it's a bit hard to do binary search Because we don't have random access to the array, right? Yeah, I think this is also like a case of you know premature optimization, right like If it oh, no, no it happened again You know if it takes 79 milliseconds like you know, there's no there's not much point to trying to optimize it too much, right? You know unless you're doing this again again again a million times, right? There's probably another part of your system that takes longer So, yeah All right, I think we'll just keep it short for the day It oh, thanks for the follow my shoget Yeah, I guess will be I'll be aligned again tomorrow at seven At seven thirty so yeah, cuz I've been happy I'm usually going at seven but I try to do seven thirty I was gonna have the option of doing floor bomb, but Yeah, then I bailed. I don't know and What am I thinking yeah, so yeah See you tomorrow, right and Let's hope we get I cuz I like it. You know when our kind of first illusion I Mean like yeah, but our first illusion is a bit hard, right? This one is just like oh, let's just do it, right and That's also like kind of what I discovered about advent of code is that you can like sit down and think really really hard and Make it go super fast, right? But it's also a competition about programming speed like how fast can you do it, right? And I think like we were done at 18 minutes, right? And It finished right so we have to balance like Running running speed against programming speed. So this is kind of the the way I would do it in a company, right because Yeah, you can spend you don't want to spend too much time on something that Yeah, oh yeah, I don't have a bot but the keyboard is a the varmillo and It's actually custom-made in a sense the keycaps. It's Icelandic flag, right? I'm Icelandic best country and Show you I have two keyboards actually I Have this one and then I have the Norwegian one right but they have different buttons. So like this is how they come But they look like the Danish flags And But we don't like Denmark because they were our overlords what I did is I brought like from varmillo like a Greek Greek flag thing which is like white and blue But only the keycaps right now, then I put some of the blue ones here to make an a region flag and the rest of them To make the Icelandic flag And You know makes me feel better that I'm not supporting Denmark. I mean, they're they're cool. Legos are nice, but They were our overlords, so we're supposed to have a bit of a beef with them, you know, it is Pretty okay. Wow Fabian Yeah, we have 300 400,000 people approximately right we're doing pretty well compared all right Too much jibber jabber. I'm gonna have dinner See you guys and girls tomorrow At seven I think also the videos have been coming up right I think yesterday's video Should be up now and then this one will be up at In 24 hours right because there's an embargo until then Yeah, it's fine. All right, so much For today and thanks for joining in and Yeah, make sure to follow you get a notification and go live Go live tomorrow at seven And yeah, it's nice to have some people in the chat, right? It was more fun like the first day. We had so many people in chat. We were like discussing different ways to do it So, you know, I love for you all to you know Go ahead do the ad with a code and then join us In the evening right where we where I kind of do my attempt live and then we discuss How to do it differently, right? Because I I think that's the most fun part right like because you have one idea and And Someone else has a completely different idea. It's really fun to kind of discuss like how can we approach this problem, right? But so far it's been kind of straightforward like, you know, there's no crazy way we can do it. You just do it the way kind of the obvious way All right, but yeah, I hope to see you Fabian Little Annie will probably be there and And my boy Jonathan He joined sometimes all right See you tomorrow and take care You