 all right welcome to the habit of code day 15 yeah it's getting serious we're 10 days now oh Oscar Allen I think I'm not sure if I even asked it this long before not with because we've done every day we haven't done in double days no rubbish it's been on we've done some early streams some late streams and like today we're an hour late because there was a speed chest championship going on and well I accidentally tuned in and then it was just a crazy exciting anyway commentators were good also so we're doing day 15 I've heard that part 2 is especially hard to do so let's hope we actually work it yeah okay so there was sand falling into the cave we were in and what are we doing now okay the stress signal yeah we went to the stress signal yeah tunnels you don't have to research them all you know you back to disability sensors okay since I'm very powerful well that's okay your handheld device indicates that you're close enough to use them okay so it has yourself your heart surface it begins monitoring for the nearest beacon since the big noses in the internet each sensor knows its own position and can determine the position of a beacon precisely our sensors can only lock into the one beacon closest to the sensor as measured by the Manhattan distance there's never a tie okay so we got an example here let's start by parsing okay import you tell data input so a equal sensor so we have the look of the sensor and we have a we have the let's say here look and let's just call us okay and we have the beacon be in college deriving show instance read input where reads Prec readers Prec reads Prec equals pars Prec parse where parse equals do string sensor at in a we do a parse pause x x okay so this is going to be L x L words pause y then char colon string oh yeah sensor at skip spaces char skip spaces string closest beacon is at a b x parse pause x be why parse pause why ah and then there's a comma in between jar comma skip spaces same here okay a return return s lx lx l y a b b y right okay and parse pause parse pause see equals to char see a char equals and then reads s reads s to p read reads add in language ghc 2021 parse pause this one needs to be intended to one more okay now what is main do so we're gonna do a read input there's a read input read input file path into IO list of input classic read input equals fmap read dot lines dot read input this is the 15th example into print doesn't like this because all right let's see if we got it preload no parse okay let's do then look into trace show n import debug dot trace let me actually just add that to utils okay module debug trace import trace trace we do that so let's see how does it fail x equal comma x equals to closest beacon ah closest beacon is edge okay now let's say is it the parse pause it doesn't work okay closest beacon is I just give basis here okay sorry about that hey Maddy ah the line is here yeah and Oscar Allen yeah but oh anyway okay we got the sensor input now sensor at x1 so consider it sensor at 2.18 plus beacon it is at minus 250 for sensor 916 plus beacon to it is at 1060 drawing sensors okay let's actually write cuz we're gonna use the Manhattan distance let's define that in utils Manhattan let's call it M dist a Manhattan distance takes int comma int int comma int and this is x1 x2 x2 abs let's just Manhattan distance it's like a yes abs x1 x1 minus x2 plus abs y1 minus 1 2 and it does not like this because it's actually what I want right and then it works too it's like this okay this is a Manhattan distance okay got the Manhattan distance down okay so consider the sensor there is s and p the movement arrangement of the big and it's like this this isn't necessarily a comparison of all the beacons in the area because each sensor only identifies it's closest beacon if a sensor detects a beacon you know there are other beacons that are close or closer than that sensor there could still be a sensor as big as I just happened to not be the close big sensor closer than sensor at 8.7 this sensor closes the beacon is 2 comma 10 and you just do not disturb here okay as I know there are no sensor beacons that close or closer any position marked none of the detects beacons seem to be reducing the distress signal so you'll need to work out where the distress beacon is where working out where it isn't for now keep things simple by counting the positions where a beacon cannot possibly be along just a single row so suppose you have an arrangement of beacons that you don't see like an angle above just in a row where y equals 10 you'd like to count the number of positions a beacon cannot possibly exist the coverage from most sensors near that row looks like this consult our partners as you just in the row where y equals 200 000 how many positions cannot contain a beacon so let's say well this is a hard one today so there are eight comma seven so that's the eight seven and the closest one is at two ten eight seven and the closest one is at two ten okay so let me first kind of look at this a bit map m okay let's um let's actually just add here okay it's not part of the input but I'm just gonna add it e and it's gonna be the distance return let d equals mdist let l equal yp equals d equals mdist i'll be and then this is mess with all that just a language call it's called a record wildcards record wildcards explicitly export this and this okay okay so these uh this is the distance for which we know there are no sensors no they talk to be able to do that actually no the alternative position where a beacon cannot be present how many positions cannot can the row where y equals 2000 200 000 so we're talking about like here this is gonna be 20 and just in the row where y equals 10 you'd like to count the number of positions a beacon cannot so like okay I get it for like 10 or stuff like that but like for 200 000 no for 2 million so I I want to say just zero it's not zero because I feel like we don't know anything about that far away because all the data all the data kind of paters out right okay right so the input is completely different from the other okay e sorry of course so in the right where y equals 10 there are 26 positions where a beacon cannot be present okay and so let's map out which which points I have data about let's look at the input actually I got confused I would like yeah it doesn't make sense this question for the example but of course this question is about the it's about the I feel like um like we should map out the points that are within distance five and there cannot be a clash okay yeah okay but that's not gonna work right that's not gonna work for um that's actually not gonna work you know here we cannot just map out all the points because it's gonna be an extreme amount of points okay I mean we did the parsing quite quickly I have the distance so for y equals 10 right okay so we can do something simple right okay so task one okay we're taking the and we're taking the list of points we're turning into the task one so what is the farthest point so first question is which of these are giving me information about y at all okay so here we have y so let's do um let's say a filter let's just say input filter so we have s we don't care about x here y okay but the big end d try again okay and then y and let's just say that the abs target minus y so this one has to be less than or equal to d no let's say task one here right of 10 so we're gonna just say okay so these are the ones for which which have any information about that y okay so right so like um because the y's are likely yeah so these are the ones that have any information about the y's let's see okay and then we want to let me see here now okay and now let me just transfer this into in comma in comma in this is the look and I'm gonna say the first filter and then we're just gonna map map s into s dot l comma s dot d we're gonna use language language all loaded record applied okay so these are the points and then these i say this is the distance i don't care about the well i do care about the beacons in the sense i care about the beacons at r at y 10 okay so um we have to count the beacons okay where backs equals a length filter s s a i don't care don't care uh b don't care target so because we gotta count the like the the ones that have the beacon there right because obviously a the question is you know how many positions cannot contain a beacon right so and then there is a beacon here right so that you definitely contain a beacon so now i want to compute the projection of this point onto the y right so we have the line and it's like we're intersects so this 12 14 right for example that uh oh a subscriber matt is holy quaternity all right oh no subscribe for our follower also has to get some follows and so 12 14 closest beacon is an x 10 okay and this is the 12 14 and the closest one to that is the 10 60 but we're looking at line 10 here when it doesn't reach there but the distance was one two three four yeah okay yeah okay so so i want to know how far away are they from how far away are they from um the line okay how far away are they from the line a so let's have another hint here and we are gonna say so let's see how far away they are from the line now map there's gonna be another hint another hint map a x comma y comma a so let's see the mdist of x comma target so let's just see at so it's gonna be c comma mdist x comma target so let's see mdist cx target okay this is how far away they are from the line this one is four from the line and if it's exactly four from the line that means that and there is one point there's one point on the line which is exactly so okay so the thing is here right so this is one eight seven and the distance of eight comma seven from the line is three okay but it's saying nine and that gives us a one two three four five six seven eight nine ten eleven twelve thirteen that's telling us that these thirteen spots which is a so that's like okay so this that's one plus so that's a so contributes let's say map a c comma dld so that contributes a c dld and that contributes a one plus d minus dl two points so that one contributes that many points on the line okay so this one gets a 13 10 okay now actually let's make this into the range okay we don't care about the we actually don't care about the y here anymore so let's just drop this is going to be x equals 12 there so this is going to be the range and this is going to be the range x minus d comma x plus d I think okay no so this one should be contributing okay but you get what I mean right like we're gonna we're gonna get the projection down okay let me see we get this straight okay so this is a only s equals s equals s comma l comma s comma d map s okay only s now okay check this again let's just let this is there okay and then we are gonna say that the projection proj okay so we actually find the a proj of a s comma y comma d is equal to x comma mdist c comma target so this c at mdist c x comma target comma d okay and so we know that it's gonna be the range so let's actually jump to get the range stuff from here let's see aim to arrange okay x this to the number of points should be the number of points should be what do we say there um p p equals one plus a dist so this is going to be the range x minus p plus num I mean this is wrong actually this is not giving us the so this one should be so this one should be the point you know 12 12 the point 12 12 this one should be a bs this one should be the point 12 12 now is this 2 to 14 that's a possibility the point that's an 8 2 yeah 2 to 14 point 8 to 7 is this one this one is contributing 13 points yes so it's ends at 14 that is true but so zero is here zero one two yes okay so these are the correct ranges want ranges okay so let's do a sort import data dot list sort now we're doing the same as we did here we are merging all the ranges same as here a copy this and we say this is going to be merge range and we need to export it okay and now we do a so if you do merge a list of sorted ranges merge ranges okay we do this in u2 merge ranges into into into so we assume it's sorted merge ranges equal so yeah not so if we have a you know a and b and the rest then we can do something merge ranges rs so this is otherwise it's empty or there's only one range okay we can't do anything okay so a case merge range eb of uh if it's just r or rs or ranges so it's just r then we do merge ranges r come on otherwise all right and then we just say okay let's say map the sort map to range merge ranges okay that did not quite work out seems like there are some issues here and okay so the start okay let's say um okay so if uh so they are merged wait so what if i sort on it i keep editing it daily for this one why if i sort these again what does merge range do that's the function here right it's trying to merge uh merge two ranges right like you have like a range which is set of numbers from some point to some other point and what is the problem here i feel like okay i don't i apparently don't have a case where like if it's one is contained within the other so if i start uh because now they are okay so where what are the cases so the let's see they're always sorted that's for sure okay case a e2 less than equal to if e1 e2 is less than equal to e1 and then we just go from e2 is less than equal to e1 and s2 is less than equal to s1 wow uh no s1 is less than equal to s2 this is equal to then s2 comma e2 very arranged s1 let's go e2 then we just prefer one over the other okay so s1 is equal to less than equal to s2 ah no then it's just actually s1 e1 so let's see if e2 is less than equal to e1 this is just e2 this is where you want to be writing on some paper right intervals ding ding ding ding okay and check the general test okay okay let's just list up the cases okay a b c d a b c d so merge range don't care and then c comma d and the case is that b is less than c equals nothing okay merge ring ring then okay so then i do s c minus d is less than equal to c minus b less than equal to 1 so if c minus b is less than equal to 1 then it's just a a just a comma d okay um and as always less than equal to b okay this is the first case a b c d no we have the case uh okay a b c d okay we have the case where um so i think this is the only case where we merge ranges right okay then we go from just a to uh okay and this is the only case right so when the case a and b yeah okay and they will be sorted so this will be fine okay now we want the we're gonna go back to day 14 we're actually gonna undo everything here get day 14 a i had like the length of the line got range size let's see i'm working on okay and sort merge ranges uh okay some map get 27 here okay and it's gonna be this our size equal our size is gonna be some of the sort of matter range my projections map only s and this one and i actually have to do this as well and then this becomes a and we also so it becomes our size minus begs right because we know there's one beacon there uh you just see and we have the same beacon multiple times tink tink tink tink tink tink let's uh do a map as s dot b oops and filter target and we can just the beacons for data set set import qualified data dot set and set set that size 27 1 uh okay we do now get the 26 which is like the we did the production and blah blah okay we're we're doing it quite efficiently i think now let's see for the input and then this one is a two million okay all right we got part one that's a easy part apparently let's see if we can do part two fast i mean we did a lot of the we did a lot of the we did a lot of the work yesterday with all merger ages stuff so this one is uh okay okay your handle divising is a certain form of a this is not a certain way a sensor but that this is speaking of what we have x and y coordinates reach no lower than zero and no larger than four million to isolate this as we figure you can determine its tuning frequency we can only be found by multiplying its s coordinate by four million and then adding its y coordinate in the example above the search basis models and x and y coordinates can be a most 20 with this reduced search area there's only a single position that could have a beacon the tuning frequency but the only possible position for the distressed peak okay a with this reduced search area there is only a single position that could have a beacon okay so we kind of eliminate all of the spots this can be found by multiplying it okay and so four million so we were looking for we were taking all the ranges and it would be such that there will be like only one spot that does not have the there's only a single position could have a beacon okay let's see um task two okay so now we're gonna we're not gonna do the air size okay and so we don't have to search every combination we can basically do a linear search a otherwise let's see so that's for y 11 okay let's see here a let's print task two where let's look for 11 minus 13 to 15 okay up task two from zero to okay let's just get here in okay and now map m print a task two right okay and i'm gonna say a map map m print and this is gonna be a flip task two imps and we're gonna map that over zero to 20 let's just see what we get ah so there's only one place where it's possible for it to be let's see a input imps input imps okay filter links let's just see how incredibly slow this is there's no there's no one that's exactly the one okay how many are not uh equal to one if you go to four million right ah i get one okay um okay so let's say just a task to input aims i i'm gonna just say you know actually oh we do want to remember the range okay so let's do here um task two target okay so there's not going to be the target okay let's call this task two prime task two is equal to map i i comma test imps and we're gonna map this over zero to four million and this is gonna be the imps okay map this over the imps i'm gonna say a filter uh equals equals two dot on s and d okay this is actually task two prime okay and then we get the two ranges okay and so we're gonna say something you don't care about start care about the second one equals uh yes filter to oh yeah and it's gonna be i comma x no yeah we're checking the y's x comma e plus one let's see now let's do it with the example first let's take in a max here zero to range and we search for task two point d here okay and it immediately comes up with 1114 and then we need to say x coordinate by four million okay so y is equal to e plus one so this is going to be x times a range plus okay here's actually times four what was x comma y here maybe i'm doing something wrong uh right the x is 40 okay so yeah sorry it's gonna be y x equals e plus one x times four million for the example we get five six million 11 input we might actually let's check this number but i'm a bit afraid that it's a boom accent all right we got day 15 not as hard as i thought it would be uh go then well math is holy quaternity yeah i mean i we did all the hard work yesterday with all the range stuff and how to merge ranges sounds like that and a trick is here that there's never any clash so there's actually just one range so that made it easy a a lot simpler and you know it becomes a linear search right so we're not actually checking all combinations we just do it for every y and we could probably have sped it up by you know don't something anyway get status get and day 15 day 15 all right but that was uh day 15 for today uh sorry for being late but jess is pretty good and tomorrow i'm going to a christmas party so i will also yeah so i'm gonna stream at 10 o'clock european time so yeah so 10 so that's nine utc well okay not 10 o'clock exactly maybe more like 10 15 right so because i got teaching so i'm gonna stream a before lunch and then i'll have friday off and then yeah we'll see about saturday i have to figure out the plan then but yeah probably i can write some sort of kind of morning stream or late afternoon streams we'll see about that anyway thanks for tuning in uh and hope to see you again tomorrow all right bye