Advent of Code 2021 🎄 Day 2 🎄 Golang TDD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
day two of 2021 advent of code um it's me back um gonna do day two i'm starting to get a little self-conscious that um i'm waiting until lunch to do these all the all the leaderboards that i'm a part of are have me coming in so late so i might start pushing it to be a little bit earlier in the day um we'll see all right let's look at the problem for day two dive we need to figure out how to pilot that submarine can take series of commands forward one down two up three forward up down increases decreases all right so it looks like we have two axises right forward maybe we have backward it doesn't look like it but um and then an up and a down which will be we'll just consider it like a positive in the negative versions of that number um oh i see so um down would be a like a positive number right so because we're calculating it as feet below the surface right so that'll be something that'll make trick us up so down five would actually be plus five and up three would be actually like minus three okay so we'll have to remember that uh horizontal starts zero um four and five word eight thirteen four two to fifteen okay so looks like we're not going backwards um all right i feel like we know enough here let's um let's grab let's see what the puzzle input is first okay so let's actually just copy day one rename it to day two [Music] and get in there okay and um this is going to be an important right as we are going to have to parse these lines at some point um let's grab this test all right our test stuff okay and then let's go to main and get rid of some of these things that we don't need we are going to want to keep make string or make int because when we parse these numbers they're going to initially be strings and we're going to have to turn them into integers um obviously that's not something that we'll do in our input here right so let's make sure that we pull that out of here um [Music] okay is greater we don't care about this anymore we'll still care about function don't care about that um and let's comment all these all right so we got part one part two i'm gonna actually change this all right so we've clipped down our main part we're going to have to get rid of these tests we're going to keep load input we're going to keep make int [Music] let's just get rid of the rest um all right so here for load input we're going to still keep the load input as this and then we're going to want to set these as strings some commas after each of these because it's a list of strings and then we'll tab these over all right so this is what we want hold on i don't need this either all right so um this is the beginning of our testing these should pass um [Music] well we'll see if they pass let's pull a terminal up this make in should definitely pass passes okay so this tells us that we're able to make an in that's good we never changed the original one um uh and that when we load our input it's getting to the file and returning a slice of strings looking like this okay now what we're going to want to do is parse these right first we're going to have to turn them into an array so let's make a we'll use this one um function we'll call it make array oops and you should call it slice just to be proper and go because this is slice not an array and what we'll do is um input forward five for example and what we want to return with is a slice containing strings forward and do we want a number there yet it's kind of a combination there isn't it i'm only going to do this once once we make it a number we make it a number let's just keep it let's just keep it a string of five the test driven development part of this suggests that we should only be doing you know one thing at a time and not sort of doing all these sorts of combinations that allows us to make sure that this make slice function is testable and just makes a slice and then we'll have um we have our other one that says make end we know that when we use that it'll work we don't want to confuse whether make slice works or not with whether it integrates well with make it okay so that's what we'll do actually let me collapse that give us more room um and let's just do a couple of these right so we got a forward one i mean there's no reason why any of these should be different right um down up three three all right so let's go ahead and test it it fails mostly because we don't even have this function yet so let's go ahead and make it funk make slice and what we're going to be passing into it is um just a regular string right so and what we want out of it is a slice of string do i spell that wrong string all right let's make it so we got to separate that so what we'll do is um we it should just be a one-liner right um [Music] return and i don't know if it's a i think it's strings it is the library and yeah split split [Music] and so what's really nice is that it has documentation here and go so s is going to be the string so str is the string that we're looking to split and then the separator what we're going to split it on is going to be a space right okay and that should return that list of strings let's go ahead and test it now and it passes all right so now we are able to successfully take each of these lines that we have pulled out here and turn it into its own separate string there right or slice of strings hold on one second okay um all right let's think now what are we going to have to do so we're going to be um we need to parse this right in some way uh we need to look at these two parts right and we need to do something with the number and do something with the forward up or down now the question is is do we just make this a part of so what we're going to be doing is we're going to be going through the input right we're going to start a function you know part one right that says what do they call it here uh you know h position right equals zero right and depth equals zero right and then we're gonna iterate through this list so we're gonna start doing a range over the list i'm gonna say you know the line we split that line into four and five and then we're going to parse forward right and then we're going to parse five right so what we're going to do is say if you know index 0 equals forward then turn index 2 into a number and add it to h position right um i don't think any of those things really need to be tested so let's because that's all just sort of logic so let's add a part one here part one um we're only going to need one line and the input is going to be load input and part one the input will be input this is failing right now here right because we have um the previous one had it being ins right so we'll we'll get to that um so multiplying those together 150 we want the answer to be 150 right so obviously that's going to fail uh it won't even compile right because we have here part one we'll pass in that to be a strike that should clear up that good all right so now we should be able to test it uh but it should fail right we want 150 got 99 because we're just hard-coding that in all right so now let's get our logic here so um we're going to initialize h position at zero and depth at zero okay so those are our first things and we're going to be returning h position times yeah um times okay right now that'll return zero right actual zero okay so what are we gonna do we're going to do four we don't care about the index [Music] and instruction in range data all right so this right here is going to be iterating through the entire list of strings right now what we're going to do is parse that instruction right return that into the slice right equals make slice with the instruction instruction all right so now this should be in the form of you know forward at position zero and five at position one um and now we have to think about that right so there's two conditions right two two like things we're manipulating the horizontal position that's always forward right and then the depth which is going to be up or down so i'm gonna just deal with the h position first right so we will start with [Music] you know what i could do instead of this i don't know how to test it so i'm going to leave it this way all right so if slice position 0 equals forward forward then we will um h position right um plus equals uh what do we have we have to turn that into an in right um make int with int no ins instruction geez i'm gonna have to make this um i gotta make this more readable um one okay so this should for every instruction allow us to um determine that position right and let's set this depth just to one real quick because we can kind of test this right so our horizontal position should end up being 15 right and if at the end of this um our horizontal position is 15 and our depth you know which we're only setting is one then we should be able to get um 15 here so let's go test that and we get the result of 15. so we're good here we've successfully um determine that first part all right second part um else so else we're gonna dive into yeah pulse would say if instruction slice 0 is down so remember down is actually where we're going to be adding to it right because we're talking about depth right so it's a little bit weird right um equals down then we're going to do sort of the similar thing equals down then we will [Music] depth plus equals make int just copy these okay so we're adding that to the depth else um what's practical all right now let's see uh i think that should be all we need to do see if we get a passing test here and we do a passing test all right i think that's all we got to do let's um uncomment out these obviously we're going to have to [Music] i think we're good here i'm going to save it to import those things and let's go let's see what we got all right i'm going to type that in see if we get it it's a minute we got our first gold star there we go all right let's continue you see all that i'll get rid of my face um [Music] so part two based on your calculations it doesn't seem to make any sense process is more complicated well that's good because that was not very complicated place any calculation okay in addition to the horizontal position in depth you also need to track a third value aim which also starts at zero the commands mean something entirely different than you first thought down increases your aim by x units up decreases your aim by x units four does two things it increases your horizontal position by x that's what we did before it increases your depth by your aim multiplied by x okay so i think here let me pull my um so i think what they're saying is you know um it increases an angle and then you also so you go both forward that but you also go down in sort of whatever you know angle you're going you're going at right so ups down down is up um [Music] so we're going to have to let's see rethink about this so that's five all right so we're still going to do some of the same stuff right um so down and up don't actually do anything anymore right we're just sort of tracking tracking like what angle we're going in but when we do a forward we are that's eight to your thing but all right so here is okay i'm just gonna add the sort of i don't know if let's add let me bring my all right let's grab part two and we want our 900 all right why is it mad about this oh because part two still has this okay all right so i think that this is going to be super similar so i'm just going to copy it all over and we're going to alter it somewhat right let's look at it again right so our depth is 0 but and we're still going to be multiplying those at the end right but forward does two things right um and depth so depth up and down do the same thing right we're not thinking about it as progress anymore though because progress only happens at forward so every time we have a forward position we're gonna be two doing two things right um [Music] oh no we need an aim that's right okay um so all the things that i was saying before are based on let's call these all aim right because this is what we were doing before was actually determining aim right now we need to add a depth and what we're doing here is when we go forward we're going to depth plus equals um aim times instruction slice one right oops test it passes all right so we're doing that right let's go ahead and uncomment it out do that oops what have i done don't want to test it um run it and my result is a very large number see if it works we did it one gold star okay all right that's it um space bar uh we were able to use reuse most of our stuff um didn't retest anything i guess i could have tested the like depth the name right but most of it was just sort of thinking in our heads all right so that's it that's day two and go i'm taking off i might do a little earlier tomorrow um just so that i can get more points with all the boards that i'm on so if you want to watch live you're gonna have to get on earlier too all right take care bye
Info
Channel: misterjacko
Views: 17
Rating: undefined out of 5
Keywords: AdventOfCode, Golang, LearningInPublic, Programming, TDD, twitch
Id: ke3XP99lHOI
Channel Id: undefined
Length: 28min 17sec (1697 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.