 welcome today 9th of advent of code in Haskell and we're doing an early stream today and why because there's a Christmas party and yeah you gotta attend the Christmas party it's gonna be a party like a Christmas party because the Christmas party don't stop I don't know you really say a question for the viewers question for the viewers it does the audio work because we had it last time not last time the other day what happened was that the audio worked on the recording it worked on my monitors but it did not pop up on the stream so which is said anyway I don't expect a huge turnout today because it's early people are at work people are busy yeah but I happen to have a free slot right now and that's gonna work out all right now let's get started with day 9 oh my god oh shh some rope okay this rope it's cricks as you walk along you're aren't sure okay all it is it's fine you do it the rope trust and twist you decided to strike me about the rope and rope physics okay consider a rope with a knot at each end these not mark the head and the tail of a rope if the head moves a far enough away from the tail the tail is pulled through toward the head to the loneliness with plank length you should be able to model the position of the knots and two-dimensional grid and then by following a hypothesis hypothetical series of motions your positive input for the head you can determine how the tail will move the rope will cook I sure in fact the head and tail will always be touching okay diagonally adjacent you know when those both counts okay let me just see what's the input okay the input is looking like this okay okay now if the head severed two steps directly up left or right from the tail the tail must also move one step in that direction so it remains close enough okay otherwise if the head and tail aren't touching and aren't in the same row of column they'll always lose one step diagonally to keep up okay okay so you just need to work out where the tail goes as the head follows as a series of motions assume the head and tail both start at the same position overlapping let's just paste in this example RULD a let's say anyway example a pasted in now we're gonna write the data type and data now we're just gonna do the writing show and data movement equals yes I was working on chess and yes it prints out chess characters that's cute right anyway a data movement it's gonna be a right up left down RDR okay so it's gonna be up or right or left or D deriving read my show and and then read input file path to a list of moments read input is equal to read at moment map read dot lines over read file okay now let's just read the example see that we've got a correct read input example in different like back when which was known as Justin TV okay but we crossed the example see we didn't have to write a parser because it's just the same representation as that okay because I both started the same position overlapping and then this was the right four steps then up four steps then left three steps and down one step after each step you'll need to update the position of the tail if the step means the head is no longer adjacent to the tail okay so let's see right four steps huh this is a starting position okay yes we want it once right and then we move some words and moves okay up up and then we start from there and then we move it up one step and yeah that's fine right because they're still diagonally connected and then we move it I mean which up again and then it follows okay then we go left then it goes like this okay and then down one step and then right okay and then down and then left okay and then right twice also go to top and then goes okay you count of all of the positions of the tail visited at least once simulate your tail complete hypotheticals here's a motion how many positions does the tail of the world visited at least once okay now let's so the state is just gonna be we don't actually care about the we don't have to have a diagram or anything we just have to have the okay so now let's say a so the initial state so let's say update okay so update let's see here let's call this actually but then I can do okay so say a moves goes from okay now I'm gonna update so I have the update so do so do okay takes in a list of moments takes a list of so takes in a movement and it return and it takes in the current state so Kurt tail type state equals it's gonna be a the position of the head and then the position of the tail state or we're gonna do it this way I guess I do and now if it so do move case and be zero it's gonna be sd so if we're kind of done with that moment equals so this and then it's just don't just do the state okay let's actually do this okay so we if we finished that moment then we do like this okay now let's so head movement okay so head move takes a moment and it takes in a comma int and then it turns and move is equal to so one it's just one movement right so you whatever ST so H comma Y okay it goes up then we go X plus one right we do X plus one left we do X minus one why and then the X comma Y equals okay so the trick here is that like a you can see here that the rope like it might move it might move on the sec like on a second move right okay so let's actually do this like this let's let's let's be a bit smarter about this okay and do move let's say but the state and then it will also have the visited set import qualified set import data dots data dot set that okay it's a do move okay let's say here this is gonna be very similar but let's just write it out for you and of the head this is hxhy hxhy and then it's a tx txty and then visit it now if we're going up okay if we're going up and the X's are equal then we just can go we can just go up two times right hx equals h tx then then we just do let me basically just do so then the new state will be a hx so we don't change the X hy plus n tx comma ty plus n and visited set dot union set dot from list and then we just we're just gonna say set a union set up from list map and we are just gonna map this over the list of one two one two and okay so that's how we move up if they are the same okay now let's do this for the other dimensions as well okay so for down for down it's gonna be if the it's the same but then we do minus n minus n and then we do minus i oh right and this is then the x t y right x y okay for left the same but then we check that the y's are the same then we're going let minus i okay a or right it's gonna be the same but then we go to the right okay okay so we got the easy case right now we will say okay because so if you move diagonally to catch up right it it always catches up right so we go are here right you know the first move is weird right because it skips this one but then the next one all else be okay so we are gonna say okay so we're going up and they are not the same otherwise if they're not the same then we the new state will be a do I have to have for a zero and be as the case and be zero let's just cover the zero case okay so now we know that it's more than one and we know that they're not equal otherwise I mean so the head moves the same right plus one we're gonna do one movement txt y then a so this the new tx becomes the the old hx okay so because we move in the right the same diagonal and we move one up as well okay and then a set that insert and we just add this new one right I checked T. Y. plus one into visited okay and then and this is gonna be the input to do move a you and minus one okay that's how we're gonna do it boom boom boom but I think I like this okay a no let's format this the same but we're doing it for D here so it's gonna be the one D and minus one and then minus okay and now similarly this one the L one becomes a now the HX's are not the same so then this will become H Y and this becomes T and TX this is HX-1 TX-1 and then this one becomes H Y and we insert HX-1 H I visit it I think this is how we do it if we don't this is not the right way we have to do some printing right we're going the right way the R a okay yes but do we need the ah it moves to catch up right yes okay so so this will actually be a so for the otherwise case right it actually becomes we don't have to do this we just have to do head right because this one will be you get that right because a because like it just moves to the position of the old but what about if we like have this case right like it and the head is going like back and forth and the tail is not moving so we always assume that it's kind of following and so that would be like the left one right it could go one step to the right okay so yeah so we have the left one like I said directly beneath like it could go one to the left that would be like this one right and could go one to the left and then off but then the distance is not yeah okay so messes up with the distance thing that we're doing okay so the easy case is still easy I think but so the the hard case that they're diagonal yeah so the problem is arising like when it doesn't actually follow like it doesn't move easy cases will still always work right the same and then I go okay but if they're diagonal and they're kind of it doesn't actually change the thing it doesn't actually change it doesn't actually move right so that would be the non-easy case so yeah okay so the head would be here and it would go it would be above and it goes with left one step okay but then it doesn't follow right so head goes left one step okay but the tail does not necessarily go one step okay let's say let's reconfigure it this way okay so now we're looking at this like that the left and then it's here and goes up one step and it doesn't actually let's actually pull the cable okay so then let's say that H prime is going to be Hx Hy plus what comma okay so this is the H prime so this will always be here H prime okay now the T prime T prime is okay and now a what how do I do the Manhattan distance I think this is just like the ABS plus ABS okay and this and let's just say this a there's an int and int a int int comma int int comma int to int this a equals a so it's gonna be x1 y1 x2 comma y2 equals a abs abs a x1 minus x2 plus abs a y1 minus y2 okay now T prime let's say T prime okay so H prime now and D is gonna be this H prime comma H prime we're in the current T this H and T okay now a this is gonna be equal to case so if the larger than so the distance is gonna be one but if it's larger than one then it moves so the new head yeah okay if D is larger than one then then we just do the move okay and then it's fine else do move else we are actually not moving the rope at all like the tail it's not moving H prime T okay this is the case like if when it the rope doesn't move that's actually de-duplicate these I think it's like this right so if I have the same axis okay any movement will produce movement if they're not on the same you might be yeah either have the case where it moves and the distance becomes too much and the whole thing moves or it doesn't actually change it just changes the H prime so it's not moving too far away right now so I would really like to be generalized over the up and left and down here but I can't choose a bit so okay but then now we have to copy this over here okay okay now this is gonna be minus one and then this is gonna be a okay it's gonna be D and then this is gonna be so if we move more than one we actually because this is that it actually becomes a new H we can simplify this even further yeah okay let's try and simplify this a bit more right we can say like a do hard hard hard okay this is gonna stick in the let's just copy paste right I think because maybe it doesn't work and then we have to rewrite it all anyway okay this is for D here and then we attach this one to minus one and then this one to L anyway this is what I'm talking about like premature optimization let's not clean it up before we know that's actually doing what we wanted to do H okay run moves takes a list of movement this set of the run moves equals a yeah and then no it's just yeah hold L a flip to move okay and then the initial state is gonna be zero comma zero and then I'm gonna follow on this zero zero and the visit it is set dot hey Hamilton zero oh what is the thing about no okay now let's see here a read and put two moves up run moves set that says we're getting 21 but it should be 13 so visit zero dot zero and then it visits a zero comma two let's what if we say let's just see what the first move does the zero zero one zero two zero three zero four zero zero one zero two zero three zero four zero okay that was the first initial my was okay then that and then we could go up okay so we're here and then we're gonna go up four two zero three zero one zero two zero three zero okay four zero yeah zero zero zero two zero three zero four zero what does it zero zero one zero two zero three zero up but it shouldn't be to getting the four zero in the first one right and this is the first one is right yes if they're on top of each other they also don't move okay so there's another case here a case a h equals t and then this one becomes h yeah so we're missing this case right like if they're could this be covered if we know yeah let's just keep it simple right one has to be the first case three checks plus one check minus one h y minus one now the tail moves correctly for the first one let's see that is bizarre hey run moves okay that's visiting zero zero one zero two zero and three zero i think i got that i think i mixed up the coordinates that should matter though and then it goes up let's just print okay let's just look at it matter the r4 so r4 does this okay and then r4 comma u1 the tail should not visit anything else here ah but it does and what is the case then so then each is not equal to t okay so then we we bumped it up the distance so i'm thinking that the distance should not be greater than i'm doing set insert each visited is that correct though okay so we're in this case right so we're doing we're doing r4 and then let's actually let's actually just return the whole state this one so okay is greater than one for the debug tracing and this is we're going up one okay trace show so they've gone right a couple of times it's actually here it's the i think i'm screwing up the coordinates a bit right so so okay up does not affect if the the x is the same as tx so the distance is two it's the distance so h prime is a four comma one yes it's three comma zero so the distance can't be one no yeah so that's distance one uh because we're not actually doing the manhattan distance we're doing the euclidean distance sorry about that a euclidean distance okay let's say okay and now it the distance should be more than or equal to two so it's not more than one but even more than equal to two let's see what does it say for you four when it goes three zero and then it jumps directly to four one and then four two then four three yeah okay i think now we got it print a set dot size dot search keep debugging um r4 u4 okay i think we figured out that this one was correct so where does t go here so t goes here t is here okay then it goes it's still a three zero then it goes four one four two four three okay that one worked fine ah this should be an r thank you sorry i mixed it up this one you passed it and now we get 13 okay let's let's do this for the input 34 milliseconds got it wrong that's not right the right answer your answer is too high let's check our code again so if we're going up we go up okay if so they're the same we just do that okay uh if we otherwise we we took kind of one step at a time we could even skip the skip the optimization let's see maybe it's optimization messing us up i don't think so but maybe six three three three okay so the optimization was messing us up uh why was that i mean we but we essentially the same amount of time we skipped the optimizations this is over the down and hx is and hy hy is equal to ty we just move them both okay we messed something up here in the optimization that's right let's just remove them and let's see you know maybe task two requires them but let's let's not worry about it if it's it doesn't actually matter then we just do like one move at a time that's fine let me just see what task two is uh now we go to day two yeah our ropes now suddenly it really looks like i don't even remember but it's still there some of the ropes that broke we're gonna wipe them towards you further yeah oh shit you only have a few seconds to choose how your arm your body don't wipe you you see what you're saying support a lot of ropes rather than two knots you was now simply consisting of 10 knots one knot is still the head of the rope and moves according to the series of motions each knot further down a rope follows a knot in front of it using the same rules as before okay so this is for one knot are we just gonna have 10 states that could be okay okay and then do we track the visited for all the knots which is too like a union at the end oh but it's only checking the tail of the rope then a okay do move we could do this kind of in a fancy way let's just see what the non-fancy way does and okay so a move without state okay this is going to be a rn move a non-tail okay rn and then we have the a jet hx come h y and we have the py we want to because i only move the head right so let's say let's say a let's actually yeah let's make this a bit nicer okay so tail move takes in an int comma in and it takes in an int comma in and it returns an int comma in now tail move a h t so this is h prime actually t a equals a let's just check it a d a case a this h prime t larger than equal to is equal to um this is just a equal to this right we're just gonna return bo int in command tail move h prime okay so let's just say like here so we're not going to have this case we're just gonna say h prime and then we're gonna say here we're gonna say a this part and we're gonna kind of pump this in if tail move h prime t let's see if this still works we're simplifying it a a bit now let's see this is we're not repeating things that's me plus one okay we still get the same answer we're actually a let's say let's say a a head change a movement a int comma int a int comma int now head change of r and ah let's just look to it like that so moment int and then moment comma int we kind of simplify it all we say u and a okay let's actually have here a mv st equals st a case uh we still need to have the case there okay anyway let's let's have it here st case moves mv 0 equals mv comma st so if this moment is zero we don't actually change it otherwise u n comma y becomes a u n minus one x comma y plus one left and minus one here oh well let me let me do it in the same order okay d d uh no this is d l l r r was one okay so do move a so if we we have a zero move it actually doesn't do anything do move mv h t is it it equals uh now where a mv prime comma a h prime is equal to a head change a mv now let's say this is equal to do move mv prime of if tail move h prime t the same here all right we clean up the code now let's uh not as much copy paste so it's actually gonna be a lot easier to modify right for the second one always still get the same result which is good okay now for 10 knots okay no uh for 10 knots okay this one actually just matters for the okay so for the first one first one will affect the second one second one third and i'm thinking like let's just do it the simple way right and then let's just see if it just crashes horribly or uh what happens like if it's super slow we need to fix it right um okay so it's just so uh task one is set uh takes a list of movements and gives us a need to ask one equals set that size dot third dot we're actually here and now read input no we gotta give an example or did we use a larger example yet large uh example example larga example larga we should make sure that it's an example okay hey how are you doing i enjoyed today's problem hey band band yeah i mean it's more of a problem than uh like you know it's not just pausing right but i'm having a hard time like i'm just kind of doing it right i'm having a hard time coming up with like oh yeah we just actually don't have to look at anything and it just works we just there's no kind of shortcuts that i'm seeing which makes me a bit scared that the naive way that i'm gonna do right now might be a extremely bad we'll see so maybe maybe what i can do is the following so i can compute the right so i'll i'll compute so instead of doing like movements i think like i can compute the list of moves right so then i just take the head and i run it through the moves okay then i get a list of the new positions of the head and then i can take that list and kind of say okay if these are new positions of the head what would the new position of the tail be right yes i will do it like that and then we don't talk about the sets okay let's check it out uh so do move uh uh move uh movement so we're gonna just never couldn't have school so i know what's the possible what is it i someone has drugs and maybe even classes right uh i mean it has type classes it has data structures but it's like functional so you don't really talk about struct so much i mean we have data but they're like algebraic data types all right so yeah that's that's that's possible okay so let's actually change this do moves uh do moves uh do moves uh so i'm gonna say movement this takes in a moment and this will take in the it will just take in the current head okay uh actually yes and then we will and then we will have a list of positions the tail is in an incoming in okay now do moves i don't care about the head uh tail t-paws equals the case and moves and v equals zero is just equal to t-paws now do moves and v okay so this is the head the tail is going to be the start of the tail so it's going to be the whole thing it's going to be confusing but it's okay okay ht equals do move uh do moves a mv prime h prime where mv prime comma h prime equals head change mv h okay and then uh if tail move h prime then the new list is gonna be a h otherwise yes else now i want to see so like okay this is do moves and i will have a running list of positions that the tail is in let me see what happens here e come at these let's see i print a do moves i want to just say r r4 and we start with the head at zero and the tail at zero so i'm gonna get three zero one two three yes okay now how do i fold this okay i want to return at the end i want to return the legs the state of the head at the end end point comma and then this one becomes okay and this is the state at the end of the first movement now i want to do moves many moves okay so okay so fold moves let's see so this is gonna be a do moves fold l okay and we take in like the the the next move to the current state and then what we're supposed to do up go subscribe movey jumala oh not bad all right i know ocaram said follow this one earlier huh we missed it up yes movey we are doing a haskel and we are even have a haskel water bottle not better i have to tell deva about this stream yeah okay right so what will this do so uh we will do the moves do moves let a final head t t tail and so t s prime so this is gonna be t s the final head at t s prime is equal to do moves a uh right this is gonna be the h h and t s and then this is gonna be h prime t s prime do moves a h t s okay but maybe i'll just fold like this i'll i'll just fold like this no and then a the initial state do moves this your state here will actually be zero comma zero ah the initial state will be zero comma zero and the initial tail is at zero comma zero and uh we're doing this on a list of moves r4 u4 and it doesn't like a here because fold l has i do want to do fold l i know that a okay so let's just yeah okay so we have the current state and then we have a okay so what does this do so it does this and then for one yes okay so now we have the list of the moves that the tail went through okay and then we can kind of discard the moves right a because we don't actually need that for the second one in the sense that a so i'll first move the head and see how the first not follows the head okay and and then i just need to see how so i don't actually check how the i don't check how the tail move i don't check i don't check the move i just check the the new coordinate for the tail okay then a this is do moves so this is the first one okay do moves now let's see if i can just do this with task one and hold the whole thing oh someone else called mean machine rex okay now this will return a list and then i'm gonna do set dot size and we're gonna take the list we get out here and we're just gonna say hey second and we're gonna say hey set dots okay so that's how we can do task one right so now instead of like just maintaining the set which is a bit cheaper we are actually now we have the full list of movements by the tail okay so then what we're gonna do is that so this this these movements right they are going to be there's like there's a lot fewer of them than there are for the movements of the head right because the head can be moving around but the tail will not move around as much okay so now we are going to write another function that says a do change that's just going to take in the kind of the in comma in and it's gonna take in the list of these tails in comma in and do change only okay so do change only so these are only changes so we don't actually have to care about zero moves now do change only so this is gonna be h prime right then we have t s at t equals if tail move h prime t okay i need to know where the head was before because i need to know what it updated to do which prime t you know how to update okay but i think i should be able to do it right the tail move okay i need again i need h over there also okay in comma in so h h prime if tail move h t then h t s else t s oh let's see i can i can encapsulate this right but i do change only h h prime t s right here this one okay it's still work yeah okay now this one is change only okay so i have a list of the movements of the tail right so a second of tail moves takes set that size let's look at the input right example so these are the movements of the tail and so these are the positions of the tail so let's actually there's a lot of mutation here reverse let's just reverse it here reverse it bring down flip it and reverse it i wonder if i get a copyright strike if i just nail that sorry okay so these are the list of the movements of the tail in the example so let's say we have now another so now it's a do change okay so now let's do it of one iteration okay so do changes okay so then we take a do change only yes okay so i think we're almost there right so now okay so do changes okay and do changes that's gonna take in like the current state of the dot not number two let's say now we're doing lot number two and then it's gonna take in the it's gonna take in the change and if this is gonna give us a list of the ints for the these are the ints for the where that one goes okay so this one is a so t and then this is h's okay now t and h's okay so then we say a empty is equal to empty do changes okay so we have to changes to changes a t head and edges okay equals a if tail move ht hd this is the new h prime then so this is where uh but this is where the tail list better than set from list better than a it's slightly faster because it uses the ord class so that's why i use it also i just like the set outside yeah it's slightly faster i think yeah so i so i feel like you know we have the information we need because now we know the exact moment of the tail right um okay let's see here uh maybe i need to do maybe i need to like unwrap it one more level do changes a this is h and then h prime yes this is how we do it h h prime so a if tail move h prime t then then it then it moves so and then we say then h do changes and we just add it to the list a h prime uh then and then the new one is up so do changes okay so then we have that we have the tail and we do changes so this is going to be the new tail is going to be h h prime edges else do changes key so i think this works but we need to have a case right where it's if it's only one left do changes t h nothing equals is this just empty uh might be right i think it's just i mean because what else can we do there wait if if if the tail moves it's just empty okay let's see here okay so this is the yank sample a now let's not print it let's say a t1 okay let's say x a xm okay let's do you know okay then let t1 equals a what do we say yeah tail moves a e xm okay now let's see here um um now let's print a let it not two this is not one okay not two is going to be a do changes and we start from zero zero and we run it on k1 print k1 print k2 so these are and so these would be the positions of the second knots so h covers so one h and then two is zero zero okay then two becomes zero zero then two becomes one zero and then three zero one zero two zero three zero and then it goes to uh one zero two zero three zero ah shit it seems to go to three one uh it goes to three so this is uh this is right i feel like uh i feel like a h would go up one and then it would be like the rope would still be here and then this one would be like uh and so it will pull it pulls that one up so why is it not like a you know three two one you get what i mean like why is it not like that okay we're gonna we're gonna do it the different way and so we get the tail moves we get the tail moves and let's just convert them back into moves but it can never move diagonally like the tail move yeah okay i feel like so i feel like i feel like this should not happen right i feel like moving h up here it should result in let's copy paste this right so it should go like this right so in the way that i'm thinking of it right did you go h and then one would go here and then two three four yes why does it pull the whole rope up i don't i don't i think this is you know i i could solve it this way right but i feel like it's bug right for instance like here why doesn't it do that right so here it pulls all of them up okay so it goes up and pulls all of them up okay and then here it just pulls it okay all right so this is some this is where our understanding starts to differ right which means that it's so h went one up which took uh one one up i don't feel i feel like it should not matter right this is the other way why not so yeah because it's like simultaneous movement right but i feel like i feel like this should not happen i feel like this it should go up and i will pull the one but ah okay that pulls the one to here and then yeah okay so that pulls the one to there yeah and then that and then okay so the intermediate state right is h right and then one and then particle physics right so that and this would certainly do it and there's course here so the spacing is correct okay and then this one pulls it twice okay so this is just the that's how it is okay uh so we just need to fix our so we have the tail moves are correct we just need to fix our to change only okay so i have a so i have the current head okay we just need to put more logic into this so i have the current head and i have the current like the new tail so it's not as simple as with the other one okay i have the new tail and and and and then so and if the tail should move okay then we just need to compute the new one else we just keep going to change only to change only okay so if that tail moved didn't do anything we just continue h r okay so uh h prime do change only uh you know this is h yeah do change only h prime a a r ah so i feel like like we're not having we don't have a lot of viewers right now but we're getting a lot of followers like subscribers and off-subscribers followers so we're getting more uh like maybe people don't have time to watch but they're like oh this is interesting okay if tail move h t then so then it moved and then we kind of record the previous position but the h prime a h prime equal and defined in now let's say write this h prime okay so how did it move a so we can't just say that okay a let a hx no this is actually t but then h is and then h and r if tail move h t uh if it doesn't matter the order then as of the new so the new head this head prime okay h is a h prime okay if the tail moved then we have h x h y do change only uh and this is this is going to be h prime and this is going to be no this is going to be t and this is going to be key prime uh and this is going to be so this is the old h right h prime and this is the tx dy and this is the old t and then t prime equals so we know the distance and so it will be pulled in the distance of hx so i think it's just hx minus tx so in the distance yeah hx minus plus tx comma h y minus ty uh no i mean this will just be that yeah okay this will just be that right so what is the difference between because it's not like this right it's not just a h y minus t y plus ty so it's not it's not the change is not like that and but i think it was like that in the first one that's why it worked maybe i can do it here let's see yeah then we can actually also test it um so this is kind of the two moves okay so this is actually there so this is what this wouldn't be the new head and the current state of the tail okay this is the new head and this is the current state of the tail now if tail move h prime key then it do change only so if the if it should move the tail then otherwise it's just t s right and now here we're gonna compute t prime based on the let t prime equals okay so now we compute the change so the tail move means that a change so if it's diagonal that means like it went to okay let me see so if it doesn't move it doesn't matter but if it did move then it's a more larger than equal to two set it to be and it only it always moves a it always so it and then it will it will move only one right and t prime let's see here h x h y equals h prime okay g x comma t y equals t now so t prime is going to be equal to is it's not the old head it's actually going to be so one of them is going to be two and then okay yeah so we're gonna say a so we have the direction so it's t x plus a x diff and then it's t y plus y diff and x diff is going to be uh so it's going to be one okay it's going to be uh uh but we might be yes it's always going to be probably going to be moving one okay so if a so if abs of uh but we might be going like left and right also a so we kind of want to normalize it so it's it's it's going to be hx minus tx and do I have sign sick signum uh signum hx minus tx okay and why diff so this is going to be like one or plus one or minus one signum uh okay h y minus t y ggi let's see here signum and sign so signum of minus two minus one signum of two is one and signum of zero zero exactly okay so we still get the right one for do change only okay so we we figured this out correctly so we figured out how it moves based on the new coordinates okay now we're still getting here uh uh this is the tail moves now we had change do change only yeah okay so let's see here do changes now let's see can I do fold out flip do do changes zero comma zero a gms so the do changes takes in uh do this is do change do change only right yeah and this one doesn't work okay then I think it may be just there is so fold l takes a b and a in a b okay yeah so it's supposed to be it's supposed to be flip here uh fold l and it takes a right and this one is actually it's not a zero comma zero it's actually gonna be empty list right and then I have to reverse it of course so it starts at zero zero and then one two and then two one and then three one yeah so it goes a zero so two goes a so it's it's it's zero zero right zero zero and then it goes to zero one uh one zero yeah and then it goes to two zero yeah and then it goes to three zero zero and so the the problem is really that ah okay it's going three one right yeah jumping directly so zero zero and then one zero and then two zero three zero one zero two zero three this one is at three zero right so it moved uh but it seemed to move in both at once okay yeah that makes sense because we did the signal right so I think that's the problem right so it's at two three and then the tails at three zero and the three zero goes from three zero to four one okay and this one should go then from two zero should go to three one huh am I confused about something here uh so two is at zero zero yes zero zero and then one zero and then two zero and three is at three zero so one three zero yeah and then three goes to oh you four yeah so three stays there and then three goes to uh so it goes from uh three zero to four one yeah okay and then the and then uh should it like pass through the intermediate steps this is a long problem today okay let me see but we do calculate it correctly for the heads but so the problem is when the change is more than one if you never jump directly uh but you should go to three one oh sorry yeah so it's at two zero it's at two zero and then it goes to three one yeah okay so I think I'm doing it correctly now and it stays at two three one and then it jumps to four two yes okay and then it goes to three three okay I think I got it now okay so now we just do this uh 10 times sorry I was confused myself so I actually it actually works but I was just reading the diagram wrong I felt like it to jump straight up but it was jumping particularly okay so uh I think we can do like so we we will like recursively so this one takes a a list of uh so a not takes a list of int comma int and returns a list of int comma int not moves not moves equals a fold l flip to change only zero comma zero now a a so let's say a to a to a to list of a's iterate okay so we are going to do here a and then we always reverse yeah reverse okay so let's say a iterate not moves a and we start with dms now let's say a map m a dollar print a take to map m print and it doesn't want to do this because uh no map m print what is wrong here I'm not using okay this should be the first list and the second list yes what if I take 10 here okay now uh exm 2 example large exm 2 at deal moves exm 2 I'm gonna apply it 10 times a ding ding ding ding and take 10 and I don't need the so I can just say take 10 and then I do last okay and I'm actually just gonna print print last and then we say uh not moves tail moves exm 2 and we say here a set that from list a set that says so for the big one it's 31 is that 36 positions uh man oh maybe we're doing it too many times yes we were doing it one to one time okay now let's do this for the read input into print set size last iterate not moves let's call this task to ask who takes a list of movement and uh uh print task to be like that no and it doesn't like this multiple declarations of task to two four five five five all right we did day nine damn yeah okay we did it in like an hour and a half but I was confused for like half an hour by if it was if I was doing the right thing or not because I was like ah jumps from here and then I was just reading the coordinates wrong anyway so the trick was to yeah just kind of define this function right where you like you first move the head around and instead of doing it just by the movements you actually compute the you actually compute how it moved right so instead of the first one you use the set because it's a bit faster but then we actually wanted the list of movements and then you know the list of movements grew progressively smaller right so first we had a whole lot of movement in the head right probably you know if you count up all these or all of the ones like you sum up all these numbers in the input it would have been a large number right but then we just moved the tails a few times right and so that was fine and then we can just compute the moves of the tail right and then we can just do that iteratively again and again and again right um yeah but it did take us a while to well not implement this function right it just took us a while to make you know convince ourselves that it was correct anyway I don't think we did a lot of fancy Haskell this time well I mean the iterate function is nice it's very idiomatic Haskell I guess like point three just map some functions over it um this iterate it's actually lazy so actually it won't compute it computes kind of on demand right and it would be kind of very cool right so to compute um so when it's kind of computing the ninth iteration right it's it's calling iterate on the rest of them until they get an output and then it uses that output right so I think it's I think it's cool anyway let's add this to the repo git status and git add dh d9 this input example large example large git status I'm not gonna add the chest example here that's just something all right thank you for that and git commit git push all right this has been our morning session we're gonna return tomorrow maybe same time around 10 12 oh right 10 11 um because I have visitors coming in the evening and I want to be done by then thank you for sticking around but yeah it did take longer than expected but this is how it gets right it gets harder and harder this is how why we it's also good to kind of start early and then you know we're in the mood right we don't skip the easy ones we do them and then we're ready to go all right thanks again for tuning in and hope to see you again tomorrow 10 o'clock european time so 22 hours from now all right see you bye bye