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


Hello, welcome to SSUnitex, Lucille this side and this is continuation of PySpark interview questions and answers. So in the today's video we are going to see how we can get the department wise nth highest salary for the employees. So we are having input data frame which is containing total 4 columns and in the department we are having IT department, we have the HR department, we have the sales department. In the output we are picking the employees who are getting the highest salary in their department. So for the IT department we can see the employee ID 3 is getting the highest salary. For the HR department we can see ID 4 is getting the highest salary and for the sales department we are having tie breaker for the 4000 salary for the ID 7 and ID 8. On those scenarios we will be getting both the rows. So we can see ID 3, ID 4, ID 7 and 8. So these two for the sales department. So how we can write our queries for getting this output? So simply first we are going to create the rank and this rank will be going to generate on the descending order of the salary and it is also reset on the department change. So like we are doing the partition on the department first and then we are generating this rank. So as we could see for the HR department so here 3000, 2000 and 1000. So for the 3000 we have 1, for the 2000 we have 2, for the 1000 we have 3 and once the department name has been changed this value reset this rank and here again it is starting from 2500 for 1, 1500 for 2 and 1000 for 3. Similarly if we are having the same values for the salary we are getting same rank and once we have created this rank we can simply filter out based on the highest salary we are getting or the second highest salary or the third highest salary we can simply filter out and in the output we will be seeing this output. So let me quickly go inside the browser and we will try to do this in practical. So here let me try to create this data frame that is DF. It would be having the same data that we have seen in the slide. Let me go and try to create the rank. So before going to create the rank we are required to import the function and import the window these two options. So let me use the from pyspark.sql.funcense then we can go import as tick. Second we are going to use the windows function. So we have to import window as well and we can import as tick. Now here we are required to write our logic. So our logic is very straightforward. First we are creating a new column that will be rank and it is going to have the dense rank because we are having the same salary for sales department and we are going to create the same rank for those. So here let me try to create another data frame and this data frame could be DF underscore rank and here let me try to use the select and here let me select all the column first and then additionally we are going to add one column and that column will be rank. So for using the rank first we have to use the dense rank function so we can simply use the dense rank and we can specify this bracket and after that we can specify over clause and after this over clause inside this over will be going to use the window function. So we can go with the window dot here we are required to do the partition. So this partition by inside the partition by we are required to do the partition on department name column. So DF dot department underscore name. So this is the not underscore this is the department name column only. Once we have done with the partition by then we have to go with the order by and inside the order by this order by should be on salary column and this should be on descending order. So let me try to use the display of this DF underscore rank. Let me try to execute this and we will see the output of this. So it will be adding a new column and that column value it is having like one two three as per the descending order of the salary and it is going to reset once the department name is changed but this column name is not proper. So let me try to add the alias name for this column and this could be rank. Let me execute and we will see the output here. So now we can see this rank once we have done this next simply we will be going to filter this DF underscore rank data frame. So we can filter out and here we can specify the condition. So condition is DF dot rank column value should be equals to one. Let me use the display of this. So actually it is not DF it is DF underscore rank. So the data frame would be DF underscore rank. Let me execute. It is going to filter the rows which is having the rank one. So we can see this rank one. So this is the highest salary we are getting. We can pass two for getting the second highest salary similarly we can pass three and four like that it will be going to filter out. So I hope guys you have understood how we can write the PySpark query for getting the department wise highest salary. Thank you so much for watching this video see you in the next video.


