Advent of Code 2021 🎄 Day 1 🎄 Golang TDD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
happy december 1st now that it's december 1st we get to start advent of code 2021 been released new day let's start question one you're minding your own business get interrupted in a submarine gotta find some stars 50 stars this is the background story um let's get into the puzzle for today uh as the submarine drops below the surface it automatically performs a sonar sweep of the nearby sea floor each line is a measurement of the sea floor at a depth as the sweep looks farther and farther away okay so you get those those are the depths first order of business is to figure out how quickly the depth increases so that number increases um number of times the depth measurement increases from the previous measurement so um we don't look we don't look at this one right um so [Music] let's look at these numbers all right so it increases increases decreases increases all right and we'll note that this is an inc is a decrease from this but it's still an increase so it's only the previous number not like the newest deepest part right um so let's see okay well let's pull it over here let's pull up our vs code i've already made a 2021 um folder in my repo let's make this oops that's not what i want rename this to zero one for day one let's uh get started again for all of you who are new to my stream i'll be doing this uh as test driven development which means i write the tests first that'll become more clear in a second so the first thing i'm going to do is take these test numbers and add them to input tests oops close that out and let me grab my puzzle input it's pretty big copy that and put it in input test two thousand two thousand rows of it all right close that out and let's look at so one thing that i learned from uh doing the you know practicing on the 2015 um event was that there's you know basically two ways to load my input oh what what's going on here um we must not have saved something anyways uh the first is if there's one long string the second is if there are um like this a number of different lines let me actually let me pull that up and see where i went wrong maybe i just changed in the 2019 or 15 template yeah okay so um i'm going to copy all of this right here and i'm going to paste it in both the where did i copy that from i'll just start from the check function i'm going to post it in the 2021 so there's two load inputs uh and then just because what i would have done is that just do this copy anyways i'm gonna post that there too all right so let's save these let's close out of these and get back to our okay so now we're back in day one we've reset those things so this is the one for one line this is the one for multiple lines so i'll just delete the one line and look at okay now i'm going to go looking at my tests so the first thing is that um [Music] i'm going to test my the load that i'm inputting um so the function that i'll be using is the input and i'm passing in the file name right which is a string which is dot t e s t dot text um and what i want to get out uh should be an array containing strings and just to make it easier for myself i'm going to dump these in here now um in reality these are all going to be returning as uh like the way that i have it now actually let me run run this test let it fail first okay um were they mad about oh because i wanted all right so let me run this [Music] all right so the problem here right is that i my current function is set up to return strings right um which which i knew but what we're going to do now because we want them to come back as numbers they're always going to be numbers you know we can just set it as a number the first time and an integer the first time and be done with it so let's go to our function here where's multiple lines and first of all we want to change this to a slice of ins right and then the problem here is that input change that and um let me collapse these so that we don't have to be distracted by them um so block is the scanner and this okay so what we need to do is we need to turn this block which is currently these number in string form into a string so what i'm going to do is um because this is test driven development i'm going to make a test real quick that um called makeint where we're going to pass in a number example one two three here and we're going to return that integer all right so let's test it actually let's um put this above so that we can test them in order all right these errors oh it's it's getting mad at me about this um let's for now just keep these as string we're not quite ready uh for that yeah so i'll fail the test but i won't you know prevent me from compiling okay makent doesn't exist so let's make uh the make end function so what is that uh what we're going to pass in is a string and what we want to return is indent how are we going to do that um return well first we have to do it i know um from the documentation from practicing uh there is a string conversion package here so what i need to do the the problem is it also returns an error right so we'll say it now and then the error equals string conversion a to i and then it receives that string string right um just to see there just import that all right string conversion is important now i got to do stuff with the first thing i'm going to do is i made a function up here to just uh handle errors so what i'm going to do is uh check air if error if there's no air then it doesn't do anything right and then i will return it val right so now that i've made this it should take that string it should return either the integer value or you know in an air which hopefully is uh nil right and then it won't do anything if that error is nil um right so let's test that again uh and when we test it now we're back to failing the test load input which means that we are passing the make tested all right so now what we're going to do is get back into here change this to inch change that's the int and oops and change this block to make it passing in that string all right so now that should turn it that string into an it right so let's go ahead and run our test now let's see 51. okay i'm just going to comment this stuff out down here not ready for it yet um we're going to test those in our test pass right so now we are able to successfully turn strings into integers and then take our input and create a slice of all those numbers that we can now iterate through all right so let's make one more um test real quick this is a pretty simple problem that we have here on this first day so far at least this first part before they throw a wrench into uh what we're doing remember that we have to assess whether a number is greater or less than the previous number so we're going to have to make some sort of comparison function so let me i'm going to make a i mean do i need an is greater than yeah i mean [Music] yeah let's just do it right um this is something that we can just do in line right um inline uh it would probably be easier but this is simple and we want to make some tests uh let's go for it right so my idea of this is greater function is going to be passing in two variables right like very you know number one like so let's look uh at these first two right uh 199 99 and 200 right 200 and we want that to return true right because it is true that it is greater uh and then let's look at our other example uh to 10 and 207 and this should return false all right let's go ahead and test it it fails because we don't have this is greater function so let's make it and it will take we'll call it val 1 which is going to be an int val 2 which is also an int and it's going to return a boolean all right so again this is really simple we're just going to return val one is less than val2 right so it will can i do that oh no i spilled that wrong i don't think i need these commas look at these parentheses all right so it will assess whether val 1 is less than val2 which is you know what we want i mean i guess we could reverse this right put 2 in the front right but essentially it's returning whether this statement is true or false right so let's go ahead and test it and our tests um are all passing all right so now i think we're ready to uh make sort of the the integration test here for part one so let's um make an input value well let's just put this as part one what do we call this load input okay um and then this will be part one and what we want is it to just receive this input right um but this is just like sort of like the data that's coming in um and we want it to return okay let's see what they said so this was our test case right and there are seven measurements that are larger than the previous measurement so we are going to want this to return seven okay um save that and let's actually look at part one we're actually passing in an int right a list of ends and we're going to return it um this is going to fail obviously because i'm returning 99 um all right so what do we need to do right so we're passing in the data and we're going to have to make some sort of a counter right so let's uh and we'll initialize it at zero um and what we're going to be doing is we're going to be so we need to keep track of the indexes right so we'll do it for um all right write the index and well we're gonna we want to skip over the first one right so we're going to do a for i equals one because we're going to start at the first index not index zero so we're starting with the second the second value and we're going to do this while i is less than the length of theta that's what we're passing in as the um like the data that we're coming in right uh and we will it uh increase that by one every time all right so we got our for loop here and now we're gonna have to do where we uh assess that right so um if and then remember we have that is greater and then we need to put on our two values right so um data i minus 1 is going to be the first number right because we're starting at index 1 here right but we want to compare index 1 the little one to the to the the first one to the next one right and uh data i okay so if it's greater than what we're going to be doing is uh counter plus plus or what is it plus equals one i could probably do plus plus two um no let's try that i think i can do that and then what we're going to return at the very end of this um is counter because that will tell us how many all right so let's go ahead and test uh see if we get a seven and we've passed that test so we are able to do the full thing with the input and the output becoming seven all right so now it's time to do the real deal let's uncomment out this part one stuff um doesn't look like there's any problems you know we're doing our data doing our data the same way we're going to put it all right so let's do it go main.go and we return in a quarter of a millisecond uh that there are that many um increases so let's enter it in one two nine two and get ourselves our first gold star all right so now let's look at part two this is where they trick us considering every single measurement isn't as useful as you expected there's just too much noise in the data consider sums of a three measurement sliding window again considering the both examples start by comparing the first and second three measurement windows all right so let's see um what oh i see okay so we're going to like sort of do a sort of a rolling average i think first and second three their measurements the first window marked as a their sum is right the second window it sum is this the sum of these measurements in the second window is larger than the sum of the worst therefore it's increased right okay um so let's think about this so what we're going to have to do is what create these different sets trying to think about how to do this to reuse what i currently have right obviously we can do this you know reuse this is greater one right just by passing in um the sum of the three right versus uh the other values i think that what we can do is you know i don't even want to um create a new function here let's um do part two and our answer should be five now for this [Music] all right so we're going to be doing a lot of this same stuff right but instead of let's here let me comment this out i don't want to do this um what i'm going to do is make like a second array that i pass into it right so what i want to do is i need to make a test for um sort of adding those numbers together so let's copy that it's not a great name but go with it all right what's our input our input is our input's going to be this so let's input equals all right so our input equals this right so that's what we're going to create right and then what we want to do is have an output and it's going to be i wish i could just copy all those what this is going to look like let's take these over first all right so output we want oops go test fail we don't have combined three let's make combined three um and what we're going to have is our input right is we'll still call it data because that's really what it is um that's the list event i mean that's how we referenced it in the previous problem and we want our output also to be what should we call this three measurement window equals empty set there and then what we're going to do is um a loop here on data uh for i equals zero so we're going to just start with zero this time um while i is less than length of data so that would bring us to uh right at the end but we want to stop at like you know two before the end so we're going to do this um minus two more right because we don't want to sort of overflow our list here is gonna be shorter uh than the original one by two um and again it will be i plus plus equivalent all right so now what we need to do is some addition right um so it's going to be 3 mw append 3 mw which starts at 0 and we're going to append on to this [Music] data you know let's let's just make this first data i plus huh okay and we're going to be appending that three onto it right so this should be taking um the this data set right and it should be iterating through it and it should be adding the first three and then these three and then these three and so and so until it gets to um this one right here right uh two before the end where it's going to stop right and we're going to okay so let's see if that combined three passes it does pass right because this uh this test that we're failing is actually this part two one let me comment it out so we should have a nice clean pass all right so that's passing all right so now what we need to do is uh i mean it's essentially gonna be the same as part one and you know we could even we could even still do that and say what we're gonna do is uh we're going to not have a counter we're going to say new data equals combine three data right so that's taking the data um that's input right this set for example and turning it into this new set right and then what we'll do is return part one with the new data right we'll see if that works this will uncut we did that already all right so let's test it passes excellent now let's run it for real it's almost the same uh well we'll see if this is right should be um one two six two submit and we got it we got our gold star all right completed day one good for us um well i'll do this later um i'll be pushing that out to github um [Music] for other people to mock it they sophie the need um so yeah there it is there's day one um got ourselves our two stars uh in at midnight we'll see uh what the new thing is and i'll run it tomorrow during lunch um in the meantime see you tomorrow take care
Info
Channel: misterjacko
Views: 165
Rating: undefined out of 5
Keywords: AdventOfCode, Golang, LearningInPublic, Programming, TDD, twitch
Id: pfuZcsBbvEI
Channel Id: undefined
Length: 36min 9sec (2169 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.