 All right, welcome to the stream It's Friday I have a bit of a cold so I might sound a bit nasal. Oh That's okay We're gonna be doing We're gonna be doing days six and seven Apparently they're not that hard as day five and maybe even day eight. We'll see how it goes All right, let's get started. How is everyone doing today? So I got my editor open Right here So everything is ready. Let's see what we're gonna do today. Hi little Annie We'll book day six Indeed All right, so we are now going to island island And We're trying to make it snow again. That was our goal Huh, okay. Let's just go directly to the task Uh, you can shift your paper That lists the time allowed for each race and also the best distance ever according to that race Okay, there's a boat race going on. Okay Um to guarantee you win the grand prize You need to be make sure you go farther in each race than the current record holder The organizer brings you over to the area where the boat races are held Uh Boats, okay toy boats big button And holding on the button charges the boat And releasing the button allows the boat to move Boats move faster if their button is a longer But time spent holding the button counts against the total race time uh-huh right You can only hold the button at the start of the race and both don't move until the boat button is released So, uh, the first race lasts seven milliseconds. The record distance in this race is nine millimeters Second race lasts 50 milliseconds at a 40 and 30 Your toy boat has the starting speed of zero millimeters per millisecond For each whole millisecond you spend at the beginning of the race holding down the button The boat speed increases by one millimeter per millisecond. Okay Hello orphan of cthulhu How are you today? Okay, so we're gonna hold it down So it's like an optimization problem basically Aha Determine the number of ways you can beat the record In each race Okay So it's not just about finding something that works. We have to find everything that works Let's see So this is the example module main where Example Equals and then Pump it in and now We have to parse it. Let's grab our parsing code From here Let's see parse seeds, right? um So you can split that length Okay, so we just need to split on function again and let's see we're just going to keep it as a list of pars takes a list of strings And we're just going to keep it as pair of lists or maybe Let's do a list of pairs Okay, uh, let's see. We don't need this So pars strings Uh, let me see what the actual input looks like Okay It's just different numbers Not like A lot more Let's just do this here. Um Oh nice shirt. Thanks a little any Times Equals, okay, so we're going to uh Time split at length Time times and then we are going to uh Drop pile Is space import data.char Is space and we're going to need this digit also Okay, and then we are just going to uh T and s is going to be split on Okay, so these are all going to be Okay, so we're going to drop while is space Let's just do like this. Um Let's uh See what this returns I don't want to spend time on this. Let's just uh Let's just say here example equals Seven nine 15 40 3200 and the input Let's just check it out input It's gonna be uh 56 334 71 71 1335 79 1350 And 99 2430 So for the first one, I think we just uh We don't need this then So if we hold down Let's say total distance So if we if we um So okay total distance Uh, so Hold down time Time And total time Is equal to uh Total time minus hold down time times Okay, let's see Where this is right here this The difference is uh this times So this is how long we Part of the time that we didn't hold down for and then we're just going to multiply that by Hold down time Okay, now let's see do So, um Let's go to hold down time a little bit change with not this one Hold on time. So let's just see what happens Print a map dist So total time here is nine over uh The range we can hold down for zero We can hold up to nine GTO Day six that hs ah, sorry Day six that hs o day six Time day six day six gotta be in the right directory so, uh If we so because the first oh the first race last seven milliseconds Okay, so the total time here is Seven right, so let me get a six 10 12 12 10 6 Okay, now we got to solve some a linear equations um So the derivative So let's write again dist yx is equal to uh y minus x times x Uh, which is equal to this is yx minus x squared And the derivative here dist prime yx is equal to y minus 2x right so We have to solve the equation um So we're gonna now map Let's print that again. So print map This seven just prime zero to seven uh, it's two two times x So this is like the slope, right? Uh, I would be better at this with the pen and paper uh, okay, so dist y x Is equal to zero if and only if and only if a y is equal to 2x if and only if x is equal to y divided by two And we can see that here, right? So it's a zero one two three And then 3.5 right zero And then 4 5 6 7 so um And let's now see dist yx is equal to What was this? This was a y minus x times x um So let's see a dist yx Is equal to zero ah, no, so So now we have the other Is equal to d if and only if y minus x Times x is equal to d if and only if a x squared minus one times x squared plus yx a minus d is equal to zero So now we are solving the quadratic quadratic occasion quadratic quadratic equation And I think this is a b divided by squared of two Something like that a quadratic It's been very very long since I solved this Yes Minus b plus minus Okay, let's copy paste this So we want a b squared minus 4ac Larger than zero for two solutions b times square 4ac equal to zero one solution And if it's less than zero There is no solution and then x is equal to um Minus b plus minus squared of b squared minus 4ac all of that Divided by two times a So I I assume that for all of these This equation works So it's gonna be like a parabola, right? So let's say solve x yd is equal to uh Let's see So the b here is y So it's minus Minus y So we're getting two solutions plus square root of y times y minus four So it's going to be plus 4ac because a is plus 4 times d And then this is all divided by minus two So we're actually going to have a y minus All of this divided by two And that's one of the solutions And the other solution is going to be at uh, okay Now let's uh I think it's curry, right? Oh, so let's just uncurry this function print uncurry solve x Uh map uncurry solve x over example Okay And now it's just complaining that it's uh that it's um These are all integers Let's just say y i di Where y is equal to from integral Uh yi And d is equal to from integral di int load float Okay, I think I might be missing something up um It is minus b plus minus square dot b square Minus four times ac divided by two a Okay, let's write it out again Okay, let's see Uh, it's minus d Uh, it's not plus d Okay, so now we have to be So let's see We have to round up So let's do here c ceiling And floor It doesn't like this Because we want just the positive numbers Okay, and then I think part one Takes in a pair And returns a pair so part one is equal to Where t comma t comma f Is equal to uh solve x p p F minus t Uh, it's gonna be total time and distance So total time and distance print Map part one example Three seven and ten Do we have three seven and ten options? Okay, so first we could go Let's see 10 12 12 10 Let's see I think we're out by one So we get we're supposed to be getting four eight and nine. We're getting three seven and ten We are close though Let's see. We're supposed to just get ceiling here Uh, why do we need the quadratic formula because we want to figure out the total of number of ways, right? What is the distance? No, sorry, this is supposed to be 30 and this is supposed to be 200 No, this is the total time here is 30 And then we just go up to 30 So here we have Exactly 200 Uh, okay, I think it's just in So we're supposed to take the ceiling in both places Uh, but we have an off by one error In case there are inter integral solutions so Exactly. Yeah, we could do that, but we can do this in o one if we have the quadratic formula, right? Uh, which is what we want so, um, let's just do D prime F prime is equal to so x f t t d And epsilon b now we're just going to say L is equal to F minus t so if Yeah, but so what you're saying, right? Orphan is that we should generate all the elements and then check them, right? But we don't need to generate any elements. We we know by the quadratic formula Exactly where it's going to happen, right? Let me see if Let's just plug it in directly, right? If so we have the two solutions here Now, let's see if test t t f Is equal to d then um I don't need this then That plus one else Oh Because then there's like one extra point No, okay, let me just see So we're gonna check we're not gonna check this we're gonna check, um D t is equal to this t t t D f is equal to this t t f Um I think I was correct actually I think we should be taking the ceiling here the floor here um So now we're just gonna see This is the so this is the first element. It stops being that. Okay Yeah, okay If dt equals d So it's gonna be uh f minus t The thing is that a If dt is equal to df then f minus t minus one else f minus t So because they're not different solutions. They are the same solution I think that's the trick So 489 Yeah, exactly Let's uh Let's just see if we are Correct here And I think we might have an off by one error, but I'm not sure Can we We might as well check All right, we did it So the problem was that um You could hold the button for at least 11 milliseconds And no more than 19 milliseconds But that's not really true. So you can you can hold it for 10 milliseconds Ah to to beat it Right, okay So the thing that I missed Is that a We found the solution And where it's exactly 200 but we have to go more than 200, right? Um, so then we don't count It's really just if dt Is equal to d That's the that was the thing, right? So it's not if they're the same It's just if they are Um Because we want to go we have to go further than the distance, right? I agree that the time complexity is better in your solution, but for so small ranges It won't make a significant difference Uh, I mean, that's true But uh Yeah, okay These are not huge elements, right? But I feel like this is the way to solve it. I don't know Because we just have an equation for it, right? We don't need to uh Okay, anyway, we did a part one Took us 40 minutes though But uh, okay As a result of that realize piece of it with a race times record distances Just has really bad kerning. There's really only one race Uh-huh Ignore the space between the numbers on each line Okay See Now it was good to use an equation Example Two So this is going to be seven one five three zero Nine Four zero two hundred It's gonna put a five six Seven one Seven nine Ninety nine Three three four one one three five One three Let me actually just grab it from here Uh, the example one is correct But for the input We should make sure it's correct. Okay So Back to your point. What is it with the time complexity here? Well This is going to take no time at all right print part one example two Because we just used the quadratic formula, right? seven one five three Exactly Now I want to ask you Kuzulu How long did your Part two solution run for And it was it four milliseconds So but you know we have an o of one solution, right? So we shouldn't Uh We should not give into our dumb coder temptations All right. Well, we did part One part two day six. Ah, that was quite easy And we we did have to revisit the formula We did some calculus But cool Let's move on right away to day seven We are trying to get to day eight today And then we will have been maybe we will have catched up dot. It just Do do do do do Let's see Okay Let me go to We check one thing Do I think it's feasible in cock? a Agdalene. Yeah, I mean It's feasible, but uh, so I'm I'm I'm at Chalmers, right? So I did the Course called types for programs and proofs and it's basically just the agda people That write the agda right under sabel Uh Peter db. All uh, you know, yes, but it was there An oulf norel, right? And he's like, I'm going to show you how to program in agda, right? So we had been doing lots of proofs, but I was going to show us how to program The first thing it does is like, let me turn off the termination check Oh So you can definitely do it, but you have to I mean, you're not You want to be able to run programs that don't terminate or you have to provide fuel or But it takes a lot longer But haskell is the golden median road where Almost every type is inferrable But uh It's still very powerful, right? So you don't have to do You have to don't have to annotate everything wait. All right. Let's start day seven Yeah, and then we will have caught up not catched up Little Annie is sending me camel cards camel t-shirt Uh little Annie is sending me grammatical corrections, which is good. At least someone is paying attention To the grammar, uh, let me see Did you bring the parts a large camel? I was riding a camel in India Like a week ago good times um Let's see Very large rock elf explains rocks desert island normally big machines Receiving the parts Now we're playing camel cards Let's see Wow, don't let the camel people hear you say that uh It is Like an ml variant. They have a nice module system A everything is eager so you don't have to deal with laziness But you also don't get easy laziness everywhere And you have objects. I mean they like it. I don't know When you would chose a camel over haskell But uh You know like jane street That was advertising here earlier. They make a lot of money And it's all written in a camel or a lot of it. So It's practical They like to tease us the haskell paper because we use dollar signs everywhere to to to say You know evaluate everything after this dollar And they're like, why are they putting dollars if we're not making money? Well The thing with haskell is that you don't know that it's haskell because it never crashes. So you don't get this Oh haskell crashed error message But there's haskell at facebook at google at Uh Like uh, ahrefs is there mercurial is there cardano. It's all haskell There's a lot of haskell out there in the wild, but uh Yeah, we like doing more fun stuff Anyway, let's get to it So you get a list of hands and then we build our base of strength Five cards label akqjt 98765432 Okay, a is the highest and two is the lowest Let me just card types equals let me uh okay, so this is a string and Strength takes a char things is equal to So i'm just gonna do a very silly thing find index Sorry, let's not do it this way Cardano gets one update a year because no one knows how to program haskell Well, they are learning I don't know Is it worth it to learn to Program in haskell and then you don't lose millions of dollars on some bug? maybe Let's just write it out to Is equal to zero three two three four five six seven eight nine There's a better way to do this, but uh, I think this will be the fastest t t j q so I could like look up in the string and then find the index all the time But I okay, this might be pretty much your optimum position Bear with me How's the audio quality by the way? Is it okay? I used to have a better setup with like a noise gate and stuff, but It's not working. Let's see. Maybe we can get it working at some point Okay, uh, every hand is exactly One type from the strongest week is they are five of a kind four of a kind full house three of a kind One pair and a high card Okay This seems like a long description but let's see so five of a kind is um Let's do here for data that's set now, let's say Hand to type takes in a string and returns Hand type is equal to five of a kind or four of a kind or Let's actually order it in the opposite way. Ah Yeah high card one pair two pair three of a kind Let's see. So this is just the So hand to type. Let's just say that this is going to be hit hand to type is true is equal to set dot from list string set dot size And it doesn't like this because I already took in the string here so, uh, the idea is that so a If the set size is one like there's only one it's going to be all of them are the same, right? If uh, all of them except one are the same it's going to be The set size is going to be four No, set size is going to be two Uh, but the set size might be like a Okay, we can't do it. It's not that easy but it's it would be nice if we had Let's just do then sort So we sort it and then we we group it and then we We do maximum by Let's see here. Okay, uh compare on No, you have to import data.function On So we can take the length of the maximum by compare on. Okay. So then we have, you know, so if it has all five That's going to be five if it has so then we just have the AAA and then four Uh, so full house Is different So then we just do like this. Okay sort by Um, let's do here k sort by of So if it's a map things I think I'm over complicating this a lot, but whatever So if we have a five data hand type Back to the hand types a high card one pair two pair three of a kind full house five of a kind deriving eq show four So if we have this if it's just one group everything is the same. It's five of eight sorted equal sort string right hand to type hand type So this is if I have a kind four of a kind It's going to be four comma one full house is Three two Okay, three of a kind is going to be three one one and two pairs going to be two one and One pair is going to be two one one one and then one one one one one high card Okay, so now let's write and data hand is equal to hand string hand type Deriving eq show So now we're doing instance board hand where compare h1 h2 is equal to hand s s1 t1 hand s2 t2 is equal to t1 is equal to t2 then Can you compare lists? Let's see. Um Okay, you can just compare the list then compare map else Let's see. Um Let's see equals compare t1 t2 in if case compare t1 t2 of eq Let's see if any of this even compiles Okay compiles. Let's see because uh t hand type so if I compare high card with two pair Then I get that high card is less than two pair, right? because The derived or instance is just makes so that first ones are less than the latter ones Okay, uh and hand string Now let's write in the example here and let's just pair us Some parsing again Actually, I think we should parse Because I'm guessing the input is a lot longer a lot more hands Sometimes this happens. I don't know. I think the editor gets caught up in some wrong state okay, and far as example it's gonna take three is gonna be taking a string to hand comma bit int parse entry as stir is equal to uh hand comma bit split at five equals to two hand instance read hand where read and I forgot how to do the read instance. Who go read instance read say read at hand Let's set the right language here uh print print do print map parse entry example Okay, we parse the hands Now let's see what we are supposed to do But at least we know how to compare hands as well five hands, okay Each hand is followed by a bit amount each hand wins an amount equals to this bit multiplied by its rank weakest hand gets rank one secondly gets rank two Okay, so let's say sort by compare on first So three two are first KT JJT gets rank one rank two and KK 677 gets rank three and Okay, so it's in the right It's in the right order Okay, so part one is equal to um sort by compare on first dot map parse entry Okay, and then we are going to uh sip sip with one Okay, so if we can take the id and we can take that we don't care about the hand We care about the bit So now this is going to be the uh list of numbers And then we sum it up right 6440 nice Let's make an input file Let's see Read file input To print dot part one at our lines Okay nice Day seven part one is there Now to finish part two Let's see what happens Okay, the elf and it is one additional rule. No j-cars are jokers While cards I can act like whatever card Would make this hand the hand the strongest type possible Oh j-cars another weakest Okay, let's just re-implement this card strength two three four five six seven eight nine Okay, so now to get the hand type We're gonna have to We're gonna have to mix it up a bit more a hand to type two Okay, so if they are all the same There is nothing We can do so first of all where So let's just make some cases based on the number of a number of jacks In it. I think that's easiest where Let's see Where nj is Is equal to filter is equal to j length stir What is that function because not span it's the Partition right not j's and j's is equal to partition and um J's is equal to length j's Okay And now we are going to say ht Non-j Groups okay String okay case Num js is equal to zero is equal to case non-j groups of Okay, if Mjs is equal to five It is simply um Gonna be five of a kind if numjs is equal to So these are the base cases right now if there's one Then we're just going to do like this It's gonna be Four so if there's four of anything It's gonna be five of kind There's three of anything it's gonna be four of a kind If it's two we can add the jack to either of them to make a full house If it's two one one it's going to be three of a kind um And there is no high card Okay, so if it's a one one one we can add one one one one then we can have a Hmm Let me see If there is a j We're always going to be better than a high card So yeah, so we would never make a two pair because We could always make a three of a kind here Okay, that's for the two k one case So for the two if we have three we can have five of a kind We have two we can have four of a kind And if we have one one one we can get three of a kind And that's always the best, right? We're never going to get a full house because then we would have done Uh, we would have done four of a kind So three Again, if we have two of it, it's going to be five of a kind If there's one one We're going to make four of a kind Okay, uh if it's five or four It's going to be five of a kind Okay, I think this will work now, um Now we have to do the compare again compare two Takes a Sting to a string it's going to take a hand String comma hand Hand type string comma Hand type to Ordering Compared to is equal to a s1 s1 t1 s2 t2 We have the same thing again, uh case compare t1 t2 off eq Compare compare map card strength to t1 no s1 map card strength to card strength to s2 Otherwise, it's just x2x now, uh, then mop So we're gonna we're gonna parse it a bit differently We're not gonna say read in that hand. We're just gonna say, uh hand hand hand to And then this is going to be enter type two hand and here we're parsing at two okay, and And now we're going to say sort by compare two on first map parse entry two Let's see a hand print part two Okay, non-exhausted patterns in case Let's see Now we're putting debug dot trace Okay, so here we're in that two case and it's a trace show Oh, sorry This is This is supposed to be njs. Oh, okay I just messed it up here. This is non-js Trace show id link j sort non-js Okay, now it's correct. This is how I debug has to go by the way. I usually just Pop in a trace show id in there And uh Unless you're working on really big code bases it it works quite well Let's see boom Day seven done Not bad One and a half hour for two days That's uh That's good so let's See and move on right let's see if how far we get with two days Admin of code, but give me a break. I'm gonna have a little sip of Water I'll be back in two minutes We got some water Let's do day eight We're on a roll Okay, you're still riding a camel across desert island when it's but a sandstorm Okay ghosts Interesting uh one on camel's pouch is labeled maps. Oh my god This is gonna take a while Um And how to navigate a desert at least pretty sure that's what they are One of the documents contains a list of left right instructions the rest of them seem to describe some kind of network of labeled notes It seems like you're meant to use the left right instructions to navigate the network Perhaps if you have the camel follow the same instructions, you can escape the haunted wasteland A and Z Okay, so a You look up the next element based on the next left right instruction your input Start with a and go right By choosing the right element of a C Then l means to choose the left element of c z So then you choose said Of course, you might not find zed zed right away If you run out of left right instructions Uh repeat the whole sequence of instructions as necessary rl really means Here's a situation that takes six steps to reach said zed zed Starting at a How many steps are required to reach said Okay We got a lot of input if you get at the 700 s day eight Eight touch the eight dodges Touch input Okay, let's just paste the input and let's Example one and let's make Example two Okay, what do you mean? Paris, let's just have the example here On the side To read so parse It's going to take a string Don't list the strings And it's going to return something we don't know yet parse, okay, so first is the path And then empty and then the rest path empty notes So the path We are just going to It's going to be a list of booleans and the notes are just going to be Um string Actually, I'm just going to be clever right away. I'm just going to say int Incoma int so First one here is just going to be map fc kc of l True. No r is true l is horse path And then where notes equals main io do print read file Example one Into print dot parse Dot lines Let's see. Okay. So we parse the first one. So the notes um So for the notes we are going to parse node parse node stir So we saw from the input that they're all they're all three of length three So we are just going to count on that and we're going to say, um uh node and rest is going to be split at three string then we are going to jump over the this part split at three rest Actually, this is all this part and then we're going to say so the left node And then the right node but in between is going to be I'm going to take these two equals uh and l n and then Okay, we're going to say map uh map PS parse results is going to be map parse node nodes Okay, now We are going to say, um I I just want to I want them to be integers import qualified data dot map as map import data dot map So now I'm going to say a Key map is going to be hot from list sip map first PS And then I'm just going to map look up all of them and l n all right So it's going to be key map key map Okay, it doesn't seem to work Uh because this is actually four characters Okay, now I have the list of nodes and then I want um I want to know where a set set set is And I want to know where AAA is Okay, so you're just getting some messages Okay, so now we have the instructions we have the map This should actually be a an int map It's just easier that way And then we can just um Look it up directly So now let's say, um Part one so, um So path Uh nodes AAA Is equal to uh parts Okay So let's see, um There's a function called cycle Where we can just actually We just say go cycle Path And we're just going to say the current one So go So this is the p in the ps and it's never going to run out Because we cycled it current node is equal to And we're going to say zero current steps so case a current node is equal to set set set It's equal to current steps otherwise otherwise Is equal to if p then So p was Ah, we want to take the I want to take the first one. So I'm going to have it left and right here If p Let's see So then we're going to say, um mapim.bang current node Helen Okay, if p then um Go ps steps plus one left node and I should really uh I should add bang patterns here Because I really don't want this to build up Okay, uh ln if p then ln else all right read file for example one Some haldi milk It's good for health So what's going to happen is that there's going to be loops And It's going to take hell along if I'm going to follow all our loops so print dot So what we want to do is we want to figure out the length of a loop um And then kind of jump over that in the stream But let's see I wonder if we're in a loop Yeah, anyway Let's see what happens So two steps six steps Let's just test with the input, but I think you know, we're gonna probably need some cleverness here Okay, maybe not for part one all right day eight Part one done No cleverness needed. We just made sure to Do it efficiently within maps So haldi milk is this indian thing I think it's a turmeric And milk it's warm It's good. Let me see. Okay day eight Okay, let's see You had the camel follow the instructions, but you barely left your starting position Oh no, you start at every note that ends with a Let's see This is gonna be a pain But let's see if we can make it work um So now we are gonna We're gonna do the same parts But instead of just returning these two, we're gonna return the list of all a notes And list of all b notes or said notes And it's gonna be A list of ints And this will be a list of ints Okay, and Let's see, um No names Is it gonna be filter? let's see Okay, so this is gonna be a notes map key map parts two dot lines Let's see So there's zero and one three. Okay So these are correct I'm actually gonna make this a set an int set And then for part two So the trick is we don't actually need to Like so maybe for some of them we're gonna end up Being at the same places at the same time So what we're really really doing is So uh cur steps pps cur notes That's our cur nodes Uh, so we just want to check if these are the same set Oh, no, we want to check if all of them basically if if I remove All the empty nodes If I remove all the set notes from cur nodes, I want an empty set otherwise They should also be paying So not go is just doing the same except it's transforming a set instead of I want to map the set I don't think there's a way to do that directly Okay if p then We want to take all the left nodes if p then uh cur nodes iris hot map let's see if p Then I want to take so I'm gonna take in the key here and if p Then I am dot bang nodes Cur node Let me see So I think I can write it like this I am I am dot bang nodes and dot and then if p then fsd else snd Let's see Okay works for the example Let's see if it works on the input Yeah, there's gonna be some looping stuff and It's probably gonna be a case that Like some of them will be in a loop So we don't have to do them Let me check the memory use I was not leaking memory. So It's just doing its thing Let me see Cur nodes prime It's like this How is aoc going? It's going great Eric wassel you are the creator, right? I'm trying to solve day eight now I started a bit late So I did day one two and three then day four and five And today I did six and seven but now I'm done day eight part two and The brute force solution is not working Even though it's quite Efficient Haskell Yeah, there's gonna be some loops and stuff. I'm trying to do like a whole set of numbers at the same thing time And it's It's not doing the thing So Let me just see how many nodes am I doing at the same time Trace show Is that size Cur nodes prime This should be Let's see So I'm not just doing six nodes at the same time So it's not that there's a huge number of nodes Maybe I should Yeah, I need to Bug in ghc Do you remember who it was? Because that's funny. They must have been doing like Some type level stuff Let me copy the address look at it while it runs Hey, that is Ah in the standard lib running a password data format integers Ah That's funny I didn't run into this because I uh I split it Definitely Very nice Yeah, this is like the the read instance is just Ah Very cool. It's fun too I would have thought like, um You would have think you would have thought that something like this would have been found way earlier, right? But I guess that like all the production people they don't use the Read instance, right? They will use like a proper parser like mega parser or something like that So It hasn't showed up Yet Okay, let me Delete this Should not be printing anything out Yeah, okay, so So this checks if all of them are set nodes At the same time And we're mapping like six There are six a nodes and then we're mapping them all We're mapping them all at the same time Yeah, I'm not but I think it's it's been a very fun Advertisement code this year Like day five part two was a bit hard Because I mean it wasn't like It wasn't like oh, I need a good data structure here. It was just You know, you had to you had to split up the cases and then Do a bunch of these And that worked out But it was fun And then I liked day seven part No day six part one because you can just use like A quadratic formula. So it just works quite fast but And day seven was nice also But day eight part two is currently Stepping me Yeah, I'm Looking forward to the rest It's been fun. I remember a couple of years ago It was very hard at Haskell because you You made this puzzle where you had like a Virtual machine or something and you were constantly running code and instructions on the machine And then you would have the machine Modify. Yeah, exactly endcode. It would like modify the code itself Yes, 2019 and It was very hard in Haskell because you had to like To make it deficient you needed to use like the state monad to kind of be able to Mutate the data structure efficiently, right because you couldn't just copy the whole thing With a one change in it Uh And I had a lot of fun with it, right because there was a lot of Like efficiency hacking in Haskell So far, uh, this year it's been very Like there's no like oh, this is super hard in Haskell would have been easy in other languages But I'm excited to see Where it takes us I like the The lore this time is a bit funny. No We're trying to get snow. So then we went to get sand Then we took an airplane Surprisingly easy in Haskell I mean some of these are very nice in Haskell like, um Like day seven for instance, right? So after I put it in the right kind of data structure You know the actual solution is just You know sorting on the right operator and then multiplying And then taking the sum and that you know, I always like it when it kind of comes down to a very simple one-liner like the actual solution after you Put it into the right data structure but So the thing is that I don't solve them in other languages in Haskell so I don't I don't know how easy things would have been like oh, this would have been super hard in python But I like it It's a lot of fun to be able to also play around with Haskell right because it's also a perfect It's the perfect size of a project to play around with right because you're not going to be writing 10,000 lines of code for one solution right you're writing like a thousand lines of code at the most usually like 200 or 300 And Haskell is a great language for exactly that length exactly No, but it's been a it's been a lot of fun And I've streamed since 2020 every year I stream Advent of code in Haskell I think I've never finished though because I usually so I live in Sweden, but I go I when I'm from Iceland So I go home to Iceland around the 20th or something like that and it's it's very hard to Stream and keep up because at that point also you're not spending like an hour you're spending like three or four hours usually for me and that's just It's a long stream right it's a long time to just be sitting there programming and looking into a camera and I think it's It's been very successful. I think everyone's having fun um Especially in the fb community. There's a lot of people doing it in Haskell A lot of people Doing it in okamal But I think it's because we like these Simple problems because you can kind of make it very elegant But you can also make it And like you're not You don't have to deal with like factory factories and kind of stuff that crops up when you have to do huge programs, right? Yes Well, let's say I think I need to do something different here It is for sure Not finishing the good thing is that it's not eating No, I it's not eating up memory. So if I keep running this it will finish eventually Yeah, I'm doing part two of day eight Right now um So part one was quite nice You just follow the instructions, right? The title loop And then I thought ah Maybe I can do the exact same thing in part two But use This set right so instead of saying Uh map this number and then check if you're at the other number I say, uh You know map this set of nodes And see if you reach the other set of nodes And the nice thing about that is that if at some point the set gets smaller Then you know, so we might be running like a hundred a nodes at once and then they kind of coalesce And they could get into the same nodes And then it should be smaller and then it should just work But uh it's not It's not fast enough It's been running now for Yeah, five minutes 10 minutes Now exactly that's but you know, so you had this discussion with the because we don't talk too much to the Like the object oriented community like the fp community and the object oriented community are not They don't talk too much together And But there's things that we could share right like things like Like where do you put a function right so in the object oriented world? You just put it in the object any kind of works But for Haskell, you know, you see you think like do I put it in this module? Or do I put it next to the data definition or do I put it next to the use site? Um And in object oriented Like in the object oriented world they have long dialogues about this and like the sign Patterns and stuff like that, but for us we have we don't we don't have that discussion so But we are starting to have it so it's going to go good. Let me see Okay, 10 minutes. It didn't work Common denominator Yeah, that was a little bit of a spoiler But let's see A Uh-huh, okay, so you can see here in the first one that So 11a it just goes 11a And then uh-huh Now I get it Thank you Eric. Thank you so much for making it. I see but I figured this out. This is the Chinese modulo theorem You need to figure out the length of all the paths and then You take the module off the here stuff Ah, that's good And Okay, let's see so Part one Let's see then. Okay, so let's change this up Because now I'm like simulating it until I hit the thing but Yeah, the common denominator is a good Good way to do it So let's see part two I'm just going to copy that part one go again Okay, and Let's see So Okay ALs x equal to And map go Path zero ALs IS dot member Let's see what this one does two and three And what is it for the input? Okay So the least common denominator between all of these So the so I think if I have it correctly, right? So I'm going to finish it and then see what happens But I think the idea is that so so Let me just check the least common denominator Chinese remainder theorem Do do do do do do do do do So how how does it work? Okay, so the idea is that Like for this one It gets into a loop right so One two one two one One two one two one two, okay And then so but they don't match up here, right? So it's easy if you have two numbers You just multiply them together But I think you need like the Chinese remainder theorem for more than So you need to find a number That is a multiple of all of these numbers Find a number that is a multiple of many numbers Yeah, so this is the LCM and So it's the least common multiple at least common multiple Yeah, so the okay exactly Hmm. How do I need to factor? all these numbers let's see factor int factor n Is equal to So I'm just going to make a super slow version Haskell has LCM in base Right, but this is not the Go so go of empty is equal to n Go of Factor factors is equal to if n mod F then F d F factor qj e d8 else go of us factor 21 factor 30 Okay Let's see I think 307 is the least common multiple here Yeah, it works That's exactly the point orphan is the same as with the quadratic thing, right? It It's not wrong It's just not It's not Yeah, it doesn't work well enough So here I think now we can take the Factor each number and express it as a product prime number powers Okay Let's see Product times 307 Let me see We finished day eight Yeah, so that's the thing right the solution here is It's not wrong Right, it's that is it's not the case that is wrong It's just not efficient enough right because we're Looking looking looking looking looking And this number here is quite big It is a 12 trillion That is crazy big So the trick is here that let me see LCM of a list of numbers LCM is equal to Okay, so here we're gonna first so we map the factors and then we Okay The highest power Of each prime number together Okay, so we have the factors and then we need to We group the factors DDD Now this is gonna be Multiplying the highest Is that is that it? Ah, okay Apparently you don't need to factor or anything like that. You can just Fold our LCM Which makes sense. You find the least common multiple between two numbers and then That number and lists. Yeah, so you just keep going Okay, cool. You don't even need to know the base case, right? But all right, so Now we've done We've done everything there is so far I push the code to GitHub you check it out there and I kind of like this solution I went with the naive one first Uh, it's way too slow because it's like 12 trillion Operations, but I kept saying uh You know, there's going to be some loops, right? So but then P cook or the I don't know how to pronounce that username Uh, suggest that the least common multiple and It worked and it makes sense, right? Because you they're all going to be in a loop because the Instructions are cycling anyway, right? and then you just kind of figure out um When like how many steps would you have to take so that they all happen to be in said at the same time? so one thing that I think messes it up a little bit is that um so the the the The reason this works is that I think they're all going to different Seds Right, so for instance if I if this said here Would link to this said here that would then link to this said here again, right then After you've taken a certain amount of steps You would just be looping between sets, right? You would not be going back to the start Uh and going around around there, right? Especially like, you know if if you take enough steps and then one of them just stays in said, right? Um, then you don't have to find the least common multiple for that one because that one is always going to be in said afterwards It just so happens that they're all kind of looping and I think it's because Eric who showed up the creator of the code You know, he makes it so that you can't do these things to make it work but I I'm not I think we should read the um I think so I think it I think it would be messed up If you could if you had these weird kind of notes, right? But we didn't have that this time So the least common multiple works Okay, thanks for showing up um The video for day four and five is live right now I set it for 11 This one will be a live live In 24 hours And I thank you for joining. Thank you for Helping out. I probably would have worked two way too hard on this least common multiple thing And uh, yeah, see you tomorrow when we do day nine On day nine of vitality code. All right. Thanks for today. Okay Bye. Bye