What is going on guys welcome back to the channel critical over here So we're gonna be talking about Buffy the vampire slayer in his video here today We'll be going over this pause reboot revival from Disney and 20th century Fox and Monica brings recent comments that she made not not that recent but like a year ago that Came out before the same comments were made later that same year from it from an executive producer to let us know that The show was on pause for the time being but not officially canceled And then we'll also be going over more on why I think this pause could be related to kandahra blake's new series that she has been releasing of Since her first book dropped in every generation and just why I think it's an appropriate next step to take in the Buffy verse so Monica brain who we know was attached to be a part of the revival when he was announced in 2018 Had actually confirmed that the show itself was stuck in development hell in February of 2022 This was during an episode of slayer fest 98 shout out to you Monica recalled that she was hired to reboot Buffy and that she wanted to put a new girl up against vampires But Hollywood being Hollywood and IP complications being what they are combined with the pandemic had left the show in the ether for now And she doesn't know if the show will even move forward at this point But then we know shortly after later that same year a producer came out and confirmed that Buffy was on pause as of now My thing is from her energy and just how I'm interpreting this My question is why was Monica left in the dark her wording just comes off Like Disney stopped communicating with her at some point after Disney acquired 20th century Fox It doesn't sound like a script was ever fully written either But most importantly she doubled down on the fact that it was a new girl and not Buffy being race swapped This is something I thought was very important for her to say because I know at one point she did make a statement after the initial Response that came out after the show was originally announced in 2018 But she never flat out said hey, it's not going to be a black Buffy She more or less said we're just gonna meet a new slayer, but that still didn't flat out say it's not gonna be Buffy being race swapped Here she flat out made it clear that you were not going to have a black Buffy Which makes me more than relieved because black people within this universe are already the reason for the slayer existing We already have a very rich deep history and connection to Why the slayer even exists your first slayer was black We met Kendra who was black got to know Robin's mother Nicky wood who was another black slayer and had a black potential slayer Present for season seven. I believe her name was Rona So there'd be no reason to race swap in this context with a history that began with black women So I'm glad Monica was going to just give us a unique black woman doing her own thing now I think The reason that is on pause is because I truly think that what Disney wants to do now With everything that happened regarding Joss Whedon stuff like that. That's all gonna just be cut out They're gonna shift focus away from him I think what they ultimately want to have done is adapting Kendara Blake's novels that she's working on because I Again want to point out they never said it was canceled. They just said it was paused I think the pause is related to Kendara Blake and her finishing these novels because she Says she has a trilogy plan So Kendara Blake who again has a couple of books released so far that I'd love to see adapted into a show from Disney Recently was asked if she liked to see this and her response was it's not up to me Disney owns the rights to it already meaning her books and the IP itself, of course Since we know Disney acquired 20th century Fox and they own all the rights to the books So if you find me advocating for an adaptation of this, this is her again It's not because it's not because it's coming from a selfish place It's mostly because just like other Buffy fans. I want more Buffy content. That's more or less what she was getting at Kendara's recent book is Doing something important which makes me want to see this adapted as our Buffy sequel even more Kendara's books just as a refresher are titled in every generation and one girl in all the world Which was just released earlier this year both censor on Frankie Rosenberg daughter of Willow Who is a student that they're now rebuilt Sunnydale High and of course Sunnydale itself has been rebuilt The books are set 15 years after the Hellmouth battle of 2003 Frankie is a witch and a slayer combo, of course She has her own Scooby gang and Giles Oz and Spike all make appearances to rep the legacy cast Buffy and Faith and the other slayers have gone missing at an annual slayer convention But the world still needs a slayer of course and Frankie's recent activation makes her the perfect candidate while we try to figure out what happened to the others and Navigate our youth similar to the original show Kendara has taken it upon herself in this recent new book one girl in all the world To address the consent and non-consent aspects of slayer activation And this feels like the most appropriate approach to take with a sequel set after the Sears finale Slayers never gave consent from the time they were created men Stopped it or strapped to grow down and put the essence of a demon inside of her So that way they could have somebody to protect them from the evil that existed in the world That of course being demons and vampires and all the other old ones that roam the earth when humans were still first coming on to the surface now This magic they perform Was passed down To a new girl every time the last one was killed until Willow decided to break that chain that those men started By activating every girl who could have the potential by channeling the essence of the scythe We know to battle the first evil now while she was acting in good faith Willow herself also did this without getting consent from those women So can Dara's books Examine this head on and it seems that our big bad of the series going forward Are a group of rogue slayers that did not want to be a part of Buffy's army and they despise willow So they are looking for a way out of this the reason I think that this is brilliant is because it is again Having that connective tissue to stuff that was already kind of left on the table for season seven I don't think it was ever really addressed in the comics In this capacity I could be wrong because it's been a while since I've kind of looked at any cliff notes or dived that deep into the comics But the fact that it was left there on the table for you to address that consent and non-consent aspect Of the slayer lineage and now it's being tackled in this capacity I think that this is the most appropriate step to take when addressing a Buffy sequel If you were to revive the Buffy verse and then of course you can move on and do your own thing afterward But addressing that non-consent and the fact that there Definitely are women out there who could feel some type of way about being infused with powers They didn't want and how that could probably just make their life that much more challenging the fact that They probably aren't getting to do certain things in life because of these powers They have and of course they're now going to take their aggression out on the people responsible Which in this case would be willow. They could also be the ones responsible for the disappearances of faith and Buffy I don't know that because we're going to find out out when the trilogy from kandarably concludes But you guys let me know should think about this down in the comment section below If you haven't already, of course, make sure you subscribe turn on post notification And there is a video in the description. I'll have links on my social media accounts I'm on facebook, twitter and instagram You can message me there of course and let me know if there's any movies news or reviews You let me to cover in the future and with all that in mind guys. I will see you in the next video


