constants, enumerable, brackets - Advent of Code 2021 - Day 10 with Ruby

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up welcome back this is going to be day 10 for the advent of code we are getting there we're getting close so this one is all about syntax syntax scoring this is kind of like one of those bracket puzzles where you get a bunch of opening or closing parentheses square bracket curly bracket or angle bracket and then you have to figure out like oh is it correct or is it incorrect and so there's two different kinds of so this is kind of what the input is going to look like and in the problem lines are either incomplete or they are corrupted so if they are corrupted that means that they have a closing bracket in the wrong place if they are incomplete they just don't finish the whole sequence and so um it talks about these legal chunks versus illegal chunks but an example of a corrupted chunk is here where it starts with an open paren but it closes with a square bracket and so yeah let's jump into it and start trying to implement this so we're gonna have day 10 and in here i'm going to call it um sub system dot rb that's just kind of like part of the prompt day 10 spec rb and in here we will require relative okay so the first thing i want to do is actually write a method or like write a class that represents this line of code and it's going to tell us things like or it'll it'll help us figure out is there an illegal character what uh is it incomplete or corrupted et cetera et cetera et cetera so let's make a new let's like say our spec dot describe line and we're going to say it like um finds the illegal character and we can see something like line is described dot new and we're going to pass it a line and the line in this case could be it could be simple yeah let's make it let's just make our own so we're going to start with angle bracket paren close bracket okay so then we want to close it with we want to close it with like the wrong thing so we're going to close it with a yeah let's just close this with a paren right here so we're going to expect that line dot illegal character to equal um the close paren right because that this is like the first occurrence of some like invalid thing like the first invalid bracket uh okay we don't actually have a line yet so we'll go to like day 10 and inside of here we'll say class line and this is going to be our test run it again wrong number of arguments given for our initialize method so we're going to have our input at input is input i'm going to split it here dot chars and then a legal character is a method we haven't defined yet def illegal character and the way that we're going to do this is using a stack so in the last thing we use the queue to do breadth first search like yesterday whatever day day 9 but for day 10 we're going to use a queue no we're going to use a stack we're going to use a stack so every time we encounter an opening thing we're going to push onto the stack anytime we encounter a closing thing we're going to pop from the stack and compare to see if they match so that is this is like a common practice for doing these bracket problems a stack is a data structure that is sort of like the stack of dishes that you would see at a buffet where like when you pull a dish off the top as you go to like serve yourself that's called popping and when you sort of have like a huge stack of clean dishes and you push them in through the top that is pushing so here we're going to say stack and our stack is going to be something like this so we're going to go through each of the characters so we'll say like input each do char and then if the character is closing so i guess we want to say like we want to have some sort of mapping for each character so yeah let's let's use a bracket map is going to be a hash where we have sort of all the opening ones and what their closing equivalent is so we're going to have like a square bracket one and we'll have a curly bracket one and we'll have a angle bracket one uh i think that's it there's only like four i think in the problem so if um so if it's an opening bracket so if bracket thing keys that include char um then we want to push them onto the stack stack push uh otherwise we want to pop from the stack so or look at the last one i don't know whatever so stack dot pop um so last char is this thing so if uh and then we have to compare like was the one that was on the stack the last opening one is that a match for the closing one so if the um if the bracket if the bracket map for last char equals um char then we're good otherwise like if i guess if this is not good then this is what we return char that'll be our legal character so let's run this and see undefined method input okay we probably don't have a reader for that input okay that works all right so let's let's do another line here um so this is like valid a valid close a valid close and then we'll just make it another square bracket and then expect this is going to give us a square bracket back cool so this is finding finding illegal characters now um and all right so what is so some are incomplete some are corrupted find and discard the corrupted lines first a corrupted line is where a chunk closes with the wrong character some of the lines aren't corrupted they're just incomplete you can ignore these for now so stop at the first incorrect closing character and then did you know that they have points so the first illegal character on the line and look it up in the following table so this gives us sort of like a mapping between the illegal character and some point value and then um we multiply the characters we found by the point value all right so let's just let's let's see like the points or something like i don't know we'll make a method here points and then it's going to be our point maybe yeah and then we're going to have some mapping like points mapping that's going to be this sort of thing that they gave us okay right so then the point is going to be the illegal character um at the illegal character okay so then i guess like let's see let's like make sure that we have the points right too so expect the line dot point to equal and the closing bracket is three and this one is going to have the value point of what square bracket is 57 and passes okay i should probably break them up and have like different it statements or whatever different it blocks for those but whatever this gets us close enough and then what we want to do at the end is so for the total error for the total syntax error score for this file you just add up all of the points for all the different lines um so if file is um dollar zero then we wanna file that readlines arg v dot first dot map um or i guess.chomp sure no map chomp uh dot map into line dot new yeah for the for that line right and that gives us back lines and then we want to just sum up the lines points um lines dot map um the point for that line dot inject i'm realizing as i'm going through this that we didn't actually uh we did not actually um exclude any of the incomplete we only were only considering the um the yeah like our code right now doesn't care if it's incomplete or corrupt it just like does all the stuff um example input and the example input here is this thing and okay so then uh let's see ruby day 10 subsystem.rb day 10 input and we got nothing back p p lines what okay read lines day 10 oh example input okay so we got the lines back inject undefined method plus for nil class okay so i want this i think to be like or zero maybe for now okay was that right two thousand twenty six thousand three ninety seven twenty six thousand three ninety seven okay so then we can take our example input or like our our actual input and add that here and then we run this against our actual input and 462 000 something something 462 thousand where is part one yes okay so that is the answer to part one so we're able to find the illegal character find the points for the illegal character add them up and return it so that's kind of cool now it says now discard the corrupted lines the rel the remaining lines are incomplete so incomplete lines don't have any correct characters at the end and so like this is this is like a totally valid thing it's not corrupt it doesn't the only the only issue is that it's missing the uh the ending and so what we want to do is figure out a way to sort of like fix a line that is not illegal it's just incomplete so i guess like the other thing we can do here is say like corrupted and corrupted is gonna say like if or like bang bang a legal character like if there is an illegal character then the line is corrupted um and so i guess like if corrupted the points are that uh else zero end i don't know there's probably yeah this that seems like a better a better way to go about doing part one but um all right so now let's find a way to figure out how to fix these um so let's look at our spec here and let's say it knows how to fix the end i don't know and maybe we say we want to create some sort of like incomplete line here so um if we just end it right there then we can say like expect line dot fix to equal and we want this to be like an array of characters that would close out the line so it's going to be closings curly then it's going to be closing square then it's going to be closing paren then it's going to be closing angle bracket so we need to write this method fix and fix will figure that stuff out so fix is going to be okay so how do we figure out what is leftover i guess we want to keep track of this illegal character like the stack here at stack stack stack um because at the end yeah at the end of calculating the illegal character we should know what the stack is and then if we reverse the stack and map it over our bracket mapping we should get this list so if there is um if it's not corrupted and when we call corrupted that should fire a legal character populate our stack so if it's not corrupted then we want to take at stack dot reverse dot map onto bracket map um and i don't know let's see okay we've got nil because why because it is corrupted wait um okay so let's see okay corrupted corrupted true what a legal char illegal char oh illegal char is returning a whole bunch of things so this at the end of this after we're done iterating we want to return nil all right let's see all right corrupted false there we go okay at stack dot re or like what is the stack right now so that's the stack that's what we like sort of worked our way through while we were trying to find the illegal character we didn't actually find one and so now the fix is going to be basically all of this stuff but just upside down right so we want to say stack.reverse.map okay let's just see okay we got the right answer that's super cool all right so take out buy bug um and then run this and we should just get okay so we got we got that we have at least we're able to like fix a broken line i mean we should probably test it with some more um uh examples i don't know let's actually just mess with this thing like yeah like i think we can make it more complex and it should still give us the same answer cool so that's um that's working all right now there is another tricky thing to this so there's another mapping of other points did you know autocomplete also has a thing blah blah blah it's true character by character so the last completion would go as follows start with a total score of zero multiply the total score by 5 then add the value of the square bracket to get a new total score of 2. multiply this total score by 5 to get 10 and then add the value of that okay so we kind of we need this mapping again um and we need like another points or something like def 0.2 i don't know uh and then we also need like points to okay so inside of point two we want to iterate over the fix fix the each or fix that map um let's just keep it easy so score is zero we're going to return the score fix that each do char and then points to at char is some value so we want to do score times equals five and then score plus equals that thing i think right so we start with a total score of zero got it multiplied the total score by five got it and then add the value of the thing okay got it all right let's just see if we got the right points expect line up point point two to equal what so square bracket is or curly bracket is three so i think we need three plus um oh gosh this is gonna be yeah this is gonna be super annoying uh does it have an example for us okay so if that's the fix all right so this is this is the line so let's just take this line so um then we expect line.2 to equal something this thing i think is that right oh my gosh wow okay it's so fun when it just i mean i don't know maybe it's like the i feel like sometimes when that happens the test might be failing i don't know like uh let's just gosh okay yeah um okay so let's say that's points that's the second points okay autocomplete tools are an odd bunch the winner is found by sorting all of the scores and then taking the middle score there will always be an odd number of scores to consider in this case the middle score is blah holy moly okay so now what we need to do is um we need to map over all of our lines and get the new point two um and then this is going to give us like and then dot sort sorted lines and then we need to say puts sorted lines at sorted lines that length divided by two uh i don't know let's see against the example input what do we get we get undefined method each for nil class in point two point two undefined method each for nil class so fix is nil oh right so we want to exclude those that are um corrupted so lines dot select um where it's not uh corrupted and then map it i don't know let's see okay is this two eight eight nine five seven something two eight eight nine five seven ah boom nice so that is against the example and then if we run it against our own input holy moly gosh 309 boom boom boom boom boom that is the puzzle answer okay so all right so ah this bracket pairing bracket pairing problems are fun uh stacks finding illegal characters are they corrupted are they incomplete et cetera et cetera et cetera um yeah was there what did we what do we go through in here that was that may have been interesting ah so i'm i'm calling these points to points bracket map these are all in caps meaning that they are constants inside of the class it's like a really convenient way to i don't know hard code something that you're not going to mess around with or to like set create some some object or some value or some representation that you want to reference and you would only change it if you were like changing the configuration of the entire system so if we came back later and we're like oh actually our points are blah we can update this to reflect that um and then yeah i think kind of walking through this entire thing was pretty straightforward i guess like down here this gets a little bit gnarly this is just a bunch of like um array manipulation or string manipulation input manipulation which is another big part of advent of code and ruby is really really good at it but i would recommend for sure learning select map inject like how to deal with all of these different sort of just innumerable methods that give you results back or collect results or filter results or find results they can be super super handy so yeah just in this alone we're mapping twice we're selecting we're mapping again we're sorting like all of these are enumerable methods if you haven't seen ruby's innumerable module enumerable ruby's enumerable module is like super super powerful look at all these different methods so like this is all a bunch of different things that you can do on objects that are enumerable um oh yeah right all in any chain chunk count we used each cons in day one um yeah we've used find we've used find index uh tons just there's just like so many um yeah include we used include um there's lots and lots of different methods here if you're if you're trying to like master ruby one thing that can be really helpful is instead of always just reaching for each or um just map like kind of coming in here and trying to see are there is there an enumerable method already for this thing um in fact i don't know about one but i guess in uh in one of the days recently where we had to find to see if there was a if the length of the losing boards for the bingo problem had only one thing in it and i didn't know that this method existed so maybe we'll go back and refactor that but yeah go check out the innumerable module which can be mixed into it's like how you interact with basically like arrays and collections of things and yeah i guess we'll call it there thanks so much for watching and we'll see you in the next one [Music]
Info
Channel: CJ Avilla
Views: 66
Rating: undefined out of 5
Keywords: cjav_dev, web development tutorials, web development for beginners, vim, ruby, rails, brackets, syntax, advent of code, advent of code 2021
Id: DlCXlbVfQn8
Channel Id: undefined
Length: 23min 16sec (1396 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.