Hydrothermal Venture [Day 05 - Advent of Code 2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on this is oxtf and i'm looking at advent of code day five um i'm traveling i'm pretty jet lagged right now and i just solved part one uh pretty quickly actually i didn't get stuck at all and then i was pretty stuck in part two when i realized i never hit record um so um i'm gonna try to recreate that a little bit obviously um well i didn't get too stuck so it should probably be just as quick um also i am traveling i don't have my microphone with me right now so i apologize in advance for the poor audio quality um doing the best i can let me know in the comments if it's worth if it's terrible or if i should just you know it's worth it even if it kind of sucks sometimes um so uh let's look at the challenge dive in i'm gonna my my input is going to be a series of um lines each line has two points on it so like zero nine five nine eight zero zero eight um those are going to represent x y coordinates in a graph and so what they show here is for part 1 i'm only considering horizontal and vertical lines so where either x is the same or y is the same so like this top one would count but the second line here would not because x and y will change um and i'm going to draw all those lines on the graph and count the number of lines that go through each space um they show a dot instead of a zero i don't i don't need to make that output so it doesn't matter and then just count the number of spaces that have more than one in this case it's five one two here and three four five right here um so you can see i've already solved this but um this is actually pretty simple i'll start with a normal shell here i'm reading into all the lines i'm reading in the input that's passed in to a series of an array called lines now i'm going to loop over lines so four line in lines uh and i will get point uh let's see i don't remember how i did this this would be good um p1 comma p2 equals line dot split and if we come a down o5 example so i'm going to do a split on this so that will give me this point and this point that would look good then i can say x1 x2 equals you know actually might be easier i'm going to take different i didn't try this but it might help me in part two actually um so what it originally was i would say x1 comma x x2 equals i'll do it this way p1.split y1 p1.split comma and then i want to take each of those and make them an int so i can do map and comma that and i can do x2 y2 equals e2 dot split making a lot of assumptions about my input being good here but you can generally count on that um what i don't know is if i if i need a int if i need a list outside this map or not let's let's run that and see what happens actually python minus i day five five exam what is like two eight next five nine okay um i don't need the map works fine without the list there um i think that's because i'm only using it once i'm just saving off these values and i'm being done with them so okay so i've got max one what i alternatively could do is um [Music] just make this that's how i solved this part originally um but instead i'm going to change it a little bit here i'm just going to call this p1 and i'm going to call this p2 whereas it's reusing those variables from above but i don't need them anymore i'm just going to overwrite them and so now they're going to have it's going to have this tuple boy i believe it's worth checking let's see um p1 map object not what i want um make it a tuple i could do a list as well for this use case it's basically the same i think that works p1 p2 so now i can say for part one all i care about is if it's a horizontal or vertical line so i'll say if p1 sub zero equals p2 sub zero or p1 sub 1 equals p2 sub one that's uh so either x or y staying the same tells me it's one of the ones i care about um now i can just do a loop over let's loop over the x values so we'll loop over uh let's see this is the tricky part you have to think about um and i thought about this last time and um we'll do it again here um i don't know i'd be very happy to just do a range over x from x1 to x2 but i don't know if x1 is always which is bigger um that makes sense but what i can know because the first if i can do a i can sort them um so i can do sorted uh p1 p2 and then i'll that will give me um that'll put the x the low x value for um let's let's just let's test that hypothesis also i don't know that's that that's what happens lord expected one argument got to make that a list so um open up that let's open up a new window here um bim 05 example so going from 09 to 5 9 that's fine let's see where's another one um the next one is three four so i'm going from three four to nine four so that's that's looks good i want so basically i'm trying to put my low x always here um and then my low y will also be here if the x's are the same um so that looks like i'm doing exactly what it's giving me exactly what i want that's good um so what did i do uh well so what i can say here is um p1 p2 equals so i'm just going to rename it so now i know p1 is always the lower x or the lower y if the x's are the same and so now i can loop over this and say four i in range e1 comma zero comma p2 comma zero plus one i'm going to step over my x's and um this is where you know so like i know in this case my y's are also just going to loop over the same but actually what i can do this is where i can use a slope um let's go up here and say slope equals p1 of 1 minus 2 7 divided by 2 p1 1 minus p2 zero this this is a classic mathematical slope right change in x over change in y over change in x um so once i know a slope i can say i'm stepping one at a time in x so now i'm going to also take that same step um so i should make some sort of y uh j equals e one sub one so i'm gonna get the first y now i will do so okay i'm rambling on i got to organize thoughts here so i'm going to do something called i'm going to put a grid here and i'm going to save all my values and i could just use a dick um and the reason i'm going to use it i'm going to actually use a default dict but the only difference here would be if i define this as a dictionary what i'm going to do is come down here i want what i want to be able to say is like grid sub i comma j plus equals one i want to add one so i'm gonna when i get my lines gonna go through this point i wanna find that point in my dictionary add one to it if if this point's not in my dictionary that's gonna throw an error um right now but if i come up here and make this a default dict and by default i'm saying it's going to be integers so now and by default integer is zero so now if it doesn't exist it's just going to assume it's already zero and then that this line will work fine um then i will do j plus equals slope so that should actually let's see so what i'm going to do here oh i'm going to get some bugs in this code when the i'm going to get some bugs in this code when the uh and the line is vertical let me divide by zero um all right i got too cute and tried to do it all at once let's come back here and do this this is gonna be useful for part two but let's go so uh delete this delete that delete that if oops don't believe that if those are the if those are equal this is our horizontal or vertical line um then what i'm just going to do is go at my low x but this time i'm just going to run over this and i'm going to run for i in range of p1 sub 0 um constants of zero sub one i'm just going to run over my x's and over my y's but because i know they're equal i'm basically doing basically doing one or the other um then i can do home i don't need i think that's all i need let's let's say that again i'm getting my two points and then assuming that they are either a horizontal or vertical line i'm going to sort it so that i get my low ones in p1 and i'm going to loop over both x and y although knowing that one of these is going to just have one value in it and so it's basically not a loop and for each time each spot on the grid i'm just going to count how many are there so this minute ago i don't actually remember exactly how to do this though um run that see grid um what i want to do is get count of how many have a value of two or more um so i can do a loop a listed rate of this comprehension here so x for x in grid if i do that x is going to be just the um keys which is this right here um that's that's not what i need i need the values i actually like to do items you can do items here and it will return um so in this in that case it would be um not just keys i can do like uh this would be like chord and num i can do chord there and then i can over here so like if i do that you don't try that yep now i'm just printing the coordinate but i am getting the number there too so i can say um if num is greater than one so there's my five coordinates where the number is greater than one it matches up with what we saw in the input um so i can do something like our print that part one okay so this is going to be when of not actually using chord here because i'm dropping into lens i could instead of doing items do values and then it would just return the numbers um that'd be good enough for the solution but if you ever if you ever need both items is useful so let's see if that worked part 1 5 let's also try the input make sure we're still good 60 63 97 63 97 so we've got it okay we're gonna go to part two and we're gonna say now we are considering diagonal lines and this is where i started to get stuck um and you can see kind of where i was starting to drive on part one um now it is telling me they'll always be 45 degrees so i don't have to have to worry about what happens if um the slope is not it's either going to be negative one or one but um you can still play with that um so what we want to do here up here um let's leave grid intact for tracking part one create a new grid for part two um this is all gonna be the same if this this these case this case is still good so we'll just do the exact same thing here grid two i comma j plus equals one but for the diagonal we have to handle for part two we're gonna handle this as well um let's move this up we want to do this either way now is where we need to deal with the slope so we can do 4i and range i'll even type it out let's go up here and yank it oops ink it paste or i arrange that and but we know we've got the p's how we want them um we do need us this is where we want to slope so we could do p2 of there's the y minus e2 p1 the y value over p 2 sub x that's gonna give me a nice slope so now i can do my steps over y's and i can just um [Music] i can even just keep like so j is going to equal e1 that's my first y now what i'm going to do is i'm going to say um grid2 sub i comma j plus equals one and then i will say j plus equals slope so now when i do the next step when i take one step forward and x i will take that same step forward or backwards in j get my new j um this is well beyond where i got stuck before so i have no idea this is going to work or not but let's give it a try um place for this part 2 is going to look very similar except we're going to use grid believe otherwise it's the same okay well i don't know if that's right or not let's check um so the example that is the example that is what we got in 12. let's see oh that worked um please make sure that was clear um what i'm doing here is in part one i handle the case where i've got a horizontal vertical line that's great so i'm gonna i left grid two here it does the same thing part two i'm handling okay now i've got it says i know i'm gonna have slopes of 1 or negative 1 45 degrees this would actually work it wouldn't work well on like fractional ones but as long as it moved um you know like if the slope was 3 if the slope is a whole number um this would work fine it's not a whole number it's not going to it's going to fall apart because i'm going to start getting fractional stuff and it's not going to fit my grid well but um my idea here is i calculate the slope so i know that what you know for every x i move forward what how does y change and that's the definition of slope and then i start with a i start with my y being p you know the first y and i loop over all the x's and as each time x steps forward by one j changes by the slope and so well i is actually moving over the x's so j you know j changes by the slope and i'm just recording the counts there and uh that gives me what i got so um hopefully sorry this is not my most polished video but uh hopefully it's useful when you learn something and i will talk to you again next time [Music] you
Info
Channel: 0xdf
Views: 228
Rating: undefined out of 5
Keywords:
Id: -GGWLui-Udw
Channel Id: undefined
Length: 17min 54sec (1074 seconds)
Published: Sun Dec 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.