 Hello, and welcome to Day 5 of advent of code. We're streaming a bit later today I mean it's the Sunday But it might actually be a hard one today, so maybe we'll Be here all night Anyway, I wanted to have More of an option for like people from the US to join But I think I'll move it even later in the following days, you know like Eight thirty or nine like an hour later or an hour and a half later And then you know, it's just easier for this to be like the last thing you do instead of Yeah, then I couldn't go climbing and play four ball and all that stuff All right so Let's start with a day five. Well now it says Zero X F F F This is a bit loose. No, let me see Fasten it. Okay, let's see what a day five has in store so I set on the tweet like the announcement that Today is supposed to be hard, right? You know, I don't I don't look at it, but I You know, I pay attention to Twitter. I try to find And I follow some people who do the task and Someone was like saying today was super hard to do functionally, right? So we'll see how it works out, but we're at least we're not doing a double feature, right soon. Yeah All right Let's get started Okay Hydro thermal feature you can come across a field of hydrothermal wents On the ocean floor these vents constantly produce large opaque clouds So it would be best to avoid them if possible Okay, they tend to form in lines a The submarine helpfully produces a list of nearby land lines went for you to review For example, sir 9 5 9 8 to 0 8 Each line of a skin is a line seven in the form of x y 1 1 2 x 2 We're actually according to one and the line 7 x 2 at y 2 are The coordinates of the other end These line segments include the points at both ends In other words in line would recover spot 1 1. Yeah, okay 9 7 7 9 8 7 Okay, so so we have for now consider only double horizontal vertical lines Okay reduce the following diagram In this diagram the top left corner 0 0 bottom right corner is 9 9 each position is shown as a number of lines So which cover that point Or dot if no line covers that point the top left pair of ones for example comes from 2 2 2 2 1 yeah, okay, the very bottom row is formed by the overlapping line 0 9 to 5 9 and 0 9 to 2 9 To avoid the most dangerous areas and to determine the number of points where at least two lines overlap In the above example, this is anyone dying here with a two or larger a total of five points Consider only horizontal and vertical lines. How many points do at least two lines overlap? Okay, so this is a Hey to me Nice to see you back Hi little lanny All the regulars are here That means we can get started. I'm also wearing this amazing t-shirt Which I got from little lanny all right Right, so now we are figuring out like overlaps right line overlaps um But let's first produce the lines, right? We need to do some parsing All that jazz Okay, here we got the terminal new file example Let's make it bigger Bigger batter stronger. You see the ligatures Wow Makes it look much better, huh? All right, uh day Day five dot hs now Module main where and we're gonna run this now, um So parse lines. Let's just import text dot parser combinator read p And we're gonna parse the line and this is gonna be a read p of A tuple of tuples right up it's Int int comma int int now uh parse line Let me do uh, so we have a Read p int Is equal to read s to p Add int right Let's say i'm defined here So this one and this is a read p int is going to be read p in Okay, and this one doesn't work because I think it needs type applications uh Wait our plugin worked, right? Worked right in the beginning language type applications Now let's gsdi this D five dot hs to read s to p is oh, yeah read couldn't match reads Prec five Move on reload this Is read It's preck right preck Mm-hmm Okay, I'm just gonna ignore the add int here And now it's just a main i main equals print put stir ln Hello slash Yeah, I upload um I upload these to youtube after the fact because you know, it's nice I think it's nice for people to have like an example of Haskell code just not not finished but like being written right because it's one of the problems we We deal with and In trying to Emulate programmers, right wait like automatic program repair um One of the problems is that we don't really have examples of incomplete code We always get like complete code committed to git, but we don't know How was it made right So, yeah, okay, that doesn't matter so do Okay, so x1 is gonna be read p read pint and then we're gonna say read s to p Or maybe but what is the type of read? I tried that right I tried that right And that that returns A function, but we have read s. Oh, okay, that's uh I didn't know what read s I would just be using reads preck Makes sense Thanks to me Okay, we went to a comma And then we do one one Pint and then we parse the string space Like this I think right Yes, and then x2 is repint y2 Pint return x1 y1 x2 x2 y2 Right it needs to be big big reloaded and now let's do Um So now we do read p to s right Okay, now let's uh do a nice trick So data line is equal to And then we just we save the lines, right? And then we say, uh Well, let's have it like this And then we say return line And then the two things And then this one returns line Okay, and then instance read line where Reads preck. So this is why I always do it to me Because we have to do rep to s parse line Okay, and then we say instant Stance show line where show line x1 y1 x2 y2 Is equal to Uh x1 comma Show x2 show Oh, yeah, this is a b y1, right? So I really I'd really like, you know, like string interpolation In Haskell, but uh, we don't have that As far as I know There is like a printf thing, but uh, uh, it's like it's got a weird type class. So I don't like it Okay What if I do now read add line Set x type applications read add line 0 comma 0 to 5 comma 4 or 4 comma 5 nice So then we can do a read input file path to a list of line Uh, I of course and we do the classic read input equals map read add line God lines over read file. We've done this a couple of times, right? You do it like this, right? I always screw this up somehow. So it uses fmap. All right, because we don't Provide the string, right? Okay, read input example Oh read file example I think we need to save this Okay, now we get a bunch of lines. It's a bit hard to tell Oh, I forgot to do a comma here. Okay. Now we have um our lines Okay, now we we want to check and We want to filter out those that we can check is vertical or horizontal Take the line or turn to pull Is vertical or horizontal Okay, so we have the line and then we have one y1 x2 y2 We have to wrap all this big thing And I think we even had Yeah x1 equals y2 or y1 equals y2 x1 x1 equals y No, yeah y1 Equals y2 So let's uh, actually just do it here, right? So do lines ellens is a read input example and then We're gonna filter filter is Vertical or is For this and then we print lns Okay, now we have fewer lines Uh, and now we have to find which of those lines Intersect So let's actually have this one as a maybe Either Int comma int or int comma int, right? So So in this case Uh, let's just look at like this. So if if they are like x1 is equal to x2 we say Just left Uh, then x1 is equal to x2 Oh, okay We have to actually do either line line here Because we want to just get the ones that are horizontal and I think they're the ones that are vertical because If we have a Euclidean geometry This is important Uh, horizontal lines cannot intersect other horizontal lines, right? unless It is the same line But we don't okay. Sorry. These are not actually lines they are, uh Sorry, I learned math in Icelandic like so we have a liner like lines and then we have a streak Which is a sty I think it's like a sty dot is Orla sub sub Starfight so, uh Yeah, we have Manhattan geometry wait, let me let me see So Icelandic Streak, uh, yeah, so a line segment, right because Uh Lines are technically infinite, right? But uh streak are like finite lines And that the trick is that like you can have two finite lines that Like that don't intersect right because they are Because they're like on a different interval anyway Uh, let's see Let's go back. Let's actually go back and check what the task is. So, uh So if I'm not lying, okay, so we're at least two lines or yeah, okay, so we're just gonna find overlaps Okay, so left. So this is gonna be So we're just gonna say just left L Okay, if it is the case that Y1 equals Y2 Now we have Just right L Otherwise, we have nothing Filter map maybe Vertical so let's just call it Bertie or Let's just call it v or h right v for h V or h. Yeah, and let me say here is v or h, right? Is the v or Or h. Let me actually open this folder again because like a Like it's not giving me the code completions here, right? Let's uh open folder Day five Close it turn it off and on again. See what happens Because I want the completions, right? They make this so much nicer. All right. See now we have stuff v or h and Import data map. No wait, this is Not map maybe from data map. This is from that maybe maybe Okay, and this this is so perfidious DTI day five Uh, let's see main. Okay, so we have a right And so where the y coordinate is the same Which means it's a verb It's a horizontal line, right? Yes, so rights are horizontal lines left are a Yes, okay Let's see Lns, okay, so I think Import data dot either. I think there's like a lefts function It takes a list of either. Yeah So let's just say lat Horace equals right right lns and a Rarity is left lns Right, this should be right. So the lefts let me print the Horace and we print the Verdi Okay, so we have a two horizontal lines and two vertical lines. Okay, a Let's actually print a so what I usually do is I do map and print and I just say Putster ln Horace Like this. Sorry Okay, let's see. Yeah, so we have a Uh We have two vertical lines We have two horizontal lines Yeah, which much is this? Two vertical lines and three horizontal lines. Yeah, and you see here that it's overlapping themselves Okay, so Uh, so this is gonna be a bit of a trick, right? So we now we're gonna count So like we only need to check whether they intersect right, but uh, I am guessing from my Year not years year of experience Or okay, I guess I did I've done a couple of years now We're probably gonna count later, right? So what I'm gonna do is like for each line I am going to count Uh, okay now for the second one, we're probably gonna have to check the others Okay, um So let's see. Um So a Horace intersects. So there's a couple of cases, right? At two cases actually, uh H Intersects v That takes a line and Line and returns a pool And then we have uh, okay, let's just say Intersects right line line pool Okay, so, uh Because I think I think also in a late later part. We're gonna have to do it for lines that are not just horizontal and vertical Okay, let's see Hmm Like there are there's like a some there's some nice, uh There are some nice mathematical equations for this Let's just look them up, right? Line line intersection Euclidean Yuclid gravity. Wow Given two points on each line Yeah, but we have a Yeah, this is the thing, right? So line intersection for if they're just two lines It's easier than for segments So I think I think what we can do is the following, right? Let's let's uh These strikes and intersects in multiple planes Yeah, yeah, yeah, I mean that's for the horizontal ones, right? I'm gonna switch songs. This song is Not that good. Okay Uh, let's see But uh, yeah, so the trick is that A horizontal line and a vertical line can only intersect in one point, right horizontal lines can intersect in multiple points, right? Okay, so, um So let's just do a bunch of case analysis here like okay Uh line L at line x1 y1 x2 y2 Uh l1 x1 right l1 y1 l2 x y2 y2 l1 l2 This is gonna be so messy Yeah, okay So we take in the two lines And then we say where uh, okay, so let's say here Is v line pool Is v Okay, but we already figured out which ones are which but yeah, so we need to Okay, let's just we just compare all the lines, right? It's okay Uh, now let's actually so h intersects h I'm taking a line to line And return a bull, okay, so horizontal intersects horizontal So h intersects h so they're horizontal if they're They The y coordinate is the same Right, okay, so Line l1 x1 l1 x1 and so Uh, so l1 y is gonna be the same l1 x2 Because so the the y's are the same Right, that's the point. That's why they are horizontal l2 y and this is l2 x1 l2 x2 Okay, so case so if If we're looking at two horizontal lines So if l1 So let's this is actually an int Where this is a number of points of intersection So l1 y is not equal to l2 y Then a There are They don't intersect, right? If l1 y is equal to l2 y and these are so there's two horizontal lines So otherwise Then we have to count a How many points they intersect on Which is going to be a So how do we Okay, so where where do they then enter intersect? Yeah, I can see how this Took some people a long time and it's not like It's not complex like we know what we're doing. It's just like we have to get the cases right Which can take a long time, right? Okay, so uh, so then we have Then we need to sort the points, right? so, okay, so If l1 x1 less than equal to Uh-huh, right. So we also don't know Whether the two line points are sorted, right? Uh, let's see because we don't know if we're getting like the left end of the line or the right end of the line Oh, okay Otherwise, okay, we do here where? so we have small l1 is equal to Right, so this is going to be sl1 bl1 so the small l1 and the big l1 is equal to sort l1 x1 l1 x2 and sort is in data.list, right? sl2 bl2 I think we're probably making it more complex than necessary here because like what insane person would not Kind of feed us the data that way, but we don't know like it doesn't Doesn't say anywhere that the lines are Like there's no conditions on the lines here, right? So we have to do this So bl2 is going to be sort Let's have it like the same right l2 s l1 b sort l2 s l2 b No l1 x1 l2 x2 Now we know that we have like the smaller to the bigger and then the smaller to the bigger And then we check if if l2 s is less than or equal to l1 b so if If they are like this Then like the number of points where they intersect is l1 b minus l2 s Plus one right at the intersection one point l2 b Is less than or equal to l1 s If they're sort of like this then It's a l1 s minus l2 b Plus one Otherwise It's zero right otherwise. They don't intersect in any points so we have We have like so the two or the two horizontal lines that that Intersect each other And they all intersect themselves I mean, this is like also we could probably do this with like set membership and stuff, but we don't want to do it Okay Now how do I generate the pair of all pairs kind of okay? Now gen pairs takes a list of a's And returns us for this the a comma a gen pairs Equal x's equals a Gen pairs prime A exists and then nothing And nothing nothing so we're gonna do like here where gen pairs prime So we have like empty and then we have Prev and then we have a rest So far equals so far We don't really need now let's Recurs on the last argument. I think that's What you usually do so we have so far Okay, and we have the previ here. Let's do it so far and then prev Now a gen pairs prime of so far And then prev and then we have we have some left We sip x comma Prev plus plus axis. So that's the new one gen pairs prime And we Sip the oh no. Yeah, we sip we sorry the map x Over a axis plus plus prev And like the order is gonna be massively screwed up, but it's fine So we have gen and then the new prev is gonna be x prev x's let's reload our gen pairs one two three Oh, it's three and two and three and one Right and we don't have like one one So we have like four and three four and two and four and one again. It's not actually doing the thing Oh, yeah, because I'm not adding the so far here So we have the one and two with one and three and the one and four we have the two and three two and four And two and one Right And it's also generating the other pairs right Okay, so we don't what we do is that we just don't add the prevs Because we don't yeah, we don't add the prevs Uh, we don't care about the prevs So with a one and two one and three one and four two and three And two and four and then three and four and like the the four and three are already there and the Four and two are already two and four. I like that Okay, uh h intersects h. Okay, so now we're gonna say, um Um, so curry h intersects h. What is the type of that curry h intersects h? Hey, yo good magic vector type Maybe with line. Okay. Yeah, so it's uncurry, right? Okay, uh, so we're just gonna say So we're gonna say gen pairs. Uh let's see we're gonna say, uh With the horizons on the right start map. Maybe v or h over read input example So horror is These are the horizontal lines, right that we saw from me. Okay, we're gonna say Gen pairs. So we have the pairs. Oh pairs of horizontal lines Now we're going to apply uncurry Uh We're gonna say map uncurry h in Intersects h over gen pairs Horace And I'll just go see the numbers Do do do do 0 6 0 0 9 0 So it's saying that 0 9 to 5 9 and then 0 9 to 2 9 Intersect Which is exactly this one. So this is a Yeah, 0 9 to 5 9 And this is a 0 9 to 2 9 and they are intersecting here, but we'd say like this Intersecting at three points No six points where it should be intersecting at Oh three points, right? Now, let's see for that one, right? So now we have a that the y is the same. Okay now, uh Let's see. Let's see trace show in And trace show in Let's just see what's going on Okay, um, that's not really telling as much Oh He should be l wait so l 1 x 1 l 2 x 1 l 1 x 2 Well, this is this should be l 2 right? I think that was I'm still saying that 0 6 0 9. Okay, and Let's actually then just do it for the one line This one this is like line of interest So these are the ones that are Getting me. We would just want to trace that right? D Now R no need to reload the horrors. I want you to need to get that. All right, and then we say h Uh uncurry h inch Intersects h And we apply that to the line of interest Are we getting a 0 and 2? And we get 0 and 5 Uh, and then it's saying if l 2 s is smaller than l 2 b Is l 1 b then Uh, all right Yeah, and would that that's fine. Okay, let's uh consider this again. So l 1 s l 1 b that's like the We the correct beginning and ends of the lines right no matter if I Like stated it in the different order And now we want to check and how many points they intersect So there's four cases, right? They they both if they start at the same time Yeah, if I say they both start at the same point, right? Then we just take that too. Okay, so let's just list all those cases, right? Okay, if l 1 s equals l 2 s So let's do a guard here Now we can actually just do this right away, right? Okay, so now this is this easy case now. Okay, so if l 1 s is equal to l 2 s then we get a l 2 b minus l 1 b right Like here because they are equal they should be three points now, um If that's not the case um if if it's the other case, right if um If it's a if they if they intersect at this point, right? Then we do Oh, no, but this is not true, right? All right, let me check Because it's actually Yeah, it's three points right. Yeah So this is actually not true, right? This is then this is if they if they are the same then this is the min of This is the min of l 1 s no l 1 b l 2 b, right? So if they start at the same point, we find the minimum of the two numbers and then we a l 1 s minus Right, so we find the minimum and then we Uh, I don't know. So the minimum of the bigger one minus the starting point And Plus one Right, okay. So it's the same Right, so we find like the the minimum of the two points, right? So here in this case is the two And then a you know, then they intersect in like the length of the line, right? um Okay, so l l That would be like I think it would be and yeah, we should be we should do something more general but We are going to go for it. So I'll let's say if l 1 b and l 2 b are the same Then it's actually So they are the same here. Then we find the bigger of the smaller, right max a l 1 s l 1 s l 2 s And then it's the l 1 b like this and the point minus the maximum of those two Plus one so here you can say it will be like two minus zero um plus one Okay, so that's the case Okay, uh, so then now we get into other cases if uh l 1 s Is smaller than l 1 b l 2 b, right? If l 1 s is smaller than l 2 s Ah, see now we're getting stuck in the cases, right? I think it would help if we drew it up or something Let me see. So if l 1 s is smaller than l 2 s So if they if they if they're like this and uh l 1 b is bigger than or equal to l 2 s So if this one is bigger than this, yeah, then we get that it's a l 1 b Minus l 2 s then it's a l 1 b minus l 2 s And if they're the same it's plus one Then they're not gonna be the same, right? So it's gonna like right Okay, that's this case You want this case? This is probably easier if we like sort the lines somehow So it would be easier if we kind of knew which one was the leftmost one And we just sort them Let's actually sort the lines. Okay, so that so that we have some Something on on the line. Okay, we know that let's sort it right. Let's say Sort line It's going to be a line to line. Okay sort it We probably don't need this step or we're gonna say norm normal line. Okay, let's line to Line okay norm line line. Okay x 1 y 1 x 2 y 2, okay we're gonna say l x so if If x x 1 is less than or equal to x 2 It's equal to l Okay, if not then we flip the two points p 1 p 2 otherwise it's Line p 2 p 2 people so now we've Now we'll normalize the line so that the left or most points are always uh, the ones we care about. Oh, this is uh Non-trivial right so many checks Read input now. Let's add norm line there Okay, so now you have the norm line and then we know that the leftmost That this one is going to be smaller than this one Let's just let's call it actually We call it norm and let's not do it here. Let's just We do it for the relevant cases. Okay Ah, let me actually look at the input. I'm just gonna see how Okay, okay, so we get a lot very big lines, right So it's definitely worth it We can't just like generate all the points, right? We have to actually yeah Yeah, it's a very annoying because we want to Okay, so h intersects hx h intersects h Uh l 1 l 2 is equal to h intersect h prime And now we're gonna have this Like this, but we're gonna say equal to Where we're gonna we're just gonna normalize the x Here right h intersects h prime norm x l 1 and norm x l 2 so now we know that they are like this okay, um If they're not the same then we Okay, now we check case l 1 x 1 is equal to 2 x 1 then it's uh So if the left or most points, so now we don't need these sorts, right? We already kind of sorted them um So if the l 1 x 1 is equal to the l 2 x 1 then we say We find the main of l 1 x 2 l 1 l 2 x 2 minus x 1 um okay case l 1 x 2 is equal to l 2 x 2 Then we get the max of l 1 x 1 l 1 x 2 l 2 x 1 and then we get a l 1 x 1 minus So if they the same there's the same here we do that there is in there. I could do that, right? So then we have this case right though 1 is entirely contained in the other line, so okay, let's Let's write this up, right? So this is gonna be you know Uh like this, right? 1 2 This one is a 1 2 Now we want to encode We want to encode a 1 2 2 1 And we want to encode a Okay, if you understand what I mean, right? So for this case if a l 1 x 1 is less than um l 2 x 1 And l 2 x 2 But what if it's like this, right? If it's okay l 1 x 1 is less than l 2 x 1 It's easier like if you just have to say true or false, right? But now we have to kind of compute The number of points so we need to figure out which one is which Okay, so l 2 x 2 Is less than l 1 x 2 And that's this case I mean I've got in the other case l 2 x 1 is less than l 1 x 1 Oh Laylif Thanks for the follow I like the I like follows Hey wait l 1 x 1 is less than l 2 x 1 and l 2 x 2 is less than l 1 x 2 Okay, now we have l 2 x 1 less than l 1 x 1 and l 2 x 1 And then l 2 x 2 Uh, no, okay, so we have l 1 is less than l 2 x 1 And we have l 2 x 1 less than l 1 x 2. Yes so this is Uh, this is another no this is not oh we just copy pasted it, right? So now we want to this case, right? So l 2 x 1 is less than l 1 x 1 and l 1 x 2 is less than l 2 x 2. All right, that that's that case Okay, there are also a couple of more cases actually Um, let's actually do it this way right This is equal to Well here we have the this case right to okay, and then we have If um L 1 x 2 is less than l 2 x 2 Um, then we have the case uh two No, we have the case one and two Then you can probably do like less than or equal. No, so we already covered the cases are equal then Else if So they're not they're not going to be equal, right? So else we have this case that Okay, so we have this case. Okay, so if we have that case Then the number of points of intersection is going to be um l 1 x 2 l 1 x 1 minus a l 2 x 1 Plus one right so then it's this one minus this one plus one Otherwise we have that It's just the length Otherwise, it's l 2 x 1 minus l 2 x 2 l 2 x 2 minus l 2 x 1 Plus one. I hope none of these are like negative All right, so I think we have this right like this case and then we have this case Otherwise, we have the other case if and Okay, so now we have the other two cases which is going to be the same, right? And it's going to be the same right or You figure out which of these cases we're in But now it's going to be different. Okay, so if if if we're contained then it's a l 1 x 2 minus l 1 x 1 Otherwise, it's a l 2 Otherwise, it's a hey janet in Welcome to A very painful thing we're going through now um l 1 x 1 plus 1 See let's see what is Yeah, it's it's it's a bit much, no So now we get 0 3 0 0 1 0 We're getting a 3 right Which is good. Um But are we getting We're getting this 3 right But so there's another um There's another one here. Uh, so what are these lines? Uh, let me see Horace infairs horace Okay, um So it's the 1 1 2 no 0 1 2 4 Jen pairs horace 4 So it's 9 4 to 3 4 um, and then 3 4 to 1 4 And exactly they intersect in one point So it's 1 9 4 to 3 4 and then 3 4 to 1 4 okay, so At least in the um At least in let's keep this pregnant at least for the example we we get the horizontal intersections d d d d d d d Okay, we get the horizontal intersections now the vertical ones And they're gonna be I think the same right except we have the One of the norm y and then we're gonna like and then we're gonna check if If l 1 then we check l 1 x And then l 1 1 1 l 1 and we don't care about this one in this case and we do I mean, it's the same except we transpose the lines Let's just do that. Okay t T line line to line So if I have a t line, okay, we have a line x 1 y 1 Goes to x 1 x 2 y 2 and we want to transpose it It's the same as saying line And y 1 x 1 y 2 x 2 Right, so we just kind of Flip that coordinate, right? I think that should be fine. Well, we can probably do that, right? v intersects v is equal to h intersects h um t line l 1 t line l 2 Right, so like, you know, we just flip it around and then it should be the same Let's see. Are there any horizontal lines vertical lines at overlap? No, right? And inverted here is the Left, okay, so we get the h intersects h we count the number of Overlaps there Uh, I don't think this will work, right? Because now we will have We we will probably we have to um Because we will have like points that are like this they're they're intersecting at all of them are like More than two lines are intersecting At a One point, right So, yeah, like, you know, like there's two vertical lines that are intersecting and they also go over Our horizontal line at the same time it's going to be like three there, but we won't get too many intersection points So I am not I don't think this is a good way to do it okay, and Let's let's change it around Let's let's let's just look at the map and then let's check for like if a point is on a line or not. Um, okay, we're gonna Screw this too much stuff Are we we still need the view range? Okay, uh, let's define data point Is equal to int comma int In the line Is a two points point point now, uh read parse line is a parse point read p point parse point is going to be do x one x is a read p and manjar comma and then y is read p and A point Return point Uh x comma y now this becomes a P1 becomes a parse point And then a string and then p2 becomes parse point And these are going to be two points p1 p2 Okay, uh Oh, yeah line p1 exactly Okay, now let's see if our lines still work Oh main imports itself. Wow What happened here? Let's just call this point show a p1 at let's say a instead create point crack and equals read p to s parse point instance show point where show point x comma y is equal to Show x plus comma Show y And then this becomes show p1 plus plus ish plus show p2 and this line is also wrong p1 p2 Yeah, let's see the p1 at This is gonna be point x1 again now. I think things should load and we still get our Vertical lines, right? Yes, and we probably still get our horizontal line. Let's delete all this Let's put it into hold just in case we Like needed later. Oh, so Okay, we don't need the generators anymore Okay, so then we just say Online Let's just define on takes a Line and a point now it takes a Point and a line returns a bull This will like overwrite a function, but we don't get point x comma y and then x2 comma y2 equals Okay, there's nothing to clean up here. So we so the point is on a line if Um, just a line is horizontal. It's like, you know, it's between the axis of the line is vertical if it's a It's like a nice Point is on a line How to find out of a point is online with an equation A 7x plus 2 right This is the thing like these are not actual lines, right? um so first Like x1 has to be less than or equal to x and x less than or equal to x2 Okay and Okay, and then we have y1 has been So this is like the check for the Whether it's contained by this the line segment, right? Now to find the line we do where So h the Like the growth rate of the line is y2 minus y1 right divided by x Uh formula for line formula for line I mean, I remember this Uh, you uh Loop formula. Yes, we're looking at slow. This is not distance Okay, a The equation of a line passing two visual points may be written as If x0 is not equal to x1 This equation may be rewritten as Yeah, okay, we always get two points on the line, right? Um No, I probably right Okay. Yeah, this is the formula for a vertical line, right? Otherwise, it's infinite Damage Um, okay, so then we check So If it is the case that We have it's in the point. Okay So let's just do it like that, right? If it is the case that x1 equals x2 um Then we only have to check that x1 equals x and uh equal to y And y is less than equal to y2 If y1 Is equal to y2 Then this is equal to uh y Is equal to y and x1 is less than equal to y x less than equal to x is less than equal to x2 now um We're only considering the vertical and the horizontal lines now So let's just so we'll probably have to extend this with like the actual formula later but We will just keep it at this For now, um Matt, maybe we are h a Right, so this is actually gonna be like this. Uh Yeah, this is it's just a lot of work this today, right? We didn't add it for an hour and a half and we haven't even done part of one Uh, okay. So now we know if the points are on a line Okay, and now we we map maybe vertical or h So let's actually like we don't care about whether they're horizontal or vertical Um, okay Go now let's uh, let's actually here. We're not gonna be right. Yeah, we're just gonna do maybe line We're not gonna check it with the vertical or If we're going to just otherwise nothing That may be vrh and then we have lns Okay, then we go uh a space Is equal to um It's a list of lines and returns The like the space of points that we have like the largest point space Okay, uh, this is equal to so then we We're just finding like the Biggest x and then the biggest y So do uh two points we do The line to point list of point points is equal to Of a line v1 p2. It's equal to p p1 comma p2 to and then okay, and now we have a max x of Of a list of points So let's just do two x chord to and x chord so these are so here we're actually paying the price for Having it as a yeah, but it's okay x chord is equal to point x comma anything is equal to x y chord of point y x comma Anything comma y Equal to y We say this okay, uh, so the space the space of list of lines is space of Lines is equal to so we map 2 point Concat map Concat map 2 points over lines a where points all points equal to This okay, so whether we say a maximum x chord This is gonna be that point Maximum x chord all points Maximum y chord all points maximum map map Okay, now let's say here then We you're not gonna we're gonna let's just say a print space lns main Yeah, and this makes sense. So the first one it's a 9 9 oh this is gonna be so bad Okay, let's say So gen points Let's see Haskell all combinations of a Of two lists It's like a nice a I like this one a Okay, let's just use it so all P's in points in space It's gonna take a point and it's gonna give us a list of points All points in space is equal to uh, we we get a point X and Y and we actually get a a comma b where a comes from 0 to x b comes from 0 to y Okay, now we have all points in space. This is extremely Bad right, but it's gonna be fine. So Okay, so now we check so first we go We check which points are on on any line All right, and then we check num lines on point We get a list of line and get a list of point and we get an end Num lines on L and s is equal and then p is equal to the length Of filter On p Okay, so then we can get like for each point find on how many points They are on Okay, so then we say Let sp equals space of islanders So a ps equals all points in space sp So print a ps right Main okay, we print. Oh my god. This is not All right, so because we paint points like that Okay, then we say a print map num lines on to the nest a ps then we get like how many lines are on and Then we say filter Larger than two Larger than equal to two And we say length and we get three But aren't we supposed to be getting five Determine a number of points where at least two lines We're doing like the very dumb way, right? But we're getting that there's only three points Okay, let's check horrors. No, we don't have that anymore a Okay lns, let's do it just like this lns map maybe or h Read input example Okay, and then we say sp equals space of an s We get 99 we get all a ps equals all points in space and api Sp and then a ps is this map m print a ps So 99 okay, so nine zero. Yeah. Yeah. Yeah So this is zero to nine one nine these are all the points Okay, then where you are So it's our on function. That's a bit somehow wrong. I think Let's see lns we have one line two three so one two three four five lines two three four six lines we're supposed to be getting right a long one today uh I'm going to grab Yeah, I'm gonna be I'll be wrapping give me give you two minutes Sorry about that. Sorry about that But when you gotta go you gotta go. Okay Let me say, um, all points in space. Let's just try and print this out, right? And what's the space here? The space is gonna be sp at point x comma y map m print l So we have a zero to nine five to nine zero nine five to nine and we have a Zero nine two to nine right so we have these two Okay, then we have nine to four three to four, which is this one No, yeah, that's This one And then we have two to two two one Which is this one? Okay, this is one two three four five six How many lines out there? Six lines. Okay one two three four Five six, okay, so we have all the lines so it's our on that's a bit wrong. Okay, so if we have a horizontal line Yeah, so if you have a vertical line, so this is a vertical line, right? And it is the right x axis And the y one is less than equal to y Two, right? These are correct, right? Let's uh Num lines on so let's not do this So let's say let's print map Um, okay, so it's gonna be p And then we're gonna say Usho p comma num lines on p So zero zero is on zero line zero one is on zero line zero two Okay, and Let's just map over the points, right? Okay, so Let let's just yeah, let's just do the literal print function. Okay, so p f And we're gonna do like this. I think it's like we can do four m Now so now it'll be good nice to be able to just be able to like say you know print x y and then So we just want to print this map. Well, let's just see here nine zero Nine all righty we are we're generating them in the opposite order We want to show that see the last line last, right? Okay, so this is a zero zero zero zero zero So one one one two two two Okay, so those lines are correct one one one two two two And then we should get a bunch of zeros which we yet Onto up until I think this is zero one two three four. Yeah until it says four here A zero zero so nine four is zero Wait, oh nine four is zero. What is nine five? Nine five zero. Okay, so the zero's row is and seven zero is one Yeah, seven turns one and then eight zero and I sure okay. That's the first row zero zero two one is uh so Wait, so one one one is zero Two one zero three one is one four one four one is one Print map. Okay. We have the list of line and we Do io nothing. Okay print map Linus Okay, this is going to be print line zero Okay, this is going to be map and print line over zero to Where sp equals space of Linus point x comma y So we are going to print line and it's going to use the zero to y Okay, and this is going to be yeah, let's call it pl. Okay now pl Nothing is equal to Booster New line Okay, pl nothing is just return pl y y's So, uh, this is going to be map m print point map and and then zero to x right um Where print point? a Print point y zero x the print point y a nothing is equal to put stir x n a print point y comma x is equal to a put stir show num lines on x axis put stir show num lines on Linus a y comma x x comma Y right a num lines on Point put string and then print Point a y axis pl takes in a list of ints and returns i o nothing and print point Takes in an int and a list of ints and returns i o nothing px py this is a py And this is a px and this is just y actually like this it should be a But now it's complaining Can't magic passage type. Okay, so the x here is going to be in y here is going to be in it Okay, uh, so we get the Lines Okay, uh, that didn't work because we didn't print the end of the lines Okay, so we have a there's something wrong with our on and hello equals num lines on otherness a Point x comma y out is equal to If and hello equals zero then dot Else Show hello on this one and it's gonna be Put string out Okay, so there's something wrong with some of our lines here Um, it's because they're not in the right order Okay Yes So why won't larger and equal to yeah, okay, so we're gonna change this actually Okay, it's x1 equals equals x2 and x1 equals equals x is Uh y less than equal to min y1 y2 and y lard. No less than equal to max And so let's do Min y1 y2 less than equal to y y and Okay, this is the This is why we failed y y and then x x x non exhaustive patterns in function on Otherwise It's false So now we should be able to get No, I don't have to print some map There's a Print map Okay, so now let's not print the map. Let's do have when we print filter larger equal to two Over a map map num lines on LNS And it should be eps Why you why does it do this? Five exactly Okay, now we are getting the right for task one And we're doing it in a very dumb way, but Let's just hope it works for the input. Okay Only 500 lines now main. Okay, let's change it to be Input and let's gt o3 Day five hs time day five This might take a while. Okay eight seconds, but We got a number Let's see Okay, that's the right answer So we're done with number one But oof we didn't do it in a smart way, right? Let's see part two is as horrible I've had to Consider all the words get full because you also need to go diagonal lines because the limits of the mod lines in the list Will only ever be horizontal vertical diagonal line at exactly 45 degrees in other words an entry like a One covers point Considering all lines for the number is one line You still have the number number of points where at least two lines overlap Okay So now we just need to extend our on Formula to cover diagonal lines Let's change it back to it Oh, no It's just to task one Okay, a num lines on Let's change this then to from map maybe vrh to just All the lines this is gonna be task two and we Don't change this much actually What's your company right now? All right, okay, so num lines on Num, let's try and just do num lines on diag Num lines on base num lines on filter on p Okay, so now we have a diag So p what so that's a It's equal to so it's either on a p1 l1 l2 p1 lp so this is just p and l all right, okay case on p l You need to go through it Okay, if it's not on a horizontal line Then we can check that Okay x1 equals equal x2 Then that's false, right? So if we if it's not on a horizontal line Or it's uh, it's not uh, yeah, so it's not our horizontal line okay, so Now we're checking if the point is on a diagonal Okay, now do we check that that was this is what we were messing up before it Okay, I mean it has to be within the box right so, uh so case So min x1 x2 less than equal to x and min a max x1 x2 and x less than equal to max x1 x2 And we have min y1 y2 less than equal to y and max white One y2 Less than okay, so then it's within the bounce of the box Right, and then we just have to check That it's on the line And then that then we can use the formula, right? Okay, so this is this is equal to if Okay, and we know that y is y x is not equal to x2 right so we check that here Then we can use this Point on a line thing, right? So we just need to check that Right, okay, so then Line will be defined by these points, right? uh So then we can check that This is equal to x minus x1 So we check that is equal to y is equal to x minus x1 times uh y2 minus y1 divided by x2 minus x1 Plus y1 and this can be Let's check this for the example. I think we have the corp formula collector Okay, we get um Right then it's because we didn't write num lines on dyag non exhaust the pattern in function dyag on dyag Yeah, okay Okay, we get 12 I think this will work It might be very slow, but it's gonna work Let's uh Let's uh, let's do it This might take quite a long time, but not like extremely long, right? It's kind of take like 15 16 seconds and we're doing it in such a dumb way, right? We're like We're not right. We're just generating all the points and then checking Quality right 18 seconds For an answer 17 741. Let's check if it's correct All right, we did it Took us two hours though And we're like we're not doing it in an optimal way, right? But I think like, you know, we accept everything as a solution if it's If it takes less than You know, yeah, if it's less than a minute, right, you know, if it if we can just run it and see what happens Then that's fine. Um How could we oh, thanks so buck chains So buck j So boss I don't know how to pronounce it But yeah, okay, so what essentially we did right nice. We We really hacked this Uh, so we could probably do it in a better way, right? We can probably like oh sweet So how long did it take you Laila? How long does it take with just oh Like if we don't don't do any optimizations, how long does it take? Yeah, because I think like the optimizations are important at least for us Because we have all these points and line things And I think like this will be you know This will be just removed Away Like so these will just be the inch comma inch, right? It won't it won't actually have this in direction Uh all the time Which is nice. Um Yeah, so I'm wondering like if we can Do it Like if we can find those points which are on any lines, right? And then kind of just count for the rest. Yeah, this is uh this can take a while so like I have the Uh benefit here that I am So this is running in the cloud. I mean it's not like a crazy machine. It's like a four core Yeah, just a basic cloud machine, but uh, it's better than Everything freezing up on the stream um And it's nice, right? Because I can just access it from this computer or from laptop and Yeah, okay, this takes a while Okay, two minutes though with no optimizations or like So I think the default flag Is like oh two That's uh, but uh, yeah, and that's like I think that's the trick with high school. Is that you know It really the optimizations can really they can really do a lot like they'd really kick in This is like without any optimizations and then a Run it again with optimizations and it's like 20 seconds Which is uh, what? six times faster And like you can see that like, you know Compiling and running it with the optimizations takes a lot shorter times than just Yeah, so like the optimizations the time taken to optimize is clearly worth it This all points in space probably speed it up a bit but Let's do this right Wait now at least are like minute optimizations So if it's a vertical or horizontal line, we do that Otherwise we can use the formula Actually, we can just always use the formula. Let's go and mix Yeah, I mean we we could like just compute so we I mean we're computing the numbers twice here, but I don't think that's the I don't think that's I don't think that's important So what would be a nice way to do this? I mean so we could like just focus on the lines All right and just find like intersections of at least two lines And I think we could do that right so we could We could take the program And we could like find the intersections of the lines And use that to generate the set of points that we check Right because now we are checking very A lot like a thousand times a thousand point a million points, right? I mean and we do it in 20 seconds. So I don't think we like need it but Yeah, I'm just thinking how we can sort of like a way we can easily speed this up You see do we use these two points? Yeah, we use it for the space Too much of a brainlet What does that mean? And how would that mean that you Don't understand Haskell Okay, so here's what I'm thinking we take all the lines and we generate the set of points on the lines right So for each line we generate all the points on that line and then we check And then we can like make a set of points and then we check Yeah, I don't know. I think I think we like our way of just it's very fast, right? And like yeah, we have like Like a degenerate case, right? You have like lines covering the entire space Then Generating all the points on every line would be a lot more I mean, I say I think we would need to like generate the pairs of lines And then you know we use all these Do two lines into our sect formulas Which we could do very fast, right? And I just filter out the points that are on more Yeah All right, I think but I think we've been going on long enough today Yeah, so what I'm saying is It's definitely a way to do this without generating all the points, right? But it would be very tricky to get all the line intersection equations correct And I am not sure that we would Learn anything. I did that then I use a map point int to count how often each coordinate is occupied by a line What what do you mean by that Timmy? How do they regenerate it all the line intersection equations My question to Timmy is like how long did it take like Was it a lot faster? Um, or is this like a reasonable time? Oh, yeah, yeah I mean, yeah that like like I said that works But uh, if you have like a degenerate case where Yeah, okay, but that's in Haskell to 50 milliseconds That my worry is that yeah, it works in in a kind of probably the usual case, but if you have a Like a degenerate case like where all the lines kind of cover the space Then Then this will be a lot more points to check. I'm wondering if we can maybe just filter out the ones that uh So let's see any To all points in space, okay, and then What I want to check now is we let's say on any Is equal to uh filter Let's let's let's let's do that, right? uh Let's see on Any on any okay, so we don't put the length here we do On any empty set is equal to false Let's take the point in first Maybe we can like drastically reduce the amount of checks we do like this if if we're lucky On any any p Empty is equal to false On any a p l lns Is equal to um if on dyag pl Then True, okay, let's just check do like this on dyag p l is equal to true Otherwise is equal to on any p Lns Okay, so these are so we're gonna filter out the points that are on any line. Okay, but let's actually Do we want to give it we want to feed it that way? point Okay, otherwise on any lns p Okay, uh now we don't we say instead of saying all points in space We filter those out, right? So we say Points on any is equal to filter on any Lns a aps Then we say Points on any Then we kind of check that we only check those points that are on any line On the the dream is that like if it's Okay, yeah, it doesn't doesn't really help. I guess because we're iterating over the points Yeah, how can we make this? I would print the map, but like it's it's at least a thousand characters. So it's not gonna It's not gonna help a lot. I think Yes, it's about 250 milliseconds. Yeah, I guess that's because there's a lot fewer Lines then there are points of points Yeah, exactly. Um But like we can see in the input right that I mean there's gonna be a million Points, um, but So let's See if we print the space, right? So I think there's like a yeah, so let's just see what this is. Yeah, so it's like a million points, right? Yeah, and they only have 500 lines So like we could optimize this for the input that we get, right? But uh, it really depends on so I this yeah really depends on if it's Okay, um Let's let's check one thing, right? Yeah, let's see. Um L and s okay, so we say H or v is equal to a partition is just v or or each, um Otherness this needs to be imported in this Partition part it T on data dot list. Oh my god. Where is it? Oh, right at the top partition takes in Okay, so The horizontal or vertical ones Hey, oh lover. Yeah, I think I agree with the lay lay left here is that there is And so let's print space print length h or v And I think there's a lot of horizontal and vertical lines Oh really Well partition should give us right h or v Diak Okay, so there are 349 Um, so the print space print length h or v and print length diag And there are a So a lot there are a lot more horizontal. I think they may probably like a An even amount. Okay. Um, let's do let's do it. Give this back into either line line x1 equals just So these are the horse vertical ones left l Y is equal to this right now Now I need to map maybe and I need to map Either id id And then we can say length so we say Horace is equal to left h or v And Where this is equal to right h or v. No, there was a this was a left H or v and these were the right, right So h or v here is on here Maybe either Yes, okay, so where you do partitions or let's um Okay, we just partition them first and then we say H and v lines is equal to map Maybe uh v or h lns And then this is h and v lines and this is h and v lines Length, uh, vertices length Horace, sorry. So, yeah, okay. So we have verticals, horizontals and Diagonal lines, okay, so And then we that doesn't take a long time right to figure out which ones are which Okay, um Um, okay, so what are we checking now? So let's Now we generate the points on each of these. So Horace H points To the line and it's going to give me a list of points. So horizontal points is going to be It's going to be a line And the Y coordinate is the same. So l 1 x comma l 1 y l 1 x 1 l x 2 Okay, so it's just me x 1 and y and x 2 And the same Let's just call it l points right l points l points A line y 1 x 2 y 2 Here we go. Okay. So if a y 1 is equal to y 2 This is the horizontal case Then we find a Where mix a So small x is equal to min x 1 x 2 B x is equal to max x 1 x 2 S y is equal to min y 1 y 2 And by is equal to max y 1 y 2 So if it's this is the one then we are going to generate map and we're going to map point dot Maybe we're going to take the x coordinate and we're going to give you x comma y And we're going to map this function over the list S x 2 b x hot tuple sections. Okay Which is that tuple sections? But it doesn't actually yeah, okay, we can Write this as this other points. All right, and then this is going to be point Okay, so we have the Ver horizontal. This is the horizontal case Case x 1 equals x 2 is equal to map And so this is going to be x 1 is equal to x 2 You want to say x 1 comma Over s y 2 b y Okay, otherwise and Yeah, this is going to be a Okay, so otherwise we have a diagonal case What is the set of numbers for the diagonal case? So we just like add one Right to like both coordinates until they Hit right Because there yeah, so it says that they're all exactly 45 degrees right so we can use that fact Oh, let's go back today for it. They're all exactly so that they're all going to be like x and so like the So put like yeah, so in which which direction are we going right? And then it's either going to be like plus one minus one Okay, so let's figure that out right a Okay, so and So if so we're going to define the change here So this is going to be diagonal points of Line I just write that like that or do we No, okay, otherwise. Okay, so let's figure out the change. So the c x c y is equal to So the change in x c x is going to be if x 1 is less than Or equal to is less than x 2 Then Then we're going to be we're increasing the x 1 then 1 else minus 1 okay See y equals if y 1 less than y 2 then we're going to be growing it then 1 else minus 1 Okay, then the points on the diagonal are going to be So then dyac points is going to be dyac points So let's actually let's sort them We normalize on the x Okay, so we're just going to make sure that they make sure that it's increasing x and We had that before right? That's why we didn't erase it. Let's say here formalize on the x Yeah, and I think we can just actually do this right away, right? Yeah, we should do this because yeah, okay Need input we normalize on the x. We just need to pick one to normalize I realize this now uh norm x Right, okay. Now we have them normalized on the x so x 1 is going to be less than x 2 um, okay, so x 1 is going to be less than x 2 So then cy is equal to um If y 1 is less than y 2 then 1 else Minus 1 Okay, so now we have the x dyac is equal to x 1 2 x 2 right And the y dyac uh The x dyac is going to be x 1 2 x 2 and the y dyac is going to be y 1 let's see You can't do that right? I can't do 5 2 2 right 3 No, yeah, exactly But then I can do 3 2 5 and reverse it right so if so If y 1 is less than y 2 then y 1 y 2 else reverse y 2 2 y 1 So this is going to be the y dyac and the points are just going to be Is otherwise equals sip x dyag We're gonna map point Over a sip x dyag y dyag Sip with curry point Will this work? I wonder So this will generate a lot of points Now then we change This aps here and we're not actually gonna we're just gonna we don't care about what they are Anymore we are going to say a aps So okay, we don't so we're actually gonna So we generate all the points. Okay, and then we just count the points of the We yeah We just can't we just check. Yeah, we check if there are any duplicates, right? So we can derive word on the points, right? Deriving or EQ or Okay Norm x is not in scope Not put it in Okay, now we don't have the aps anymore, but we don't actually care. We're gonna We take the lns. Okay, and then we so then we say let aps equals sort Dollar concat map L points Lns This will be a list of points and we do group, right? Import data on the list, which will group them Let me do group. Okay, then and then we do Um Ling filter larger than equal to two dot Ling so how many of the groups longer than okay concat map in the second Let gps because that's the groups gps is a list of just the points Okay And then filter Length just we're gonna do Yes, let's check this for example, this is That we're actually getting Nothing sensible here. We get 12 It happens really fast Now let's see. Wow Three now we're back to two mid-time All right We got it on to 360 milliseconds on this machine So right so there's essentially two ways to do it um We either generate all the points and Just check for We generate all possible points and then we check on how many lines those points are or we um Or we do something like this, right? So I think this still works. This was a dollar all points in space dollar space lns so 350 ms 20 seconds so this way it takes a 350 milliseconds 233 let's just say that It looks good otherwise The other version takes 20 seconds approximately So yeah, but This really depends on the amount of the The amount of lines Okay, it takes 13 seconds now, but yeah, okay Okay, but it as we discussed, right if you have a really dense If the lines are really dense, right if they happen if they kind of cover the space Then like the other one will be worse Um, but because they don't cover the space we have a lot fewer points um So let's even say here points on lines equals this and then we say a Points on lines and let's print dollar Length points on lines Right, so then we have a hundred and eighty three thousand four hundred thirty two Otherwise we have Like a nine hundred and eighty thousand points generated and then we have to check So every line Whether it's on the line But here we just have to Resort the points and then we check how many are equal, right? Or how many have how many yeah And like we can we can find out the By the length of the groups so we can actually just say map length And then filter larger and equal to two here Um, and I think that will uh, I know now we're doing both but I think it's also important to note that like This old version like we can actually print the map. Yeah, okay. I guess we can print the map with anyone as well But it's uh, it's not as trivial kind of all right Uh, we did it three hours. We did two ways the slow way And the good way Thanks to to me For inspiring us to do our best All right, uh, that's enough for today a I'll be back tomorrow Say Same time so 1900 hours in Sweden that's 1800 in utc And then uh, you will have to calculate yourself when it is I think it's like 10 o'clock in california something like that Um, but yeah, and we'll see how we do it How we do the next one. Let me actually Get status commit Yeah, let's add it get add d5.hs and Input an example and then get status commit d5 Get push All right, the code is up On github you can see the link right here And and then you can compare the two And uh, yeah, I'll see you tomorrow for the next one And uh, yeah, so I'm at tritlow on twitter and that's usually where I announce that I'm going live Uh, and it's also where I announce like, you know, if I'm not going right if I'm like Hey something came up. I really need to do it later today whatever so You know, if you come here and I'm not here Check the twitter. I'll probably be an explanation All right, uh, hope to see you tomorrow. I don't like yeah, and if you follow me on twitch Um, you'll get a notification when I go now live. So then you don't need to check anything you can just wait for the notification and tune in uh, and lastly, um Um, these videos are also on youtube So like uh, yeah, but they come out 24 hours after the stream because uh, which has like a Like you can't stream it anywhere else and or or host it anywhere else for 24 hours. So yeah But uh, you can check out the playlist there and I think day three and four I have to like edit it slightly and then it'll be available there And so probably yeah in the morning and then this one will be available Even sooner. I think it should be available. Yeah in 24 hours. All right Thanks again for tuning in and uh, see you all tomorrow Right. Bye