A bolted-down wardrobe stood in my basement for years, locked securely. But one particular evening, that changed. My friend Ryan and I were packing boxes, when the wardrobe slowly opened of its own accord. Now, I have kept that thing locked since the day I moved in, mainly because I didn't even know where the key was. Lost, somewhere in this house, undoubtedly. After this small surprise, we drew nearer and peered inside the wardrobe. I expected to see nothing but junk, at most, but a small, square room with a single wooden chair made our rise. Ryan stepped in first, and I, again reluctantly, followed suit. The only thing in here besides the chair was a rusty, olive green foot locker, which was closed at the other end of the room. A sudden coldness emitted from nowhere in this room, and I was ready to walk out, and Ryan called my name, forcing me to revolve on the spot. He had opened the foot locker, and was pulling an object out of it. The item in his hand appeared to be a videotape. Ryan informed me that the tape had no label at all, but once I got a good look at it, I saw a drawing of a frowny face in green marker. I pointed this out to him and he laughed it off, saying that, It's probably one of those cheesy horror movies. As we stepped out and did the remainder of our job in ten minutes, we headed upstairs. Ryan looked me in the eyes, brandishing the tape in front of me. He explained that he was going to take it home, and I accepted his offer immediately, since I had no working VCR player in the house. I kindly asked him to call me tomorrow morning, to tell me what was on it. However, I still felt uneasy about the frowning face as I watched him leave, wondering what it could be about. The following day, at approximately 12pm, I checked my messages once again for Ryan's call, in case I had missed it. Still nothing. He has been known to sleep past this time, so I thought he'd call eventually. I tried calling his phone, only to get a busy signal. Who could he possibly be talking to? At 3pm, I still hadn't received any calls, or messages. I tried Ryan's number again. I had received another busy tone. How long has he been on the phone? Approximately every hour after that, until 7pm, I had called. And called. Only to have my auditory senses wracked by the busy tone again, and again. I had become very impatient, so I decided to go to Ryan's house to ask him personally. As I stepped out onto my porch, I looked down and saw, to my astonishment, a videotape, flipped over on its face. Confused, I picked it up and spun it over. It was in fact the same tape Ryan had pulled out of my basement, yielding that same weird frowning face on it. I lowered it from my face, and wondered why Ryan would leave it on my porch, and not hand it to me in person, especially since I had asked him to at least call me about it. I walked to Ryan's apartment in about 6 minutes flat. As I stepped onto the threshold of the door, I started to knock loud, and accidentally pushed the door open with my fist. Coming out my apologies, I began to look for my friend. I heard a strange noise, exactly like muffled speech, coming from his room. I called out his name as I approached his bedroom door, throwing it open. He wasn't in there. Ryan's TV was on however, showing the generic blue screen from a VCR player, once a tape was removed. I looked at my hands and found that I wasn't carrying the tape anymore. I was again angry that I had dropped it in my rush to get here, but as I stared at his entertainment system, one single tape lay on top of the TV. Sure enough, as I swept it off, my eyes caught the green frowning face, ridden on the tape. It was time to get to the bottom of this creepy thing. I popped the tape in, and sat on Ryan's bed, staring at the screen.  He was looking directly at me. I moved a couple of inches to get a good look at the TV, and his eyes followed my movement. I stood up and strafed left. His eyes followed me perfectly. I strafed right. He still sat there, expressionless, his eyes glued to me as if I were the one on TV, and he was watching me. I sat back down on the bed, and I saw him take out a remote control, still staring at me. He appeared to press a button, and I felt as if my body had gone rigid. I couldn't move my arms, legs, or any part of my body whatsoever. He stood up and walked out of the camera's view. I couldn't even move my eyes, I was staring at the screen, at that strange background that held freaky paintings. Thankfully, I could still breathe. The screen began to turn into static, and a few seconds later I was watching myself on TV. It appeared as if an invisible camera were coming out of the television, and pointed directly at me, sitting on the bed, looking at myself as if I were in a mirror. I was terrified at this point.  As I still stared at myself on TV, I noticed the old man appear from thin air behind me. Right around the bed. I would have screamed my lungs out, but I couldn't do anything besides breathe and stare at that horrifying image. The old man still had his drooping face, and began to handle slowly around the bed. I didn't have any way out of this. I stared at him on the TV silently in terror, and as the man on TV grabbed my shoulder on there, I felt a real hand grab me. At that moment the hand touched me, I began to writhe and scream, throwing myself off the bed and onto the carpet. I was in tears. I suddenly looked behind me, and saw that nobody was present in the room with me. I sniffled, pushing myself back up to my feet to look at the screen. There was Ryan, standing in a black background with a horribly morbid expression, clawing at the screen, apparently trying to get out of the TV. I then saw a hand grab him by the hair, and twist his neck 180 degrees, so that the back of his neck was facing me. He slid down and disappeared from view. First static began filling the screen. The old man's face suddenly flashed on again, holding up his remote and pressing another button. I felt extremely odd, like I had forgotten everything. A ringing silence filled my ears, but I could feel myself moving. Suddenly, I found myself back at home, in my kitchen. I was facing Ryan, and he was holding up the creepy tape. He was asking me if he could take it home to watch it. I promptly replied no, and he left without argument. I threw the tape in the garbage. When I arrived to the second floor of my house, I found it waiting on the bed for me. This happened several times as I tried to destroy it. Over the years, I lost the tape as I moved again, to a new home. As I explored the house, my landlady, Louise, had called me to tell me she'd be arriving in five minutes to have a face-to-face meeting. A few minutes after the call, I heard a knock at my door. Combing my hair to make a good impression, I opened the door, and was about to greet her, when I noticed that there was an oddly familiar old man on my porch. He leered at me with bloodshot eyes, and a droopy face. He held out a tape. I grabbed it and looked at it, trembling with fear at the sight of the green frowning face that had scared me to death years ago. I raised my head up, and saw that he was looking at me with the same slightly scary drooping unsmiling face, dressed in the same outfit as he had in the tape, that he had stared me down in. I blinked and he vanished instantly. Today, this tape is still stuck with me, and I haven't seen Ryan in years. I still haven't watched it again.  I don't think I'll ever be able to get rid of this thing.


