Advent of Code 2021 🎄 Day 9 Part 1 🎄 Golang TDD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome to day nine the advent of code um done and go tdd style um let's let's get to it right ah let's see what's the last thing that happened um what were we doing i forgot what we were doing last time oh yeah there's the there's the display we're trying to make it through a series of tunnels or something like that right okay so now uh these caves and tunnels seem to be lava tubes oh that doesn't sound good and parts are even still volcanically active small hydrothermal vents really smoke it slowly settles if you can model how the smoke flows you might be able to avoid it and be that much safer submarine generate generates a height map of the floor the nearby caves for me smoke flows to the lowest point of the area all right so consider the following height map okay so like if this were to be some sort of topographical map that dips okay so and even though the you know there might be basically it's saying that this is the lowest point based on whatever is surrounding it right here right there's no lower point in the surrounding vicinity um so it is the lowest point this is not the lowest point right because there's like a one here this is not the lowest point because there's a zero here everything beyond the zero is larger okay find the low points the most locations have four adjacent locations up down left right diagonal locations don't count um uh four low points are highlighted two are in the first row blah blah blah um all of them have lower adjacent points which is defined by uh left up down left and right um risk level for a low point is one plus its height [Music] um all right all low points in a height map all right let's go ahead and open this up that's actually that's not so bad that's not like huge it's not that many rows it won't take long to iterate through it right we can already find let's see there's a little point right there we've found one right um let's uh let's look at what we got here um that i believe is going to be multiple rows right so we're going to want to pick this one have multiple rows yeah but it's not just we can still use this and we're gonna no let's let's find one that has um sort of similar stuff but has their id's because because we i think we wanted to turn these into integers which is what we're going to want to do here so that we can uh copy them all right so let's copy seven rename it to [Music] zero nine um change this today nine and then we will still be into it let's get oh you know i probably should have picked a different one actually just because um this is just a one-liner whatever we'll figure it out um yeah there's only hundreds it's only 100 by 100 probably so only a million only a million numbers to go through right a million numbers to compare to four numbers so that's what four million comparisons for this first part shouldn't take that shouldn't take long not when we're using a compiled language all right let's see in our test input is this and let's look at our tests that we have um part one part two we're not doing an ordered slice we're gonna make int we're going to make a slice perfect the only difference is you know what i could just do right here now it's not a map right but it is a list list uh all right so what do we got we're gonna have this comic all right see these keyboard tricks are awfully helpful okay even though we're not ready to test yet let's go um look at our gains that we can clean that out um part one r2 get rid of those don't want that do you have a load input we do have a okay so we're going to keep all these um [Music] we're not actually going to split them on anything um right because there's no commas there so let's go look at this right it's just uh let's actually let's go ahead and test that i know that it's going to fail um oh it's not happy that not that at all um oh yeah that's okay we do want that um let's um you know let's pause this comment some of these out as always we'll be returning an int um oh that's because i haven't so all right um [Music] let's go ahead and test it again undefined load input yeah because we commented that out but that means that we should have passed our other test all right so now we're back to load input so let's look at load input um i have seen other people kind of do a little bit more um right so this file right here is um [Music] import those things it's just like a byte array right which is just basically everything i believe um and then if i string this right it's basically the whole thing with the um so it's instead of caring about how many lines are in an input right it's just capturing that new line character right so the first thing we can do with this file is say like data lines um we can just say data let's just say data right um is so file so we're just going to do strings split we're going to split um file that's not right yet um i'm going to script split stream file on uh the separator new line right so now we have each of the lines right um and data is a list of strings and then we're going to split those on nothing right so right so now we have data right so for so this is going to be the string right of of numbers here right and what we're gonna want to do is uh make a slice of those make slice of numb um so that's going to separate those as uh i mean it's fine for this one right that we like we could turn this into an ant and then split it on characters but for these uh actual data that is like super long right we wouldn't want to do that um uh so we're gonna make those and then we're going to do a line slice equals no we don't need to do we need to do that yes uh we're just gonna do it um so what i'm saying is we're going to have a list of ins with nothing in it right so now that we've uh so for [Music] range string slice right so now we're iterating through those and then we're going to line slice append line slice make int um string okay so now this should give us that list events right and uh our last thing right here is we're going to be um input equals input and then we're going to add on that line slice okay and this should be sufficient to give us that return value we're looking for so let's go ahead and test it and it passes let's just make sure that i didn't have that still commented out okay so that's not commented out we are returning that list events okay now how are we going to approach this problem um i am thinking that the way that we are going to approach it right is iterate through each of these numbers right or you know each of these numbers and what we'll do is just to just to make it easy on ourselves right we're going to do we'll assess the first we'll assess the first row right only looking at the you know ones before and after or under right and then we'll do sort of the guts of it that is always going to have one on the top of the bottom and then finally we'll do the last one that only looks on top right so okay so let's look at the um okay so what we're going to be passing into the we'll call it the normal one you know we could do let's think um what should we call this um map ends okay so we'll do the ends first how's that sound and what we'll do is um input one is going to be here we go all right this is what we'll do um we're gonna have just the data here right um what we'll pass in is three things right we'll pass in for example the first the first line will pass in the first line so data zero the one that we're going to be sort of comparing it to which is data one and we're going to put it in as a phone call i don't know what that is um i'm expecting some interruptions today um i have my equipment dropped from my new job happening that i need to sign for and then my car is also in the shop and i need to pick it up so i do get interrupted i'm sorry hopefully my dog will bark him up if the delivery person comes did check to make sure that the doorbell was charged all right so what are we going to do i want to i want to use this map ends one twice right to say um [Music] like you know if the uh and we could just do a boolean right so like the first one is is true right so okay that's not like meaningful and i don't like it but uh oh i could do that right um okay so the index for here would be like zero right um and it the index for the last one will be not zero okay now the question is what do i want to return um probably a array of like what what there is any any low points um or could just be like a sum right the sum of those values uh so in this case it would be uh one two three okay so let's just do that three um so that would be the first line and the last line would be um data data minus one right no that's what should be here um so data length data minus one [Music] uh oh what order do we put it in oh and the reason um so yes this is the target one that we care about and this is the one one that we're going to compare to so for in this example the first one right actually so this doesn't matter right we'll just um we don't care about this uh extra value right we'll order them in ourselves right so first one is the one that we're mapping right this is the for the first line it's in the first position this is the one that we're mapping and then we're comparing it to the second one which is index one and this is index length minus two okay and what we want to find here is five and we want the result to be six six right i think that is a sufficient test to check those ends let's go ahead and build our function here our first function it's actually doing things all right uh f funk building happens um so target lime when i learned i could do this the other day too now if they're the same i can just do a comma and then declare them at the end and what i want to return is an inch all right so all right so what i want to do is um and again this is for just the last ones uh i'm going to want to compare one just gotta make sure that so there's another thing that i'm gonna have to deal with right [Music] which is how to compare those lines right [Music] let's actually make that first let's save that test will fail here so i'm going to make a test for a function i'm going to call like is lowest or something like that and what i'm planning on doing is it's lowest uh and what i'm going to be passing in will be um let's just call it let's just sort of look at this right a value right and then a list events that way we can sort of make it expandable of the surrounding ones and they can be there can be any number right um so in the list the example of this 2 right here it would be 3 and 1 right and we want to return a false because it is not the lowest because one will be lower right um okay and let's do another one let's say one right next to it and surrounding it is um nine let's start with two nine and nine right uh and it will be the lowest that is the lowest one is the lowest of these numbers right um let's do another one just uh to show that we can do like um sort of all all four so this one has we're always going to be comparing at least two numbers three numbers or four numbers right so let's uh say seven right here right we'll do that seven uh and it's got nine eight eight six so nine eight eight six in the order doesn't really matter right okay so let's and of course we don't need this data here okay um i'm not gonna bother testing it because we know it's going to fail let's is lowest and then remember we're going to be um what should we call this oops what do i do here um what should we call this um point which is an inch and neighbors here i spelled that right it is an int and we're going to return all right so um if at any point a number is lower around it we're going to return false right so we have to like check them we have to check them all right so um return true is going to be what we what we get if we don't return false and exit in the first place right so we'll say for [Music] um i don't care about that it's okay so if uh if the neighbor is less what do we do when we get tie um [Music] lower than any of its neighbors right so it has to be lower it can't be the same right um which could be problematic because that could have like a two-digit low point let's just assume that those don't exist right numbers aren't right next to each other i mean we could make a um [Music] i mean so there are numbers that are next to each other here right but they're not low ones maybe we could put in there's something that that checks for that um okay but we're not going to deal with it now if the neighbor is less than the point um and we're just going to return false we don't want to test its low its lowest okay so is lowest should pass which it does we can show that by we'll just comment these out real quick so that'll accept anything of any size right um which is helpful right to pass stuff into it right so now we're going to have to look at how we map these ends um and we're going to do a similar thing right for let's start let's just uh for we could just handle errors but let's not do that i don't want to i don't want to get caught up in dealing with errors all right so for [Music] um we'll just use a standard eye but we're going to start on index one right [Music] that way we have three and we'll check the first and last ones sort of on the ends okay so let's actually check them first um if is lowest target line zero right that's the first value and we're going to be care comparing it to target line one and reference line zero okay so [Music] if it's lowest then we will total plus equals target line zero plus one right and okay so that deals with the first one let's finish this for statement um until i is less than the length of target line minus 2 right because we're going to make an exception for the end as well and i plus plus so that's going to that's going to be our logic let's uh do this last one here it's kind of kind of janky right but um so this is the last one right the last index right and what we're gonna pass into it is um target line minus two right so the second to last one a reference line target line minus one we're going to return okay we gotta get rid of these should be plus equals all right so now we've dealt with the top of the uh the last in the first index and now we need to um deal with the bulk of it which will be um [Music] sort of a similar if statement here but it's going to be utilizing i a lot more um all right so i plus one i and then minus one and that should that should return our total okay go ahead and test it passes did i have that commented out no i got it okay good it's returning those values um [Music] that we want there okay now we need to do sort of a more of a bulk right um let's say test bulk it's kind of gross um we'll change it to map center because i don't like using bulk just sounds kind of gross all right so in this case we would have map center with sort of a two target lines right um let's see we'll do we'll do we'll do these two right so our target line is going to be line one like the one that we're looking at and then we'll also be passing in zero and two right and we should get back zero from that and let's make this smaller so we can see this better um [Music] and then we'll do zero one two and the one before it would be one and the one after it would be three and what we want from this is six right because we're going to hit a 5 here that's weird that these comments don't like seem to come with it until i save it all right um obviously that'll fail our [Music] function here is going to be pretty similar right um certainly the is lowest can be reused [Music] but it has target line above line below line okay and all right so but we have to do a similar thing here right um where we uh sort of our dealing with the first index right so in this case um [Music] we're going to pass in zero we're going to pass in uh target line one right and then we're going to do above line zero then we're also going to do below line zero right uh plus one above and below right so that takes care of the first part let me drag this over so that we can see a little better all right um and then the is lowest one here we'll do the same thing at the beginning but then we've got the second to last one and then above line minus one below line minus one okay and now for the bulk of it we have the same thing target line and then below line and above line right now go ahead and test that and we pass all right so now we can we have functions to go through the whole thing [Music] as we iterate right so for part one what are we looking at uh risk risk levels i guess we should make a test for this first right um all right part one um our input is going to just be uh data that's what we're getting out right and we want the uh result to be 15. so we're going to start with zero and we're going to do sort of a similar thing that we're right doing here um so i'm just going to copy it um no actually we're not going to do that at all all right so remember that these uh map center and whatever are returning an int right so first thing we're going to do is uh risk level [Music] plus equals map ends data zero right and data one right uh finally you know what we could um probably just copy this right all right so this is us doing the first and the last and then we're going to do a for loop [Music] for i equals one until i while i is less than length of data [Music] minus 2 because we don't want to do the last one right um plus plus risk level plus equals map center and then we'll do that with data i and i minus one i guess the order doesn't really matter and data i plus one all right let's go ahead and test it and our tests pass let's go ahead and run it and actually running run it again [Music] right in a couple microseconds oops just type it in um one eight three zero no it's too high it's too high hmm so where am i getting it wrong because my test code is passing in my data correctly right let me um instead of doing this i will do load input just because that's that's what we should be getting out of that right but okay um all right so what could be wrong what could be the difference says my answer's too high which means i'm finding too many spots right let's look at maine.go you know what's weird so i am splitting them all up i'm cruising through them did i one eight three zero one eight three zero okay i typed it down right um [Music] hmm so this would be the first one this would be the last one and then we're gonna go [Music] index one which is the second one up to index instead of length and minus one linked animation second last one i i minus one i plus one which should return an int data right there most one this is the last one and this is the second to last one that we're comparing it to yeah i don't know what's wrong with this um let's make sure that my data it's all there that i didn't like erase a digit or something um one two yeah got that hmm and i would say i would say that maybe it's too low right um if i wasn't counting the i'm adding one right this lowest map ends total is that plus one what's wrong with it huh and puzzled wish apricot was here to save me um [Music] nothing should make any difference wait what's this so the first one target line i'm going to make this a little more sensible the one before it and then after it we're comparing it to um the one above it the one below it at i right target line eye above line and below line okay when we pass it in above line would be minus one below line would be plus one what is this i honestly don't know why this doesn't work this seems like it would make so much sense i'm just going through grabbing the numbers all right i'm gonna start um doing something here all right um we're going to print line sort of an extra step right um but now i can debug it um for value all right so i and okay so let's go ahead and test it right so oh because we didn't um what's this about wait why can't i need to print this out i don't i forget how i do that uh just printf formats and write sustained output okay um [Music] right right so this is telling me so we're skipping um in this in this case we're skipping line uh zero here right because we're um i mean we could we could just add that word geez stop talking um we're skipping line zero and skipping the last line so we should have line one with zero line two with six why don't we have line three well we have line three i wonder if that's the problem that is the problem that is the problem i mean that is a problem 1 8 4 3 but so that's different but i don't think my number was i think my number was too high right not too low yeah my answer's too high um yeah making making more of them well i mean at least i'm assessing all the right numbers now right um let's this only has to happen once right so i'm gonna go ahead and i need to add a new line here it's not super ugly i'm gonna get there eventually all right and we're gonna do sort of a similar here um but with this okay so yeah okay ups i'm expecting fedex though target okay um oh there it is this is the repair shop one sec okay i don't know what's wrong with my stuff though all right let's look back at our lines um six six three that's that's what we should get right um let's go ahead and um run it and see what we can see in our just like our last line i'm sure that it's i mean looking at the like first line and last line i'm gonna i'm guessing are the problems right um all right so you guys can't really see this can you let me go to my input here make it a little easier to see all right let's look uh i will what will i do right i'm gonna put uh just to make sure that i don't screw it up all right so if i see a character that is the lowest i will 1 right 0 9 9. seven is not right because there's six oh one is not here either is it because there's zero on the other side right um nine nine nine seven has six on the other side uh eight seven six five has four on the other side six seven eight nine five four three has four on the other side so three is one there going up down to zero i'm going up back down to five but it's got four on the other end six seven eight nine one zero i feel like these are way higher than they should be right like the total is not 35 on the bottom here can't be seven has six on the other side so six five has four on the other side uh one has zero three four five six nine three two it's legit nine six five has four on the other side four maybe we'll get up to it nine nine eight seven three two has one on the other side three four five six eight nine one all right so let's go through there this should be one um just make this big and i'm gonna one four one one three five two and if we add them up we've got five six seven ten seventeen seventeen and here we got thirty five why why do we have 35 why do we have 35 why do we have 35 i'm not counting something right somewhere and i'm just not going to save that so it won't check it when uh when i go all right so when i map the center data one right no i mean it's it's getting when i it's getting what i got my logic my logic has to be has to be off somewhere because it's like what it's like is that i've got this is lowest and it's like stuff is coming back true let me um um we're just going to look at this line 9 here or line 99 and oh geez yeah it's returning some nines here why would it return some nines let's look at our input we got zero we got three we got zero we got zero we got 2 4 and a 9. why would we have a 9 and why don't we have this one i'm stopping too early sometimes i think all right let's look let's look um first and last okay test map ends we're gonna go well that's not good why is it doing that the only place i told it to print the line is if it is lowest all right so oh i know what i did so i'm saying if neighbor right if neighbor so say the number's 9 right so the point is 9 and all the neighbors are 9 right um then the point then this is false right and so that's why we're coming back with some nines um how do i how do i think about this is this returning nine now why did i change cute input 15 line zero three line two six we got here three two six five six well that should be 15 am i not oh am i not adding that on um oh yeah i'm not so now i'll pass that now let me get rid of 504 way lower no no it's too low let's uh i'm missing i i did notice right remember that i missed that last um line 99 15. let's go look at that right so we had 0 3 0 0 right two four and we're missing the one right why are we missing that one is it right yeah that's right so why are we missing that one i'm guessing that we are not getting to the end of our line um that's what it is 2 5 12. boy you guys this is ugly today am i doing here 5 12. it's me to wait one minute though [Music] contest it again get rid of our comments um and it does pick up that one right okay where is that really looking forward to whatever part two is that's gonna that's gonna wreck me isn't it okay right answer gold star finally freaking finally all right um next we need to find the largest basin so that we know what areas are most important to avoid the basin is all locations that eventually flow downward to a single low point there for every low point so we can find those and map those right um uh basin is all locations eventually flow downward jeez so basically what let's basically find everything that is separated by separated by a like a line of nines right oh i'm not having to pretend how to know how to do this yet let me get rid of this input uh i've got no idea i got no idea how to approach this i mean i know that i could like iterate through and find all the rows that in columns that aren't nines right but how do i like how do i map that um do i map it based on sort of the first the map based on the first coordinate right so there's a map with an identity of zero zero or like a a basin with the identity of zero zero and its size two right and then you hit here and then one with size this and it goes until it hits a nine and it's five right now we start here and see it's not a nine and a attach it to the zero zero right here we got one that starts a new basin the problem is that there has to be some like normal person way of doing this that's not the way that i'm thinking of it um because yeah i can't even pretend to i think that approach could work but how do i can a map have multiple values now i can do that i can do that maybe right i can make a struct that is an x and a y coordinate essentially right i can make an array and fill it with all this garbage right um i go through if it doesn't you know if uh oh man that's ugly right um okay so maybe not or maybe i do it you know i've been i've been thinking about just like how i can do it efficiently right but maybe i just need to do it right so the uh if a key was this and then a value was another struct right so it would basically be like um let me throw it in tests here right um map um chord chord or chord list of chords of map of lists should i do that create a map of string i don't know this might this might be the one this one's hard maybe i just the thing that i'm concerned about is that i last time yesterday right i started this off um with a idea of how i could um it was like if i i could see it right i could probably eyeball it right um all right i think what i'm going to do is i'm going to stop the stream and see if i can learn some stuff i just don't even know how to like approach approach this right now um so i'll think about it and hopefully i will come back to it all right in the meantime got one star i'm worried about this weekend man i'm gonna die if they're hard all right um in the meantime take care
Info
Channel: misterjacko
Views: 8
Rating: undefined out of 5
Keywords: AdventOfCode, Golang, LEarningInPublic, Programming, TDD, twitch
Id: _8HKOPA53oc
Channel Id: undefined
Length: 87min 42sec (5262 seconds)
Published: Thu Dec 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.