 All right, welcome to today's Advertisement of code in Haskell. It's day six Took us three hours yesterday. It was quite intense. I Mean we got to a solution in like two hours, but then we did an extra hour and we Made it like 80 times faster So we're gonna see How we do today I don't know. I Haven't like So apparently task one is not too bad, but task two is worse is all I've heard But yeah We will see Let me just check One thing real quick I'm just gonna see the Dream manager Yes, our pitch rate is good. All right, let's check it out day six. All right, so yesterday and We we were like figuring out some clouds some like poisonous clouds So now we're getting pretty deep right and it's getting steeper Okay, a massive school of glowing lanternfish swims past They may spawn quickly you search large numbers. Maybe exponentially quickly You should model their growth rate to be sure Okay and I seems like Santa is just Finding random things to do actually Although you know nothing about the specific species of lanternfish you make some guesses about their attributes Surely each lanternfish creates a new lanternfish once every seven days However, it is processed and necessarily synchronized between every lanternfish One lanternfish might have two days left until it creates on a little bit longer before So you can model each fish as a single number that represents the number of days until it creates a new lanternfish Okay, furthermore you reason a new lanternfish which really needs slightly longer before it's capable of producing more lanternfish Two more days for its first cycle Suppose you have a lanternfish with internal time around you three. So yeah, it's a two case. Okay, so you choose one zero Then it goes back to six and it creates a new lanternfish with internal timer of eight After another day the first lanternfish would have an internal timer of five and the second lanternfish would have an internal timer of Seven a lanternfish that creates a new fish which is the resets its timer to six knots seven Because zero is included the valve tower. Okay, the new lanternfish starts with internal timer of eight It does not start counting down until the next day Oh Realizing What you're trying to do this summer not only produces a list of ages of several hundred nearby lanternfish year puzzle input For example suppose you were giving the following list 3421 this means that the first fish is until number three the second head is shashed internal timer of four And so on until the fifth fish which has an internal timer of two Simulating this fish over several days would proceed as follows 34312 Okay Yeah Okay, so each day is here become six and adds a new eight to the end of them Placed while each other number decreases by one if it was present At the start of the day in this example after 18 days are total of 26 fish After 80 days, there would be a total of five nine three four Find a way to simulate lanternfish. How many language would there be after 80 days? Okay, so the input is pretty simple We don't really have to parse much today, let's say module main where Example let's just put it right in there And you know for the input itself is gonna be a hundred That's not too bad. Okay, so the first thing to realize is that a Each of these are independent All right, so We can calculate like how many the first Fish that starts with three will give us And how many official starfish four will give us? And three and one and two and then just sum them together Okay Right, so it's a three two four one and then so each day is zero becomes a six and Adds a new eight to the end of the list. Wow. Okay. Let's see so let's go for you know like a Lantern fish So we have a Internal timer and we want to simulate it for a number of days Are you gonna return it right? Okay lantern fish? So it's a timer and then days Okay So so Lantern fish So no matter the timer if the days is zero it's gonna give us a Put that here lantern fish underscores of the days is zero That's gonna be one fish Hello, little Annie Hello Bella Hello, Bella And we're working this out. Okay, it's like facial zero. It's gonna give us one. Okay Lantern fish The timer is zero It's going to give us a Days So lander lantern fishes the days are says zero is gonna give us Lantern fish Like one right six days plus lantern fish eight days Okay Otherwise If it's not zero it's just gonna give us lantern fish timer minus one days days Minus one. I think Let's have it the other way around. Okay zero days Days zero Okay, let's compute this for GCI D6 adages return Let's compute it like this now. Let's just map Lantern fish and let's see No All right, so we want this to be There's zero days left. Yeah, okay We want this to be Days and then we have this internal timer six and days and eight So what we're gonna do is like we're gonna do this right and then we're gonna memorize it to make it faster But first we check we can just do this We reload it now lantern fish we're gonna map Lantern fish 18 over Example some That's 15 Each day a zero becomes a six and adds a new eight to the end of the list While each other decreases by one if it was present at the start of the day This is maybe I think it's all right, so we decrease the days as well. Now let me get 14. Okay, wait Do-do-do-do-do-do-do-do-do-do So at the end of day one, so why does it become So lantern fish 18 three why is that one? So zero should become one. Okay now 18 Zero is lantern fish six plus Lantern fish days. Oh, I forgot this 26 All right, so I think we are getting there now So okay, so we're gonna be able to Compute this pretty fast Let's just get the input, right? Let's just see if it's Super slow, I don't think this is slow input Ain't input equals quite long this input, right? But first of all, okay But let's let's do some premature. So I don't think we need to memorize the function so much Um, but like like in this input, right? We can sort it like we can just count the number of ones and We count the number of It is quite long It doesn't matter So we can so we don't need to do it for all of them, right? We can just kind of do it all at once So like yeah once for each one. Okay, so this is that we have a lantern fish function and then we're gonna say Lantern pool is gonna take a list of ints and return as an int Lantern pool is going to say Group, oh, yeah, we have to we should have the days as well here. So we're gonna take the Inits Inits and then days So we're gonna say a sort in its We're gonna group them and we're gonna import it at least So Where in its Okay, then we we have like a groups and so we're gonna say We're gonna we're gonna let me just see here undefined So, let's just look at them, right? Okay, so we reload. Oh, yeah Group in its right. This is gonna be infinite Group, okay, so we have a group of sort input All right, so then we are going to sort them I comma I and else is equal to map a We're gonna take and we're gonna take a list and we're gonna say We're gonna say here and it's x comma a ling of L group Right, so we take this and we map the L at x underscore to x comma length L So we and then so what the thing is that we could just multiply by these, right? Let's see. So we group the eyes and L's then So so num per pool is equal to This takes in the in it and the The eye and the L and it's equal to lantern fish And then the it stays and then I and we multiply by that by Oh Okay, and so number pool is an int so we do a some map some num for pool Some map number pool group No, I and L's Lantern pool. Okay, then we say here a Example and 18 26 now you say example 80 5934 Okay Now we do it for the input. It's not bad Three six five eight six two now. Let's say here print lantern pool input 80 and let's GT oh three day six Compile it Time day six Okay, it's six milliseconds. It's not taking any time And we check if it's correct. All right, you got the first gold star Are we gonna do be quick today, but I was told that the first one was quite fast But the second one was harder. Suppose the lantern fish live forever. I have unlimited food and space Would they take over the entire ocean? After 256 days in the example of other be a total of Okay, I think we can do this. We just have to Make this into integer num per pool is Going to be so this is gonna be from integral now. What's wrong here? Num per pool? Num for pool takes in an It's a list of int and int Intergr, okay, and this should return an integer Now the types are correct Time day six. Yeah, so now we should have integers everywhere and Okay, so this might be slow, but we'll see Okay, it's taking quite a long time to run But we're computing it once per day, right? Do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do That's a not running for the input. Let's run it for the example first. I don't think it's any It's not it's not gonna be that much longer for the Okay, we're gonna have to memorize here Let's just run it. But so I think it's because like the six on the eight We're computing that for all of them, right? We don't really need to do that Do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do-do Nice to see you again Good luck on this problem Thanks Uh, I think we need to memorize things because like the naive way of doing it is It's not going very fast how about we unsafe goers and i think there's like a maybe it's just like a function we can just call the memoize i'm wondering um yeah it's also like not multi-threaded or anything right and it's taking up all right let's just have it carry around right a map of int comma int here we don't care about it here we don't care about it here we don't care about it um but i mean okay actually we do care about it import qualify data map map as map okay now lantern fish so it's actually going to be here um lantern fish okay so i might uh yeah it's gonna be fine lantern fish um it just can take a slanted let's do it for the lantern pool lantern fish takes in an int and an int and returns an integer now lantern lantern fish equals lantern fish prime map dot empty empty lantern fish prime where okay so this is a memo so we do first here case um days days comma zero memo of just lantern fish prime map int comma int integer two int integer okay if if we have an answer memoized already just rest and we turn raise so if we don't have it then we say um all right and we have to return the map again right map int comma int comma the girl comma integer lantern fish uh so this is gonna be a second uh this is gonna be a memo now we just return memo comma one okay so if so if if it's in the map then we just return memo comma reds okay otherwise we say uh we say uh let memo prime comma r equal to lantern fish prime memo days zero now wait days uh days minus one days minus one uh six memo prime prime r2 you'll do lantern fish prime memo prime days minus one eight and then in a map dot insert map dot insert days comma zero into and then it's r plus r r plus r2 memo prime prime right r plus r2 do do lantern fish map empty and we take a second you can imagine we can map map map mean this one takes a map okay yeah okay sorry i always go up the order here so if then this one is an integer then we just return memo reds otherwise we lift that toe up we press it along okay this should be redundant bracket and then this this is a memo and this is just a lantern fish prime memo let's see if it works lantern fish prime in line 14 what do you want so it's int int right so this is a a day's eye this is day's eye why was it complaining i don't know let's run it for the example two six nine eight four four five seven five three nine five six seven three nine yes input i think we got it four milliseconds thanks to memoization all right that was pretty easy uh well it wasn't easy but like we knew that we just memoized it right so you went from like it's not even done after 11 and a half minutes to four milliseconds and why well the memoization really means that it really means that we we really it's called dynamic programming right we really we're really taking it from like an exponential problem down to like a linear and like blah blah problem right by just kind of computing once because otherwise what we were doing is like we were computing again and again for the same values and now we don't do that so we can actually probably make this slightly faster by by like by like folding here all right so and then kind of use the same the same memoization but i mean four you make it in four milliseconds or one millisecond zero it's not bad right and we did it in 30 minutes so i think it's a pretty decent problem today or decent solution at least okay uh yeah since this was so quick let's see what can we can we do it yeah so i think like this is also like you know if you're going for like an interview or you're like doing programming competitions um this is usually how you do it right you memoize because a yeah because they they just love to give you kind of exponential problems that are super quick if you memoize and functional languages make it pretty easy to memoize so because we just you know we just passed around a map right and then we're looking up in the map and then like we compute one thing with the map and then we compute the second and it's like and then we insert the answer we got uh into the yeah and that's like that's what we return from days of zero right and then it just works right yeah i'm actually impressed that it just worked though i uh i was expecting to have to deal with some trickiness but i think yeah i think this is a solution we can be happy with it's very inconsistent but it's like sometimes four milliseconds sometimes 10 but yeah like we're at that point where it's like it's so fast it doesn't really it doesn't really do anything right okay um let's just do the fold right and i remember which one it is but yeah so we're gonna say uh that so here then we don't we return we return the map a type a memo is that's just equal to this memoization map and then this is memo to memo and then this is so we return the memo okay and then number pool uh so this is going to take in a memo and an int comma int and return an integer and we're going to say lantern fish let's actually have it like this uh but we just kind of expose lantern fish prime and then we just call we say here this is memo and lantern pool lantern pool m so and then we just say this is uh the function we're gonna fold over here is gonna be memo and then into int and then and it returns an integer now ghi what's the type of folder i don't think we want to do like this it takes like the the current memo value and then and returns memo comma integer and we're working at g type fold l prime and we're gonna do folder okay uh which is me just means we do like this so say fold r number pool and then we say map dot map dot empty comma zero ding ding ding ding and this one is and then we take the second of that now we do here uh this is gonna be int comma int and then memo curve so we say where n res is equal to so memo prime comma r is equal to lantern fish prime and then it's going to be memo and it's going to be days right and it's gonna be okay so this is the result the new memo and the result so this so these this is going to be the new one is going to be memo prime and after like with this we don't even have to do the group and sorts right we we just look up yeah but so it's going to be memo prime and it's going to be uh curve plus from integral l times r now let's reload now let's do lantern pool m here let's see if it actually works i mean it works but the the other one was so fast that we don't really see any difference we're like at the limits of how like just timing with a bash command right ding ding ding ding ding ding ding all right so now we do it uh we can also use past the memory session in the new one so i think we'll just call it quits after this i'm gonna go climbing it's a nice port since i finished so fast uh yeah but you know some days are three hours some days is 45 minutes let's add this skit add day six at reaches skits date skit commit let's uh let's add a add a kit ignore here that says a star star star dot dump simple star star input star star example kit push okay we've updated this and uh yeah i think we did good today it was a we did it in a decent amount of time we did it like in a reasonable way we tried to do it naively just say hey you know this has called fast enough to just do it um but that didn't work out because it was yeah we'd run it for 11 and a half minutes and was still running which is a bit much but by memoization like a very simple memoization just like look up the arguments in a map and then like once you compute them save them and then use that like uh it just worked right so i think it's good all right thanks for today sorry i guess for how short it was but uh we're just too good huh all right thanks for again and i think i'll see you at 7 30 tomorrow 1930 that is my time so like half an hour later because i'm gonna go play floorball at six and you know i want to and it's from six to seven i want to be you know get be able to get ready so tune in tomorrow at 7 30 and have a nice monday all right catch you a tomorrow bye