In this video we are going to look at a few coordination compounds and try naming them based on the rules that we have just studied. So here is the first coordination compound and the first rule is that we name the cation before the anion. Now in this particular compound which is the cationic and anionic part, the coordination entity is the cationic part and the counter n is the anionic part. So obviously chloride part would be at the end of the name. Okay so let's see how to name this particular coordination entity. Now within a coordination entity we will first have to name the ligands and how many ligands do we have? We have two ligands ammonia and carbonate. So when it comes to ligands we know that we have to name them alphabetically right? Now ammonia is a neutral ligand and its name is amine and CO3 is an anionic ligand and its name is carbonate. But since it's an anionic ligand we know that the name should end with an O right? So it will be carbonate DO. Now which among them will take precedence? Well alphabetically ammonia will come before carbonate and how many ammonia ligands are there? Five. So the prefix for that would be penta and how many carbonate ligands are there? Just one. So that would be carbonate DO. So within the coordination entity if I simply look at the naming of the ligands it would be penta amine carbonate DO. So this is the ligand part, perfect. Now after the ligand we need to go to the metal right? Now since it is a cationic complex the name of the metal would be same as the metal itself that is unlike the anionic complex where the name of the metal ends with an 8 we have no complications in the cationic complex. So the name of the metal would be cobalt, correct? If it was an anionic complex it would be cobaltate and what's the next thing that we need to do? We need to write the oxidation state of the metal right? So for that if you assume that X is the oxidation state of cobalt and you have neutral ammonia so it would be 0, carbonate is minus 2 and chlorine is minus 1 overall charge is 0. So that would give X is equal to plus 3. So therefore the oxidation state of cobalt is 3. Now remember there is absolutely no spacing to be given between the ligand and the metal ion right? So here is the metal, so no space to be given between these two. So this is the entire cationic part and now we need to come to the anion which is the chloride ion so that is pretty straight forward it would be chloride. So the final name of this particular compound is pentamine, carbonate, cobalt chloride. So let's look at one more compound. So here is the second compound. So let's try naming this okay? Here again the first rule is to name the cation and then the anionic part. Here again the coordination entity is the positively charged part and the counter ion chloride ion is a negatively charged part. So what do we need to do? We have to name the cation first which means we have to look at the coordination entity first. And within the coordination entity we know that we need to look at the ligands first and then the central metal atom okay? So let's look at the ligands. How many ligands do we have here? We have three ligands, two ammonia which means we have a main, one Cl which is an anionic ligand that means the name would end with chlorido correct? Because we will add an O at the end of anionic ligands. And now again we have NH2CH3 which is neutral again. So this is nothing but methanamine. So these are the three ligands that we have and which one would take precedence? Well that would depend on the adorbital order right? So first comes ammonia, then comes chlorine and then comes methane amine. So this is the order in which we will have to name the ligands. Now how many ammonia ligands are there? Two. So the prefix we add would be diamine. How many chloradions? Just one and again only one methane amine group. So let me write the name here that would be diamine. Next one is chlorido. So chlorido and methane amine would be written in brackets. Methane amine. Okay so we are done with the ligands part and now we need to look at the central metal right? Since it is a positively charged complex the name of the metal would be exactly same as the metal itself. So that is platinum. If it is an anionic complex the name would be platinate correct? Now what is the oxidation state here? Let's quickly look at the oxidation state x plus 2 into 0 plus minus 1 of chlorine again plus 0 plus minus 1 of chlorine and total charge is 0. So x would be plus 2. That means the oxidation state of platinum is 2. So this is the cationic part. So we are done with the cationic part and finally we need to name the anionic part which is very simple which is chlorine and we need to leave a space between the anionic and the cationic part so let me move it here chloride. So the name of this particular compound is diamine, chlorido, methane amine, platinum to chloride. Alright, let's look at another simple coordination compound. So let's first try to identify the cation and then the anionic part. Now interestingly in this case our counter ion is positively charged and our coordination entity is negatively charged okay? Since we named the cation first we can write the name of the cation directly here which is now we need to look at the anion and anion is a coordination entity. So first we will begin by naming the ligand and then the central metal atom. So the ligand is chlorine and we know that since chlorine is a negatively charged ligand or an anionic ligand the name has to end with O so that would be chlorido. We have only one type of ligand here which is chloride group and how many are there? We have four. This would be tetra, chlorido, simple right? Now let's look at the central metal. When it comes to the central metal we know that the central metal atom here is in an anionic complex. In such cases the name of the metal atom would end with an 8. If it was in a positively charged complex then the name would be copper but since it is in a negatively charged coordination entity the name would be cubrade. In the next step we need to write the oxidation state of copper. So what is the oxidation state here? Okay let's quickly calculate that that is 2 into plus 1 of potassium and let X be the oxidation state of copper and you have 4 into minus 1 of chlorine group and overall charge is 0. So that means X is nothing but plus 2. So copper here has plus 2 oxidation state. A very simple example right? So the final name would be potassium tetra, chlorido, cubrade 2.