Advent of Code 2015 - Day 9 - Golang TDD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
sorry i didn't join y'all to do this um on my lunch break it was a very busy day for me my kids had like parent teacher conferences and i was doing solving interesting problems at work so lunch time flew right by but i'm here now we're going to get to it let's do that all right so we are on day nine here i believe i think so uh let's look at the problem okay every year santa manages to deliver all his presence in a single night this year however he has some new locations to visit his elves have provided him the distances between every pair of locations you can start and end at any two different locations he wants but he must visit each location exactly once was the shortest distance he can travel to okay um let's see so this is going to be a problem with um sort of questions of how many combinations we can make um like in this situation there is london dublin belfast right so there's three different locations um and then of course six different uh combinations so what are we how are we going to think about this um i wonder so i'm my first my first thought is that we're going to get a list of all the unique locations cities probably hold on let's look at the input okay one of these planets okay suns okay so we've got one two three four five six seven eight okay so we've got eight unique places so we're gonna have to make our list of eight um hopefully go has a library that shows us every combination of order and then maybe we can make something like a map that shows us the distance between you know each of these so a map that has all of these in both orders right and once we have the map of all the different orders what we can do is iterate through each of those combinations um taking whatever index we're on and whatever the next index is finding the distance between those two and sort of summing them up as we work our way down the um that particular combination total it up and then go on to the next one and keep track of what the long the shortest distances i'm guessing that that might be the most straightforward way to do something like this so let's see first i know that they're in python is um it's called combinations or like iter tools something like that um okay so inner tools is the library combinations is the function i'm not going to click on that because it always has that pop up at this site um but basically what it is is you would pass in a list in this case we would generate our list it passes in that and it returns all of the different combinations of that nice and quick um let's see if there is we go i don't want to have to make one there we go it's exactly what i want your ball and has all my combinations in this case it would be a string um a string for me yeah yeah okay let's look at that tool oh and that was the that was that first okay so i'm gonna have to use this library right okay first let's uh let's set up our let's set up our stuff and it's gonna be let's look at our input copy six name it nine get in there glad i don't have to make this myself all right first let's grab our input all right clear out our tests um i don't think i'm going to need anything special here um we're gonna need to test an array i'm not sure we're gonna have to make some strings to ins right because these are gonna start off as strings so i'll keep that test i'm not going to need this test i'm not going to need that test we can just comment out part one part two uh and then we'll comment out these as well all right so now let's look at the actual functions here we don't need this instruction um we will keep this function i'm gonna collapse it down though um [Music] you know we're gonna let's just keep we'll keep this we don't need to do these replaces right um we'll just keep it as as they are uh we'll keep string to int we're gonna make instruction change light make arrays part one part two um let's see hold on what are we trying to return answer is a number all right so like last time remember how um i had this funny thing where it was returning a value that wasn't what i was looking for because it was kind of close so i'm going to start returning 999 instead of one like i was so that hopefully if i do that again and i'm calling the wrong function that it will it'll warn me a little bit more because that would be helpful actually all right so we've pulled everything out let's get back to our tests okay so let's set this up as our input test and what we're going to do is we want to return these strings i'll fix this formatting commas tabs okay that's what we want to get back if we put in input string let's test it okay so we're able to read our file just fine now we're going to make an array out of them uncomment out this and think about what we're going to want [Music] take this whole thing this is what we're going to want to get and we're going to be passing in this string all right go ahead and test it now i'm sort of cheating oh oh i wonder if i dropped uh yeah oh because i had uh i had two different ones here before let's get rid of these actually we only need to test one i think um okay that's all good um so now we gotta start what are we gonna start doing we're gonna start making a list right um and [Music] what are we going to do first so let's grab this um i don't know it's called and go but a set um is typically all the unique items right from you know a list so what i'm going to do is i'm going to pass in so this is for the first part so that i can make my combinations right um i need like a unique set of things um i wonder if i can you know i could probably do that at the same time as i'm doing other things but that won't make it testable right and we're trying to make everything testable so i'm going to do independent um sets of things right so let's uh make set and make our function called make set and what we're going to do is we're going to be passing in uh this load input because [Music] um we're gonna pass in that whole that whole list right here or should we pass in no that's not what we want because we want to be able to sort of traverse over all of these these two different indexes in this array so instead of having an array results we'll have test input and it's going to be a list or what we call girl a slice containing strings and too many of these all right um let's get all of our values here and belfast belfast dublin london dublin london belfast dublin belfast now i mean this doesn't really matter these numbers all i'm going to be caring about is these index 0 and index 2 but we'll still input that as if uh you know in the way in the way that we expect and what we're gonna want is i don't know i don't know if the order matters well let's let's just see right um get rid of this london dublin belfast right that's what we want um okay obviously this is going to fail we don't have that um [Music] make set set up yet and i don't know if this is going to be the proper way to have that result we'll see sort of what order the things come out of because we don't care about the order um because we're going to be using this combinations library here to [Music] you know make every permutation of this right um we don't care what order these are in but we're going to need to care for the purpose of passing this test so in the process maybe we can learn a little bit about how it makes sense maybe you know i'm you know might do it in like alphabetical order or it might do it in the order of um adding so let's see go ahead and make set let's make this just do this in order and what are we going to pass in we're going to be passing in [Music] i mean that's essentially going to be the data right that we that we pass in and pass it so when we yeah so it's going to essentially be the same thing it's going to be no that's not what we're gonna do we're gonna have made our string our array first um locations um and we're going to be returning um [Music] a another set of strings listen because they're going to be all the different locations in all the different orders and i i'm assuming that's what the um [Music] what it's going to return yeah but just not it's you know strings too good um perfect so i'm wondering if there's a set type in gold i'm guessing it's just going to be a map with like a true value or something like that whatever if um i'm just going to make it like a map um map and it's going to contain a string and a bool and then we're going to say 4 i don't think we care about our index um we're going to range over the locations and the line is going to be um a uh not an array that's not the word in and go it's going to be a slice right and we are going to be looking at index zero and index one so if i'm so bad at this one i'm gonna look it up i'm gonna look it up every time i swear i know exactly like where on the page i have to go all right if um so we don't care about the value right um because we're just gonna set that to boolean right um okay set that's in the dictionary um well i wonder if there's there's definitely a better let's um so if it's not okay so this is false right then this i think would would get us to the point where we only care about what we do if it's not in there we'll just add it right set line 0 is the key and we'll just say equals true it's very very tiny and then we'll have to look at the other one for index two zero one two is the next one two all right let's see and then we're going at the end turn that set yeah here we have oh you know what i don't i don't need to do this at all right hold on i'm gonna comment all this out for a second um i'm gonna try something else by bye see so we're still going to do for line-in locations we're just going to do if i think there's isn't there contains um i remember contains method for maybe doesn't exist oh they're they're telling me they're telling me to make okay never mind i take it back um let's see i don't want to make i don't want to make other things right um okay some of these things are just so much easier in in python aren't they um all right so we're gonna make a map here um since i want my um just set of strings and i don't care about the other stuff um i'm gonna do iterate over the map keys right um to return just that stuff that i want that's a really big picture all right so for e we don't care about the value equals range map and then we'll um that that dot and oh yeah that's right um i think all right uh i think we can check it now um is that a squiggle whatever let's try it um it passes i wonder what happens if we were to [Music] swap places there yeah so everything is as it should be and we've learned a new thing which is it adds an order um which is maybe not new for you but it is new for me and now i know all right so [Music] what else do we got here so we've made a set now we have to do um those combinations right this is going to be a pain in the butt to test [Music] but let's do it anyways um make combinations so our input is going to be essentially the result from this right let's um all right so that'll be our input and our result should be um ah boy london dublin belfast i'm just gonna copy these all right oh boy we're not going to i have no idea what this output is going to look like so i'm just going to say okay we're going to output all of these but we're going to check the length right [Music] so there's going to be we're going to want six and we are going to look at the length of this result which is cheating maybe um oh because we need to call it make combinations right okay so i don't know if you see what i'm doing here but because i don't know what the combinations are going to be i'm not going to even try to uh make those look like what they're supposed to look like but i do know that there are supposed to be um six different combinations so instead of assessing what those combinations are i'm just saying that i should have the output b6 in length all right so is that cheating no it's it's clever but maybe it's two i don't know we'll see though i just don't want to write that test all right so what do we got um function make combinations and what we're going to put in there is a location list and it is going to be a list of and what we want out is going to be a list of lists containing strings right dublin london belfast one in dublin belfast on the belfast dublin right all the different combinations is what we're looking for all right um now it's time for us to try to get um ernesto suarez's stuff going here so let's all right now we're going to throw this up top of import that should be fine um okay let's collapse some of these okay make combinations so um so let's just uh so what they got here well let's scroll down to [Music] slices of strings right so we're this is what we're passing in here right this iterable right um [Music] and okay so let's just style it as this so we're going to r equals uh the length of location list right um [Music] iterable is what we're bringing in right so [Music] um and combinations is going to be empty but it is okay now we're going to do what they got there right or b in here's our list bar and what we're going to be doing is instead of printing out that line we're going to be combinations append combinations b right at the very end of this combinations all right let's save this i wonder if this should import i don't want that all right so hmm bye do i spell that right yes yes i did all right why i keep getting rid of it all right i don't use many i've never used many public libraries so what i need is i wonder if i need to like close out of this and then come back to it sometimes that happens i wonder if i need to there we go just had to give it like an alias here um all right let's see how it works all right didn't work um let's print v first just like they did and see what we get so we can see what we're supposedly adding oh it's just failing why is it failing um all right we're gonna have passing an error int and we're gonna return hmm why wouldn't it give me oh it is print one out here it looks like london dublin belfast is that what i'm passing in london dublin belfast but it should be giving me more than one it should be it's it's like it's failing to range over that list um it should do all of them but it's only returning one of them anybody has any ideas for why that would be my string looks like right can make it um it's not a list anymore now he's returning one why pass in iterable which is our locations list r we're passing an int which is r i don't like hmm i'm just gonna hard code three here and see what happens the problem with that length oh now what's it mad about all right well it still did it only once is he gonna change the order on me all right so i guess sometimes it does it in different orders that can be problematic i suppose let's uh do the same thing you know that would actually be not too bad um now that we know that is gonna work let's just um set it there and then we can at least know that that works all right um [Music] why isn't this working you know it's hard because i don't have any insight into what uh this library is doing hmm let's let's do this again um just with the integers um you well it's only doing it once why is it only doing it once okay let's think ernesto you're you're killing me um so we're doing combination oh is it combinations or permutations i don't want combinations i want permutations don't i all right well maybe that's my problem i mean i okay well there that's what it was i was doing combinations instead of permutations now i'm passing um all right let's uh i think we're pretty close to um attempting to solve this um i wanted to get this done today but that took a good long time though i've learned a new library so that's fun um all right so oh i've only made my list of combinations now i need to make a map um containing all right let's we're getting close guys we've got one more major tests make routes all right so um in this case right we have to make the routes right we're going to pass in london to dublin let's uh i guess it's gonna be this list right um and what i would like to get out of it is um a map containing a string i guess it contains two strings i wonder if i can do that is this possible um and the result is an end and i want that to be the first one how do i do that um what would that even look like um it's an invalid type well that makes sense so how am i going to do this am i going to [Music] so here's here's where i'm at i don't know what order these two are going to be in right what i need is to be able to tell whether it's either london dublin or dublin to london right so i have to have a map that contains both of those keys with the value for 68. um i'm wondering how i can do that i'm not talking i'm trying to think um london awesome all right um i wonder if i can nest maps is this taking it too far that is one of the problems with um these sorts of go very strict structures for me is that i have a hard time i'm trying to but you know like i know the way i want it to look in my brain but it's it's sort of hard to you know figure out this part without um without uh let me say practicing it a ton so this is what i'm gonna do um i want to make just the keys like this and i know it's not right i know that it's probably ugly i know it's probably slow but let's do it and get through it all right so that's what i want my result to be um obviously in the um in the real world i'm just gonna oh this is hard um what's your problem why don't you like this i mean missing key he gave me the key do i have an each i have an equals all right let's see how to make a map literally map in string that all right so they're just commas not uh not brackets i can slow my freak out a little bit all right um well instead of strings i can have string string do you think that's possible let's see i think i had looked at this before and it was not okay yeah invalid type um all right so let's run it um fails let's go make that route and what are we passing in um we're passing in we are returning [Music] a map string i don't know if i should do it this way because i don't know how i'm going to be able to combine those um it's gonna have to be all of them isn't it god i'm gonna have to make this test bigger like like this this whole thing bigger than um i thought all right sorry we're everybody to we'll just do two of them um and return that map of all of them right um so london to dublin and then what was my second uh london to belfast equals 518 and this is what i want london to dublin um dublin belfast all right so this will let us you know pass everything in um what's it made about here oh okay so this will be the whole thing here this will be the whole thing there what's in that about here okay it doesn't allow it in because i haven't said that yet all right so okay so we're going to pass in [Music] our whole list in this case just two of them right um [Music] and we're going to return a map right so uh for i'm gonna make our map first um route map equals map string and um and maybe we'll want to make make another quick little function here um map key um and we're gonna pass in london and dublin um this all the irish people today but by writing dublin incorrectly over and over again making it doublin all right make map key and what i want my results to be is um two values right how am i going to return those maybe i can just put in parentheses you know let's um no because i want to split it split it on the hyphen okay this is work it's not happy about it yeah it doesn't like that um i mean i can return you know an array let me do this that's that's gibberish to me right now i don't understand what that is um you know it's frustrating because i'm pretty sure that i could get through this challenge right now i'm trying to write the tests i'm trying to now this is what we're going to do we're going to just test this and see what comes back and um um what's called mcmap key pass in return um how are we gonna do that uh we could just do that in one liner i bet um training [Music] spring i think that's how it works so we're gonna return inverse shoot what do we got here we just need a comma um i don't think we need all of these hyphens let's see all right what are you mad about want one got two i told you you're gonna get two why would you want one oh i don't know if i need to have a comment here there we go uh all right so let's go test this it's gonna fail we know it's going to fail um [Music] wait what are we complaining about right now did i not call that mcmap key uh yeah i'm passing in two strings right just let it fail right all right um so i think it's mad maybe i can't test this sort of thing um i wonder how i could test it here this is what it'll be fine won't it i'll just test it when i'm doing this um i'm gonna test it right here when i'm when i'm making this right all right so make test routes does it test test make routes um wait where am i make map key make my p test make routes okay um london dublin right so we're passing in test input we would like to return out these and they might not be in the right order but let's just go with it um so we're going to need to iterate or um we don't care about the index line uh yeah let's cut line in range so this should now have us inside the array right and we need to make a map and the way we're going to do that is not have to look it up but instead check here right if since we're doing both of them at the same time um so we're saying if the roadmap and then we got the key so the key is going to be one of the keys is going to be should i just make two even this is like silly i'm gonna return to here um a map or a slice of strings and return okay well now we can test it i suppose let's go back here we're gonna pass in london dublin and we're gonna return with london dublin all right so that's passing is it oh build failed that's because i'm moving too fast i don't have okay let's test this define make routes that's fine but uh otherwise um that other past looks like a past let's fail it real quick [Music] jesus bring myself all around there we go expected actually wait why is it why is it mad about that this is those are the the same things what's different oh that's what the problem is all right we're we're back we're back all right go back to make routes all right so we're bringing in a string okay so what we're gonna do is we're gonna say uh we're just gonna look for the first one right um and so uh that key is going to be make make map key [Music] line index 0 line index two remember those were the two um we'll just call that key valves because it's going to return both from right that's going to be a set of strings right and we're going to have that be key evals 1 or 0. we'll do 0 the first one right with the idea that if you know in this process we're going to be adding both sets right um and if it doesn't exist we're going to add both if it does exist both should exist right so we're checking to see if let's drag this down a little bit so a little more room so i can copy my crap up here all right um so if they are not okay so um if they're not in the map right um i need to come here okay spelling mistakes all right so if they're not there i need to add them right so root map evals zero uh equals um this is where we can have the string to end we're gonna need to check this um put it up here because um [Music] line what's that value uh zero one two three four okay and we want to also add the second key value okay so this should be iterating through all of the instructions and making [Music] those key value pairs um for uh for being the key of that of that list right so we have all of the possible routes let's return um route map all right so i doubt this is gonna go perfectly um maybe it will did not it is just doing it out of order did i populate that wrong tobin bell hospital plus dublin london dublin oh because i have london dublin and london belfast all right making my test wrong it's mad still but i think that it's just a matter of it being in the wrong order so we got belfast london with that value dublin london with am i doing these values wrong jesus okay yikes i'm just not being careful right passes okay so now we're able to make all of those key value pairs now the last thing we're going to have to do and i don't know if we need to make um add this too so we already have it going um let's do part one and we'll load the data and what do we want to return um the shortest all right so here is what i think that we need to do for part one and hopefully we only have to do okay um first we need to um get our list right we'll call it site lists and we are going to set that equal to just i'm just collapsing all these so that i can see their names um i can pull this down too uh and that's going to be make set right um i need to make my raise first i think i quoted myself into a pickle here um my issue being what can i say a minute um what did i do so here is here's my problem right now is that i have a little bit of an issue where what i was going to do is make that initial set the problem with that initial set is that it's supposed to be um that list of strings instead of just that input that i have um let's look at my tests so here's the problem is that i start my process with this list right here right [Music] okay i know what i'm gonna do instead of making that list of sets first i'm going to make my combination right because that is what i have um i shouldn't have done it this way i wasn't thinking it through um i'm just gonna make a list of strings right okay so four don't care about index um line in range data all right i'm just too i'm just too far to go back right now um all right um and array date and we're going to add um array line okay so this should get us array date data um [Music] this data type that we need to make a set um so let's let's get that um set equals um make set with ray data all right um all right so now we got that location set all of the individual [Music] guys there right um location combos equals make combinations with location set okay so now we have all the locations in a list all the combos in a list and now we have to make routes oh well we we want this good i'm glad we have this array data right um because we need it right um [Music] routes equals make routes with ray data all right so now we have our routes and now we have um okay so here's here's where the here's where the magic happens um i'm going to iterate for um i have to make a like a ticker in a second but i'll get back to it um so for don't really care about index um [Music] and combo equals range location combo so we're gonna um traverse through all of those combinations um right so now we're one one level in right and then we're gonna go over those four and we're gonna need the okay so now here is where we need to start adding things up all right so i is the index we're starting at zero um imagine a world in which right now we have sort of a list of dublin london belfast right this is our first set right so the first thing we have to do is bind our key which is dublin london right so um [Music] we're gonna make another guy here similar and i'm not going to test it just called make key it's going to have string 1 and string 2 but it's just going to return a string and it's only going to return [Music] them in this order string 1 string two right because that's the order we're in right now in that order okay so where are we going to get string one and string two from um let's make them key equals makey with um what do we got me key so the first one is going to be low location and the second one is going to be combo i plus one right this one could be combo i right but since that's already set as location we're getting the next location right so this should be that string and we need to have we're going to have an eye here that's going to cause problems for this j j okay um we're going to make a shortest route equals no no no i'm gonna make it um okay so we've made this key and what we're going to do is we should have all those combinations right we should have all those key values stored up what we need to do is get that value now of that key and we need to add it to [Music] combo um length it's gonna start at zero right so we're gonna say combo length plus equals routes and that key value is going to be key right and that should give us an int that's that's the value of that step right um now this we're going to have to change this to iterate over the length because otherwise we're going to end we're going to have like an overflow as we um j is less than the length of combo minus one all right and then we'll do j so this will still have us iterating over that the difference being that now we have to actually just use that index there okay all right we're doing good now okay so in this line right here we are let's start from the beginning here so we're have all of our list of combinations we are hitting the first one right so let's say that this is our list of combinations we're hitting the first one [Music] we're starting with index 0 and then we're going to go until we hit [Music] so this is length is 3 so we're going to do it while [Music] j is less than 3 minus 1 which is 2 well it's less so only to 1 right so it's only going to be 0 and 1 because what it's doing is it's getting this value in this value making the key pair to get the length and add it and it's doing this one and this one getting the key like and then it's stopping because that's the end right okay uh so now we have combo length we've iterated through everything and what are we going to do if so we're handling something because if we're on our first loop we can't do a shortest route if we're doing like the longest route or something we could start at zero and know that the longest route is always going to be um you know greater than zero um we could also make the shortest route of a billion and expect that it be shorter but we're just gonna make sure that we do it right by saying if um shortest route is equal to negative one which is the only time that should happen is in the event that this is our very first iteration through because that's what we initially set it at um then we're gonna say shortest route equals combo length okay because it's the first one it's gonna be the shortest right um else no that's not that means it's not the first time through now and i could also um set this to zero and say if index if i equals zero you know what let's do that let's do that um can i set things to nil i'm just going to set it to zero um if i equal zero then shortest route equals color length else um if combo length is less than shortest route then shortest route now is going to be set equal to combo length um else we're not going to do anything we don't care if it's longer then we're just going to skip over it and then at the end we're going to return sort of drought all right we're ready to test let's look at our test here um 605. all right let's trust it see how it works passes passes you ready for a really making it work let's see [Music] main dots 141. geez that's real short that doesn't seem let's look at our data set that can't be right that's not my right cert what am i not doing right i mean maybe it is no you know what i'm gonna trust the tests i'm looking at this and saying tambie to whatever but like maybe that's a route that's never going to be taken let's let's trust it trust trust truck 141 submit it's right that's right one gold star we did it i trusted my tests they didn't let me down that makes me happy um i don't think i'm gonna have time for part two um [Music] oh the longest distance the longest distance i'm just going to copy this crap down um [Music] i'm going to do the same time so what i'm doing here is we're still gonna do the same stuff right um we're still gonna on the first pass set them both the longest route but in this case um you might just be able to crack this longest route then longest route equals combo length um let's see part one part two equals part one data all right i think i can do that right oh i did have two ends here returning two ends no that's not the right function why didn't one of you tell me how dare you um okay so now we're returning two ants for part one returning two ends for part one returning these two ends for part one and we're gonna say format part one one and part two part two all right run it 141 ah let's just type it in seven 36 we did it we did it everybody under two hours well that second part was easier once we had all that first stuff done um alright so let's tidy all this up [Music] up to all these extra spaces all right i feel pretty happy um with all this stuff i'm really happy that um i learned about this uh intertools library that was a good a good find maybe i'll have a time to use it again i'm sure i will at some point um see if i can clean up this test i don't need part two this is i'm just going to comment this out now since it's the wrong data types and stuff um yeah all right so uh thank you all for joining me tonight um i typically uh do this on my lunch break every day um sort of between 11 one ish central time um i should be doing that same time tomorrow if uh like today i get too busy i just i just don't want to try to get to it tonight um just like everybody we all got other things going on um but yeah so thank you for joining um if you have any comments feel free to let me know um usually i post these to youtube afterwards too so you can find it and comment on it there i don't know if you can comment on these streams after they're done i'm new with this all right anyways um everybody take care i'm gonna sign off now and go to bed because it's uh 10 30 and i'm old take care
Info
Channel: misterjacko
Views: 33
Rating: undefined out of 5
Keywords: AdventOfCode, Golang, LearningInPublic, Programming, TDD, twitch
Id: XKx0HNbneOY
Channel Id: undefined
Length: 112min 23sec (6743 seconds)
Published: Wed Nov 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.