Sonar Sweep [Day 1 - Advent of Code 2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there this is zoe xdf and today starts advent of code 2021 uh this is a bit not a hacking ctf but uh just a pure programming one um but it's a lot of fun um and i'm gonna try to do it on youtube this year so i haven't even looked at the challenge yet i'm gonna do them live at least at the beginning um so let's go ahead and dive in um this is i'm logged in here and go to challenge one um so then we start with some kind of story and the idea is um so let's see we're reminding a business on a ship at sea when the alarm goes off one of the elves tripped them through the slay keys into the ocean uh we're in a submarine covering christmas lights uh um so the antenna signal strength is displaying zero to 50 stars we need to collect all 50 of them by the 25th um we solve we collect stars by solving puzzles each puzzle grants a star the second puzzle unlocks when you complete the first so we two a day um this is how it is every year so the suddenly submarine drops below the surface of the ocean automatically performs a sonar sweep of the nearby floor on a small screen the sun are so sonar sweep report your puzzle input appears each line is a measurement of the floor it moves away so it might look like this so let's grab that and jump over to our terminal window and call this uh 01 puzzle oops actually let's see it them 01 uh example okay let's see make sure we got that right starts with 199. back to here 1.99 okay this report scanning outbound debs the first order business to figure out how quickly the depth increases so you know you're dealing with you never know to do this determine the number of times the depth measurement increases from the previous measurement okay that seems easy enough let's give it a shot um so we'll do bim j01. obviously start with our shebang user bin e and b python three we're gonna import this actually we don't need that uh yeah one port says cause we wanna go give it a file name that we wanted to work with and now we can do with open this.rtv1 to get the file name we're gonna be that'll pick the file name and we're going to read as f and now we can do lines equals f dot read lines um and this is a place where it might be easier we know we're going to want numbers so um you can map i think this will work int to that red lens see if that will work python three one up i and our example is one example oops and that didn't didn't error out that's good let's see so what do our lines look like now map object list that's what i want sweet do that um although i wonder if i can i think i can just uh right over that actually let's leave it like that for a second um so what i want to be able to do here is loop over this and compare um compare one to the previous one um so i've talked about this in a previous video but i'm going to use zip um so let's see let's i wonder if i can keep using the lines if i need to save it as a list let's try it for a second um so if i do lines i do uh zip lines lines let's make that list so you can see it nothing okay i think the map object only will only iterate once and then it's gone so we're just going to save it as a list here right away so that now we have an object we can work with multiple times lines so if i do zip lines lines what the zip object does is it will pair objects together so in this case cause i'm doing the same thing you just get each object twice so what i really want to do is get the next get just starting from one to the end there and now i get pairs of the first you know so i get the first one the second the second and the third etcetera so now i can easily just do a loop over this so i can do i don't the list was just a display but now i can come in here and say um x comma y uh x1 x0 comma x1 for x0 comma x1 in and now i can just do a test you know what i'm probably find the number of times it's a number of times it increased so the number of times it increased would be when x 1 is greater than x 0 like that uh and now i think i can just do that and that's my answer so print part one i'll make it a little bit nicer here grab this part one equals one don't need the minus i anymore part one equals seven um that looks good let's go back here and now so i need to grab my actual puzzle input uh you know one of the things they always do is they give you kind of a toy example with you know here's here's 10 on 10 readings and then the real input i have to solve for is you know many so we will say guys there we go um and this will be one input stuff there we go and so we go back to our terminal we get 1342 i'm here and we got our gold star we're on to part two part two considering every single measurement isn't as useful as you expected there's too much noise in the data consider the sums of a three measurement sliding window again considering the example above okay start by comparing the first and second start by comparing the first and second three measurement windows their measurements are the sum let's see the measurements in the first window marked a their sum is 607. the measurement in the second window mark b is 618. basically we were doing kind of an average over um three pieces here so the first increase because because 618 okay so um i think we need to now look at this as sum so let's how are we going to think about this um not quite as simple as our zip from before um but we need to be able to get we need we're going to need four items at any given time right um let's see go back here and run this again so we have lines um let's see if we do a zip i don't know if this is going to be great but we can do a zip with lines like that lines two and lines three let's see what that looks like well that's not what i was expecting i expected to get more than that um i mean the quick there's i'm trying to think of an elegant way to do this but the easy the quick and dirty way to do this is going to be to just loop over um so i can do this this is like bad python practice you're not supposed to do it this way but for the sake of trying to solve it quickly here um because at the end of the day this is a coding competition we're supposed to be trying to rush um comes out at midnight every day so i'm always asleep so i don't ever try to compete in that but um i can do eye for eye in in range uh len lines and then i can say what i don't want i need i need up to three i need three sliding windows so i actually don't need the last two i can do minus two and then here i can do some lines i comma i plus i plus 3 is less is less than some lines i plus 1 to i plus 4. see what does that give me it does give me five um so what i've done here is i've basically said i'm gonna loop over the range from the length of so from zero to the length of i but i don't need the last two because i need a sliding window each time um so what i can do here is you know let's come back here for a second if i do just for just to show you how this what it's doing um comma and there i think that'll work i don't need the sum so i do this you can see what it's doing is so it's creating these tuples like this one and then this one and then this one uh et cetera and in each tuple it's getting three so 198 lines up just oh we have it right here so 199 200 208 is here and then it's paired with 200 208 210 this one the next one is you'll see is the same as this and it's paired with the next three and so if i then come in here and add a sum or any of those lists now i'm getting the two numbers i want to compare and then if i want then instead of printing that as a tuple i'm just going to actually convert that to a actual comparison so now now it's going to return true or false that's why i get the twos and falses and python is just nice enough that if i try to add together if i try to treat trues and falses as numbers they come out as 1 and zero so i can just do that and get my result so i can come here and do part two equals um let's give this a try and this is again this is ugly you're almost never in python supposed to be doing four i in range blah blah blah i might come back at the end here and try to see if i can figure out a more elegant way to show so let's see part two part two so if we run [Music] this now oh let's do back with the actual input uh 1378 let's see if that worked and it's it awesome um let's see if i can clean this up a little bit and um play again play a little bit more with um making this look nice and not having to do a not having to do a link arrange land lines um so let's see i can do a list of zip of lines lines one to the end and lines two to the end what does that look like that looks like what i was looking for um that i'm not quite sure what i did earlier that that didn't work but i'm gonna i'm gonna try to work with this here um so what i can do here is i can actually save this as like zipped equals that and then i can do another zip pull this together because now what i got here is i want this i want to pair objects just like i did before so this time i want to do um list zip zipped and zipped one to the end um let's see and so this is tuples of tuples but you can see this right here is two pairs and this right here is two sets of three and this right here is two sets of three and i'm just i'm starting to get what i want here so now i can make this into a list comprehension so if i do x 0 x 1 or x 0 x 1 in oops yeah and i don't want to do this but i want to do a um let's do sum of x0 sum of x1 and i just put that in a tuple so now i should have my two sums yes so here so 607 618 618 618 618 617 etc so now i can instead of making this a tuple do my comparison i got trues and falses and i can do sum okay so let's come up here and talk let's look at how we make this better um get rid of that and what we really want is part two equals so we need to do two parts these first need to do zipped do our list here like that and now we can do um our sum here like this and that should be it let's see if that works let's get let's get out of here where's my input 3278 i believe that was the right answer [Music] there's got to be some way to see the right answer [Music] um return to your advent calendar down here at the bottom 3278 so that's right um the other thing that's kind of neat here is what this really means is i can make this a single generic solution that solves both of these so let's see death solve and the secret here is that i want to take in kind of an unknown size of array arrays so i'm just going to take in lists like this and then i can make my lift equals list zip um lists like that and so when i pass the first one in this isn't going to do nothing because it's passing in individual items so it's just going to zip individual items together into see i can probably show you it's a lot easier to show than to tell so if i do was my see so if i do um the first one i'm just going to do um what is my lines so if i do a list zip and in the first one i'm going to pass in just lines like this it's going to return just the items so it doesn't it's fine that that'll be handled fine but in the second one i'm going to pass in on three sets of items i'm going to pass in this lines one on and lines two on and here it's going to turn the triplets but what's cool is because when i apply the sum it's not going to care because when i sum you know the tuple by oops let's see sum uh 199 comma that the answer is 199. um so i can make this generic function here that that takes any number of lists and zips them together and then i can do my same thing i did before which would be x zero uh thumb of zero less than s thumb [Music] one for x zero that's one in zip zipped zipped one so i'm doing two zips just like i did there um and what i'm gonna do is you know so the in in this first case it's gonna grab one these two right here and then it's gonna grab these two etc and then the next case it's gonna grab these pairs these pairs so i can now get back up here return sum like that so now hopefully my spacing's not off okay um now i can print uh part one and in this case the answer is i'm going to do solve lines like that i can print part 2 and i'll do solve lines comma lines one onward comma lines two onward that closes the solve closes the ring closes that closes that i'm sure i've made a typo in here but let's see if it works see yep synthetics match okay um i need a here see if that works parentheses does not match um i did this too quickly clearly um so my sums are good there where x in my zipped is there so that goes there and that goes there that looks better alright one more time oh didn't like it um so this is where it's good to jump into pdb um let's see what line number am i on 18. minus mpdv my fee break 18 minus cc and we run to that point lists looks like what i want if i do a next the zip looks like what i want okay so let's let's just start to build out our okay so let's see what do i want here let's just do x1 comma like that zipped and zipped too interesting um that's not what i expected to see let's see so we do a list zip of zipped comma zipped one colon oh i see the see the error i don't see the error in that line but i see the error here i am taking the last item of zipped not one onward item zip i wonder if that's what i did earlier um that seems to be it um so that was a silly mistake but let's see um so there you go that that's the much more there's the much more pythonic ways um and this i thought this is particularly neat because you can apply now i have a generic solver that just solves both parts 1 and part 2. so thank you so much for sticking to the end i will come back to you tomorrow with another one [Music] you
Info
Channel: 0xdf
Views: 677
Rating: undefined out of 5
Keywords:
Id: wZp_gEY635g
Channel Id: undefined
Length: 21min 42sec (1302 seconds)
Published: Wed Dec 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.