Advent of Code 2021 🎄 Day 14 🎄 Golang TDD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] it's me i'm back um i am going to be doing day 14 today of advent of code um trying to get it in on my lunch break uh let's get going myself into the corner and read the prompt let's actually make this bigger so people can see it all right pressure gonna make the submarine stronger [Music] um a template and a list of pair insertion rules all right so it looks like this would be the template and this would be pair insertion rules so a b to c means that when elements a and b are immediately adjacent element c should be inserted between them okay these insertions all happen simultaneously okay so i mean that means that if you've got a b b it's not like you do a c b and then have to deal with a and c and c and b right away that makes sense um c is inserted b h and then we're left with okay so it's basically going to nearly double every time with the exception of like one on the end [Music] after 10 steps it is thousand characters in length okay so this is going to be kind of a pain in the butt right to iterate over um [Music] you gotta count the number of times things occur most common element the least common element to get our answer okay so assuming that this doubles every time um let's look at our puzzle input oh okay so this is already we got here about 5 10 15 20 about 20 in length um so we would double it basically like three more times here to have about six thousand twelve thousand twenty four thousand okay i mean so that's not a lot i suppose to go through twenty four thousand but we're gonna like we're gonna have to do it a ton of times we're going to be making [Music] like a key anyways right where we have or a map anyways i'm guessing where we have the key the value that we're looking at and then the one that we insert um so as we say we were going to do one string we would iterate down the line grab the two um maybe make a new line right we're making new so it would be what it would be it would be a slice right we'd make the new slice we would take the first two characters um and the first one and then like the result here in the second position and then we'd shift down add that first one and then the result and so on and so on until we got to the very end in which case we're always just gonna add you know b at the end nothing's gonna nothing's to get in the way of that so that's what we would do if we had an array i'm wondering and then at the very end we have to go through and count everything um we can do that i guess i'm a little bit concerned about the length of the array and how many how often we'll have to iterate through it [Music] how often we'll have to iterate through it and the counting and then what sort of position we're going to put ourselves in for part two that we're not going to be able to like do this in the same way so what i'm wondering is if i could make a map with the key values being sort of each of the two characters here so nn nc cb right iterate through that map and say for example nn where's that and then see and so what i do with that is i'd say make in a new map write the values nc and cn right so the two values above and below that make those two values with each of the two just sort of like we did with those um those glowfish or whatever those were if you remember where we you know put up a generation and then at zero we split them both to six into eight um i'm thinking we could do that same sort of thing with each of these then we could do that for every turn and then what we would have is really a list that would at most be the length of the instructions with numbers on the end of it um the only problem with that being that we would so i mean so that would be easier to pass around it would be easier to store it would be at the end easier to iterate through we just um well i guess we have to think about how to [Music] count them right because we're sort of double counting i wonder if we had something like ch equals eight right there's eight instances of ch if we could say like keep track of them and to say that that means that there's eight c's and eight h's right um and here say this hh was five right we would say there's five h's and five more h's and then what we could do at the very end is sort of divide that by two um because we know that we're sort of gonna double count everything that's not on the end so n is gonna be counted twice here c is gonna be counted twice and then what we could do is like add an extra n and an extra b we could keep track of the first and the last number because they're always going to be on the end as they get pushed out i think that's gonna be more work but i think that will set us up easier for a potential um weird thing in part two so that's what i'm going to do um let me copy part 13 here um just like the way that we i've been going through the um inputs in doing part 13 so i'm going to go with it and rename to 14. what do we got um input test it's going to be similar here with that sort of starting value and save that test maybe we could do a shorter one um i'm gonna save that for now i haven't made it shorter yet but let's get our input oh i mean that's a pretty that's a pretty big map but whatever it should be easy enough to make it i guess the question is is are we going to want to split these two guys into different parts or are we gonna just want to parse um i think we'll just parse them as we go i mean i guess we could you can make a struct for the key like a vf and make you know the two pairs be vn and nv right maybe we'll do that either way let's close out of these and let's let's do that um we'll just do that to prove here with the shorter one that we can sort of make that make that map okay so let's go into our test let's comment out part one here we do care about load input but we're gonna go get rid of some of this stuff for first let's see okay we don't need to worry about it strings i don't think there's no real strings here the only ins will be ones that we create in our map so i think we can get rid of this um [Music] what we'll make though is a struct that is what they call it poly plan and what it's going to consist of is um the before [Music] what can we call those the two the two pairs um pair one here two um which will both be strings all right let's go look at our tests here um suck all right um [Music] and again we're looking at the test input short so it's just going to be the first two all right so what do we want we want um input and inserts we'll call it okay and input result is just going to be i guess we don't even need an input result do we we'll just um input and we want this to just be the string in and in c b though maybe we should make that an array do we care if it's an array i think it does need to be an array because we need to like you know put the two together um or should the key just be also an array then we don't have to think about it um well we'll see um let's just have it be the string for now all right so that should be our input um [Music] and then we'll have the we don't care about inserts this is going to be the harder one insert result is going to be a map with a string and contained in it is going to be poly plan and it's going to be a list no just one right yeah just one poly plan um all right so what do we got here so the first one is going to be the string is ch and the polyplane is going to be pair 1 is cb pair 2 is going to be bh right right all right and next one will be hh hn right so the first one then the second one and then nh all right why is it mad about this oh because i don't use it um let's use it here okay all right so you know it's gonna fail let's just go and see what the all right so we have poly plans which is going to be a map of um string containing polyplan that's going to be the second thing right so [Music] let's put that there and then the first one is going to be a string which is just the initial map right um and that'll be easy enough i'll just i'll just uh you know save that as line one right um no but i have to do it after but what did we call that um in the test um template all right read the file plan switch no i'm not going to do that this time since it's not like a variable there's only one line and then we start the stuff um all right so data from the danger [Music] so what we'll do is um template well we can do this outside of it data zero right that's a string good uh that's the first thing we're going to return all right so templates out of the way and then we're going to say 4 we're going to iterate with the i and we're going to start on zero one two line two um while line is is less than uh the plane type data oh read that i plus plus okay all right um so you don't need to do any of this stuff anymore um but we do need to transform a little bit what these lines are right so let's look at them make sure that they're all yeah they should all be two characters in one character so let's just kind of do a fast and dirty um so we need to sort of split those out i'm going to split them uh on the separator space right so that's going to have one side of the other side in two parts all right um and we are going to make poly plans e-value line array zero right so that's the key value um [Music] equals poly plans we know bro um pair one equals what's it gonna be i'm just going to call it key split and i'm going to split um okay so now this is just those first two and we're going to make this strings join no that's not gonna work so sprint that'll be um he split zero right so this is the first one um and line array one right um brackets do that split all right so i think this should be key split zero which is the first thing and line array one which is what's on the other side of the arrow so in this case cb and pair two will be line array one on this side and key split one and i think that's it we're gonna make that we're gonna add that to [Music] our template yeah actually i take it back right we we need to make our initial guy right here um so template is going to equal [Music] that and uh we're going to do a just get two right um much um right okay so what we're gonna do is we're gonna make this uh make this map um which is going to be um template map with the value of sprint template i [Music] i need to do those values uh play i and template i plus one uh uh plus plus all right okay so we're making that template map we're turning it let's go back to our test um we're gonna have to make an input and just like all of our maps i'm sure that we're gonna have to comment it out right because um it's really hard to get these uh in order but there's only one two three of them right so maybe we'll be able to handle it all right um all right so and then right is the first one with a value of one uh nc value of one and cb value of one all right let's go ahead and test it again passes oh wow that feels good okay so what do we have we have a map with our values in it um let's go ahead and see if we can take this input result and count sort of the final the final amounts right um so [Music] let's make a test here's a simple one uncomment it we're going to test so this is going to be sort of how we're going to reconstruct our string at the end and what we're going to do is we don't care about the inserts here we do care about the input we care about the input result no we don't care about the input result we know that we're getting that um but what we want is it to be um n is one and there's two c is one b is one right that's what we want the result to be all right because we wanted to show that there's two of those oh we need something else from our input don't we um so we need to keep track of the first um and last character um you know i'm just going to hard code it for now i can i'll add some logic in a bit um let's go ahead and something's in the front door it could be amazon guy all right huh uh in it we're going to be passing that stream map which is going to be a map [Music] of string then we're also going to be passing a um beginning and end string and we will be returning we'll be returning also a map okay so i'm going to make this result map a map of string [Music] and what we're going to do on the first end is um beginning one and one we're just going to pre-populate that with those two right because if we're going to divide it by two we would need to like add those on the end so that the division doesn't it shouldn't be a half right because those will have a sort of a surrounding pair all right so now what we're gonna do is loop through that string map for [Music] string map key okay so what do we need to do we need to um take the key and the value and yes what happened little keys string and we're going to split it on nothing and then we're going to result result map with the value slice 1 or key slice 0 [Music] plus equals value right that's what we got there and then we're going to do the same with key slice one right and that should allow us to return result map that contains oh we need to cut those in half then right um for key value in result map i'm not going to do any of this um can i just do that let's try it right so what i'm doing is i'm trying to go through and divide each of those by two all right let's go ahead and test it oh because i didn't pass in those uh strings um in my tests input and then you know just because i know it um and let's try it again oh it passed all right so it's able to calculate when we give it this map what the number of strings are all right now we need to i think we might be actually ready to um [Music] oh testing part one all right um um we want to get this result here um i think we want to get two more things um out of here i don't care about it here um we do care about it here what it's a good thing to test let's go back to load input make sure that we can do this correctly um we're doing it right here um beginning equals uh template zero right so that should be the beginning character and uh should equal tmpl a template um and template oh i'm sorry about that code size is hard to smooth i'll make it bigger um is that better internet bird um all right let me collapse this down so that we can see more of it um all right uh length template um minus one good and then we're also going to want to return that string beginning all right um now when we do our test we're going to what is this oh we still gotta comment this out all right passes so that's good we're able to detect the beginning of the end here um all right let's go ahead and start working on our part one in earnest okay so part one my keyboard a little bit um what are we going to pass in we're going to pass in a [Music] template map which is a map string int right um what else do we need template map um what do we call these instructions poly plans and it is a map of string [Music] and plan and then we have beginning and string all right that's a lot um we're only going to return mint geez right let's get cracking all right so what do we need to do here um so let's think we need to make a oh a function um all right um [Music] let's see so what are we gonna do um [Music] we need a for loop right um for i equals zero i is less than ten i plus plus all right this is our ten sorry if that was really loud um so this is our 10 times that we're going through the process right um and each time we're going to need to make this something we should probably test let's test it um [Music] let's make a function called it's not really going to be a a function but it's going to be the turn right and into it will pass template map and poly plants you just copy these and uh and it will return just the template map all right all right so what we're going to do is make a new map all right and then four [Music] key [Music] value in range template map what we're going to do is let's collect collapse some of these um we're going to so the key that we have look at this so in this template map right we have basically these um let's look at the test we have uh the individual pairs and how many times they occur what we want to do is take poly plans with that key value right so that's going to be like this one right here right that's what we're looking for um all right so new map paulie plans key pair one right so this is going to equal it gets a little plus equal value right so we're taking we're getting a new map we're placing as the key value that first pair and setting it to the value of the key right because that's the number of times that's going to be created and then we're going to have to do that with um also with pair 2. right and then we're going to do that the whole time and we'll return new map okay so before we get too far let's go ahead and test that um let's comment this comment part one out um okay what did i say call that take turn all right so in this case our input will be all right um our input will be this that's how we care about input inserts um and we're going to look at just that first turn right it's going to do it just once and what we want is all right take put input inserts and we want the result to be let's see so nc and see currently right is one after one turn and see cm is one and b is one you see one ch is one hb we already have hp hb is one and that's what we want the result to be right let's go ahead and test it failed but is it just out of order oh because we only have short um let's do the full test that's what i want pass oh beautiful okay yes so i think we're ready for the final final run right um let's uncomment out this again and foreign um right so where are we on the main okay let's pull this down get a little more visibility all right so for that we're going to um template map is going to okay so this should cycle through all those times and return us with a map at the end and then what we're going to do is total letter equals calc string template map right um i have to pass in the beginning and then right yeah template map beginning end right so now i have the total letters and max equals zero equals the biggest number i can think of that'll fit in this category and then we're going to say you know i don't think we care about the key we don't care what the number is value in range total letters if if value is less than min let's start with greater and we're subtracting them right so return um next minus spin all right let's run it and test it passes did i uncomment that out oh all right so now let's slide this over and see about actually running it um i'm gonna copy this from test all right so let's go ahead and run it go run [Music] go oh it's mad at me wasn't mad at me was it mad at me 93 30 93 calls it so it's about loading the inputs and 38 huh what's what's the deal with this um my problem yeah okay vector enter at the bottom of return all right let's go ahead and try it um two three six zero got it first gold star um [Music] all right here we go part two not nearly strong enough a total of 40 steps should do it all right so that's easy enough um what we need to do is add to our take turn let's make this smaller um [Music] we got to parameterize this beginning and string um [Music] cycles and [Music] that should do it let's go ahead and look at this 10 here test it again hmm did i put that in the wrong place oh down here passes um [Music] i'm gonna get what am i gonna do you
Info
Channel: misterjacko
Views: 17
Rating: undefined out of 5
Keywords: AdventOfCode, Golang, LearningInPublic, Programming, TDD, twitch
Id: V4VEYib4xiQ
Channel Id: undefined
Length: 71min 28sec (4288 seconds)
Published: Tue Dec 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.