Dive! [Day 2 - Advent of Code 2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on this is oxdf and i'm looking at advent of code 2021 day two diving in for the first time right now let's see um so you've got to figure out this pilot thing um okay so it looks like we're getting instructions our input is going to look like this we're going to get instructions that say forward down forward looks like basically the three instructions are forward down and up it seems pretty straightforward uh you get a final position of 15 and a depth of 10 multiply them together to get 150 and we get results um okay so looks like i'm gonna have to process those three commands um find a position and a depth and multiply them together let's see what uh go ahead and see so first we will save this uh test input um [Music] over to our terminal uh example there you go looks good and we'll go ahead and save the puzzle and input as well all right it seems straightforward enough um let's go ahead back to the terminal let's give it a part one here m o two example have that up so we can see it okay so let me start making some uh start making our script so user bin and v python 3 import sys and so now what we're going to do here we're going to very similar to previous ones we're going to open up we have to read in our input so it's going to be access.rtv ooh typing is hard as a read as f we're going to do uh c commands equals f dot read lines and let's see do we know what kind of processing do we want to do on each line um i guess we'll do it in the loop um so we'll do this in a for loop i bet um we could probably do this in a one-liner and maybe i'll come back to the end look at that but for now let's just do um for cmd in cmds um and we're going to see cmd.let's strip see i want to remove that end line from the move any white space from the end so i just have the two things and then i'll split it on space and so then we will have d and uh let's see so um direction and uh quantity equals that um so then we probably need to come up here and start with a depth position equals zero let's see let's do it this way position comma depth equals zero and so now all we need to do is um what's the easiest way to do this i'm trying to think um you could do a bunch of ifs but um we could also just define a so if we define an array up here that sort of says um uh compass i don't know equals let me do this and we say what are our three things forward is going to be equal to um zero ah this this is messy let's just let's let's do this let's go back here and we'll just do some if direction equals forward position plus equals quantity alif direction equals down depth plus equals quantity lf direction equals up depth minus equals quantity uh else import pdb pdb dot set trace and all i'm doing here basically is saying i could probably you know if i'm confident my input i could have just put an else here and got and subtracted quantity but just in case something goes wrong and i get a direction that doesn't match any of those three i'm going to just import the debugger and stop right there and so i can check out what's going on so once that's done i just need to print part one and that will just be depth times what was that position like that save it and let's go down here and give it a try python three day02 and then o2 example i think we're looking for 150 oh we don't like oh of course um qty equals int qty 150. um let's try it with our puzzle input much bigger let's go back here and see if that worked beautiful continue to part two based on your calculations the planned course does not make any sense you should have marine [Music] you also need to track third value which is your aim okay um so we'll start we'll have a third value that is aim it starts at zero um hmm okay so this is we basically have to do the same thing again but this time we're gonna the rules are just different um so let's go back here um do we yank this whole step right here and drop it in so now we need we really need three things position depth aim equals zero zero zero um pull up here so down increases your aim by x units so we're gonna do is down aim plus quantity okay uh up decreases your aim by x units so just like before and now i just need to you know let's see forward does two things it increases your horizontal position by x units got that and it increases your depth by your aim multiplied by x so we'll say um that equals plus equals aim times qty like that seems good read that i read those instructions very quickly so let's see if we uh let's see if this is actually right um now we got 900 what are we expecting for here 900 let's give this a try and see if we solved it that quickly sweet that was it um so we got both we got both there um let's see i'm trying to think if there's anything i can do to make this look more elegant um and the answer is probably uh come down here and what if i define a function that handles the input so on def parse and parse line line and in this function we're going to probably do basically the same stuff we've already seen right so we're going to do that we're going to get a direction and quantity we're going to set that quantity there if direction [Music] equal forward i'm going to do it a little bit different here we're going to rather than try to make the decisions about what to do we're just going to um give it i mean at least starting with the first part we're going to just try to figure out what we're doing here so we're going to do direction equals 4 return q t y comma zero if direction equals down yep return zero comma qty if direction equals up turn to 0 comma minus q2y i'm not convinced this is going to end up being any nicer but let's let's keep playing with it for a minute just for the sake of playing with it um so now and then i can do my same import pvb pdb dot set trace i don't expect to ever hit here but if i do i'll i will now know it um so i should have returned one of these three by now um so now for part one i can start to think okay what is um you know so what does it mean now what do i need to do to if i've got this where i'm going to take a line and it's going to return two values um so now i can do let's see so i can start to think about this as a list comprehension um line or line in lines i read that in earlier and now for each line i'm going to parse the line so now i'm going to have a list of tuples and so for the first challenge i just need to basically add them together is that right um see this is probably there's probably a good point where i would come and play with it anyway um do that because i didn't need the pdb i could have just put a i so let's see um let's see what this starts to look like hopefully i'm using example input oh no let's let's that's gonna be way too much output there we go um let's switch back here to a dash i just for the sake of not needing pdb here and okay so now let's see what does this give us uh lines is not defined did i not define oh commands okay so now i have that does not look right uh because line doesn't exist uh wait no yes it does for command that means this doesn't matter but let's fix it um that looks fine um so if i do parse line dmd sub-zero i expect to get back two that is not what i expected to get back okay it'll help if i name my variables correctly so i'm doing by that there we go that looks better that looks better um so now what i really want to do is just once i want to sum all these together let's see can i um no that's not what i needed um so what do i want to do here i've got my list of things i need to um map i guess what if this is where i can use a map um thumb onto that i don't want to there like that see what that looks like um so that okay so that went through and summed these together they're summed five and zero it sums it summed each element and that's not exactly what i want um i wonder if i can pass let's try what happens if i do that i don't know if i can zip that i'll explain exactly what i'm doing here if this works 15 10 i think that was right okay let's talk about that that's cool that'll work so let's see what that let's see what i did here um when i have the make this simpler so i can show what i'm doing i think i need that okay um with no with no thing there what might when i pass it to zip um let's get back to our clean date so i've got these i've got this list of tuples and if i pass that to zip um just as it is then it's just gonna take each it's taking uh it's got one list basically it's just saying oh you gave me one list of things i don't know how to zip one that's a thing so i just you know zip expects really expects more than one so you give it two lists and it takes element one of each list and puts it together element two of each listen puts together well you just give it one list it's just returning element one as a tuple with no second no the second next object element two is a tuple with no next object and that's not super useful um when you pass into a function supposedly i want to take zip um when you put star in front of it like that what the star does um i think the easiest way to explain this uh so ah here okay this will work um if i do a print i have no idea let's let's let's capture this for a second so uh we're just going to call this x for now so i can stop typing it if i do print x like that it prints one object that is this array uh this list if i do a print of star x it's going to print each it it's as if you pat it's the same as passing um each it takes it removes the list basically and it passes into the function each item in the list as if it was one thing as if it was an art uh an argument um so if i have let's just show another example um if i do make a function the theft test and it takes a and b by axis called def add and we return a plus b really silly function if i try to pass it if i try to add of 1 comma 2 like this let's make it y equals 1 comma 2. if i do add y it doesn't like this it's saying oh you passed me one i needed two arguments a and b and you passed me one this array it now if i pass if i try to do this it's going to now complain that like oh i can't do a list plus an in that doesn't make sense it's confused but if i come here and i put a star here it returns three just fine because what the star does is it takes the list and it just basically blows it up into individual items and so now instead of passing in one object i pass in two one and two and they got added together so how does that apply to what i was doing before let's go back up here basically i'm doing here is i'm taking all of these so instead of passing in this one list i'm passing in each item in x individually so basically pretend i'm passing in one two three four five six six tuples and now when i when i zip six tuples it's just gonna take the first object from each of them together put it in one group the second put it one group and that's it they all have two so that's why when i do this like this and i need to list it i get two objects back and it's it's like this and so what i can actually do now is i can map and what the map function does is it takes a function so this case sum and it takes a list and in this case my list there and it's going to apply it so now it's going to apply some to this object it's going to apply some to each object in the list let's apply sum to this object and apply some to this object and it's going to return it's going to run a map object i can easily turn that into a list here and see that it's going to return the sum of all these and the sum of all these which is what i'm looking for so now down here what i can do is part one equals this way do this like this i guess in fact i don't even want i want i want to product these together right so um i can't remember what the what's the um i have to go back here to our um all right let me pull up my chrome window with that metal code uh python multiply items in list four different ways all right geeks for geeks what you got um where's traversal i don't want to do a loop i find there's got to be a function for this i don't want num pi i don't want num pi is there really not a way do i really like to fill in math um and they're like a isn't it there's gotta be a better one in 2012 it's probably old let's find a let's see i thought this was gonna be a much easier question to answer than it is turning out to be if i find more reputable reduce function that might work i'm still funk tools um huh all right i guess we're gonna do it with math um was it math.prod go back and try all right okay i guess i guess we're gonna import math that feels unnecessary to me but fine so i don't even think we need the list there do math dot prod looks like we're lined up right let's make sure we import math uh and then do part one looks good all right that looks good um we could take on part two and i bet there's a way to do it um but i'm gonna go ahead and call it today that was that's hopefully enough uh beyond root section there for you um if if you're not familiar with my blog posts often i go have a beyond you know beyond roots the section where i've already solved the problem but i'm going through trying to uh look for extra stuff i can find um so anyway um this is prettier and i bet there is a way to do um part two it's a little bit trickier because it's not gonna be um it's more state dependent in part one i basically you know i i can just take all of the depth changes and sum them together in part two i gotta keep track of what the depth is when i apply a forward move and so that's gonna be harder for sure um but it's probably doable so anyway um i'm gonna go ahead and wrap up the video hope you enjoyed thank you so much for sticking around till the end and i'll be back with another one of these tomorrow [Music] you
Info
Channel: 0xdf
Views: 398
Rating: undefined out of 5
Keywords:
Id: MTGqkxXhAzg
Channel Id: undefined
Length: 21min 26sec (1286 seconds)
Published: Thu Dec 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.