2021 advent of code - day 08 solutions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another day of advent of code this one is day eight and this one was a lot of reading so hopefully i'll be able to simplify the problem a little bit for you because i found the problem pretty hard to read and well it wasn't hard to read there was just a lot to keep in your head so anyway let's jump into it okay so the problem that we're going to see today is we're going to have a bunch of these input rows and now this is something that i missed from the reading as well the rows have an extra new line in them in the sample input so this is not actually what the input will look like but it'll look more like something like this put s dot replace and pipe new line with just the pipe i think i think they actually didn't have this extra space there either but double check your actual input versus the sample input and make sure that when you're puzzling out the sample input that you have the actual same same content because otherwise i can throw you off um right but in the input the i'm going to call i'm going to call these rows and i'm going to call other things lines so again real confusing frozen lines uh so this is one row and the row is split by a pipe and on the left hand side it has the input side and on the right hand side it has the output side now on the input side there will be 10 chunks of string and the chunks of string have letters a to g and those represent electrical lines that are going to power parts of a digital clock and in this example they're marked a b c d e f g uh in order but they will not be in order in your puzzle and they will be different per line so whatever the lines are set here is different than this row here is different than this row here and what you'll do is you'll use the inputs to figure out which chunk of characters represents each individual number in in the output lines so you'll be able to use because all 10 of them are specified here and there are some tricks to figure those out but fortunately in part one you don't have to actually figure any of that out and it gives you a hint for starting to figure out part two so in part one it says you can uniquely identify some numbers by the number of lines set and these are the four numbers that are unique so if we look at these numbers here grab one over here you'll see that one only has two lines set or as four lines set seven has three lines set and eight as all of the lines set and so these have a unique number of line set there's no other that has two four uh three or seven those are those are the only ones that are set there uh so that makes it easy to identify these and so it's just kind of a hint there i'm going to write part one out here and then we're going to talk about part two i'm going to kind of show you my code approach we're not going to write that one live because it's a little bit more complicated all right so for line in input s dot split lines uh we need to only look at the output for part one um another easy thing to miss when reading uh output equals line dot split on the pipe character and then parts equals output dot split and we're doing a lot of splitting and we want to count the number of 1 4 7 and 8. so count equals zero and then four part in parts if lan part is in the one four seven and eight have lengths of two four three and seven two three four seven two three four seven uh then count plus equals one and then at the end we print that count and i forgot to write down the expected value so we're gonna have to check that real quick um but we get 26 and i seem to remember that is right a8 part one dot pi yes the expected is 26. cool and you'll see mine looks very similar to that although i actually just have track of all the parts instead of doing it here this is a little bit faster but this was me scrambling to read the problem okay so that's part one in part two we need to figure out what numbers what the numbers are per line and then sum the digits in the output so this is a four digit number and so to do that what i did is i tried to pick out numbers that are unique first what i did is i figured out which ones have length six and which ones have length five and separated those out so nine zero and six have length six and these three have length five so from that you can do a little bit of figuring here let's just clean up extraneous well it's still gonna it's still gonna trigger someone's ocd here so we're just gonna figure this a little bit better oh it's bothering me so i'm just gonna fix it ah this one's messed up too anyway so you can separate the ones out that are length three versus length five and that helps you differentiate a little bit here but then you have to get a little bit clever the next number that i tried to figure out was the number six here and the reason or the the way that i could differentiate it from zero and nine is based on this left hand side here and again we don't know what the left hand side is we just know that this one is missing one and this has both of them fortunately we can overlap it with this over here since we know this has both of the left hand side so if we were to intersect these with one six would only have a partial overlap here that's how i figured out six that gets us six figured out well we have we have these figured out now all right so once we have six figured out um i was looking again for overlaps from ones that we know and if we look at zero and nine the four has a different overlap between these two uh in the nine it will have this in the zero it will not from that we can find either zero or nine once we found zero nine we know we only have one length six left over so we figured out zero then we need to go over here and what i used to find five is i overlapped it with six and it should be the same as five so we should still see the same number there that's how i figured out five so we've got that we're good here and then between three and two we again see this left hand side so we can overlap three with one and figure that one out and then there's only one left over and so that's how i figured that out now of course i did this in code and so it's a little bit more complex than just visualizing it but that's how i went through and figured out each of the numbers now i'm going to show you my code which you know is maybe not the most interesting but goes over the concept in a little bit of detail a8 part two i okay so there's a lot of a lot of code here um and another thing that i noticed uh that i forgot to mention here oh yeah lines aren't always in the same order so even if you see a line like cd cbd gef it might not be in this same order over here so you have to make sure that you sort the characters in each chunk make sure that you're comparing them properly which is uh what i did here i also have this here for some reason oh i know why because at the end i need to figure out the actual digits okay so again we split the line into two parts the start and the end i keep i keep the end parts around because i'm going to use them later to figure out what the actual digit is down here then i get the total set of digits i could have just used the input i didn't need to put the output in here as well after reviewing the problem and learning a little bit more about how it works i sort the internal parts this is a key mistake that i was making early on otherwise sometimes your set will be too big and then from that i basically just build up a mapping where i figure out the length so this is a find the digit that has length two uh this little trick here i did in another video so i'll link that in the description basically it's make sure there's exactly one thing in this right-hand side and then assign it to whatever this is so we do that for our four numbers uh then we figure out whatever the length six things are i'm able to find six based on its overlap with one i'm able to find 9 based on its overlap before then 0 is the last one that's left over that doesn't have any overlap then from the length 5 ones i overlap 5 with 6 and see what it does i overlap 3 with 1 and see what it does and then we have our leftover number once we have that mapping we invert the mapping so i was doing number to string this is now string to number and then from that i can find the four digits multiply them by a power of 10 and then add that to our total so that's that's how i did part two uh but yeah this one's a very a very visual problem so uh i recommend you know trying this out in paints or or figuring that out similar to how i did anyway i'll see you around for the next day
Info
Channel: anthonywritescode
Views: 428
Rating: undefined out of 5
Keywords:
Id: WDFh2jdUYlw
Channel Id: undefined
Length: 10min 2sec (602 seconds)
Published: Wed Dec 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.