Seven Segment Search [Day 08 - Advent of Code 2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right this is oxdf and i'm looking at day eight advent of code 2021 um and i've just been reading through this challenge it's the most complicated by far we've had so we've had yet but um i don't think it's gonna be that complicated to solve so i'm gonna try to explain it um but also i'll have a link to the challenge if you wanna go read it uh in the description below uh basically there's gonna be this concept of an eight segment display or seven seven seven segment display um where basically there's seven lines so in here they're labeled a b c d e f g they can be used to make uh all the numbers zero to nine here's here's the representations um and basically i've got this challenge where i'm going to be given this line here and the first group here this these there's always going to be 10 and they are going to be the letters that that make up each of the 10 digits but the issue i have here is i don't i can't match a b what is a and b up here but i can say this is the only one that's too long and because i know one is too long it's got to be one so e a and b are either are connected to c and f um i don't know which one's which but that's okay um so i'm going to get each of the 10 here and then i'm also in the same line going to get uh four outputs and that's what that's the four character display what the results are um for that line and so um i will get actually what i'm gonna get is a bunch of lines and they're going to look like this and each of them are going to be different so each of them is an independent puzzle on their own um so you can see here that like for example the one must be b and e uh here the one must be c and b or b and c you can you know the order is not relevant here um in this one it's e d e d okay um so i can look i can already feel part two is going to be asking me to solve the challenge and i think it's pretty doable actually but for part one all they want me to do is look at the output values so just the four values after the pipe in each line and count the number of times i see that the codes for one four seven and eight and the reason one four seven eight are special is because they are unique in the number of uh lines they use so like one only uses two if we can look through here so zero uses six lines as does let's see nine uses six lines and six uses six lines uh two three and five use five lines and then the rest of them are unique so one uses two lines four uses four lines seven uses three lines and eight uses all seven lines um so so the one the four the seven and the eight are are unique as soon as i see one of their length i know exactly uh which one it must be um so it basically wants me to count how many times in the output do i see those values all right i'm over here in my linux vm uh i've got my little stub started here all i've got so far is that just it's going to open up the file i give it it's going to read in the lines it's going to apply strip to each of those lines so that i don't have to worry about white space i don't even know if i need it but it's just easier to put it there and then let's get started so um we need to keep a total count so um we'll do count equals zero and then we can do four line in lines um and for each line we're going to split it on let's take a look at the example here for each line we're going to split it on pipe so we'll do let's see digits and uh and display equals split equals line dot split on type that then for now we're going to almost certainly come back and process digits in part two but for now we don't need that we can just process uh part one so what we want to do is loop over so we'll say started let's start with this comprehension x for x in this dot split so that's going to give this right now will give me an array um each of the four long with the four strings in it um but i don't actually want x i want to do one there and i'm gonna only get it if len of x so length of the string is in two for the one three for the seven four for the four or seven for the eight um and so then for any any string that is of the length of one of those four things it's going to put a one it's gonna put nothing it's gonna put nothing otherwise so now i can just sum that up and do count plus equals sum and i think i can i'm basically done let's give it a try part one [Music] run this and see where we might have screwed it up python day 8 08 sample 26 i believe that was what i had in the data here 26. let's go back and try it with the input 261 here 261 submit and we got a gold star all right and we're on to part two it's what we expected it to be uh so through a little deduction you should now be able to determine the remaining digits through the example above careful analysis blah blah blah digits and four digits would decode to you to code each of them and then sum them up to get a result okay um we can do this so see um i do [Music] i think the easiest way to show this um grab grab the back up here and grab these work out the logic here okay um actually i'll go up here and work next to it i guess uh so to start there's four we can identify instantly right the one len is two um four is four seven when is three [Music] and eight then is seven so those four we get right away the other six fall into two groups um i think i said this i talked about this earlier but you know zero has one is six sides um two is seven is five sides three is five sides five is five sides the two three and five all have the same number and then 6 0 and 9 are all missing one side so how can we figure out what they are well um we can start to look at what we know and so for example when i look at it was 0 6 and 9 6 is the only one that won't so i know the two objects that are in one they will those two have to be in zero and have to be a nine but they won't be in six so i can say six is len is six and uh one not in what hopefully that makes sense um so then i can say for nine um nine actually has a four in it so b d c and f right there um six doesn't have four in it but it doesn't matter if we don't know that one already and zero doesn't have four in it so nine can be len is six and four in um spaces here then i can say you know zero is when is six and not other two um and so that leaves the next ones and we can do the same kind of thing i'm sure let's see um so two three and five three is gonna be the only one that has one in it when is let's type that uh five and [Music] one in uh when i say one in i mean one you know the the two sides of one i think if one is a set of two objects um that is going to be a subset of three where it will not be a subset of two or five um so let's see two is we know we know every almost all the other ones by now so let's see what part we care about um who has looking out for something um difference here is going to be two sides or there's nothing that goes down the whole way uh okay five is is a subset of nine in nine and then two then when is five other that'll work so that can be our logic for how we figure out what to go on um so now we just need to implement that so let's go up here and start coding again um i'm going to just leave that that was quick enough that loop was quick enough that we can just leave it and we're following part one and do another loop so we'll do four line in lines um and so we'll do the same thing digits display and now this time we're gonna we're gonna want to process first we're going to work with digits so we'll say digits we overwrite digits again equals um when we run digits there we're going to have a string that is space separated we do so digits right now is going to look like it's going to be this string right here so i want to uh digits.split on the space that'll give me an array and i actually want to create a set and the reason i'm going to use a set oh no i don't want to say here i want to use so i haven't i'll have an array so i'll put the string be string c e f b e g a d blah blah um so now i'm going to loop over these i'm going to create an array i'm going to create a set for each one the reason i want i want to sets is going to make this very easy to work with because now all of a sudden order won't matter and i can easily do things like is sub sub is subset and things like that so um so we'll do our normal list comprehension like x for x in digits dot split um and then i'm just going to do set of x and now my string of uh this will be a list of each of the things um we will also go ahead and create a result equals that so now we can start to set things in here um let's see so we can do this is where we get to our rules okay so we do x for x in digits if when x equals two and that's going to only this will be i think the easiest way to like remove this from the that'll give us so if we then we know there won't be one of those we can put a zero on there to get it off um now we have the set so we can say result sub equals reasonable so we can do the same thing for four seven and eight case face paste won't seven actually i didn't mean to do three there i think i meant to do three here four here and eight will be all seven okay so now i've got the first uh i've got those all set and now i can start to do the rest so let's see um i want to go over here the next one is going to be six is going to be the len is six and not one in it so let's see we'll do x for x in digits if when x equals six and um x dot uh let's see result of one uh is the website not in not is subset x i think that's gonna work and we can do a result of six equals into zero all right now we'll do nine um the anchors here um so this is nine nine is going to be still length six and this time we want to put a four in it the result so four is a subset of x see what we want there and then um result of what's the last one of the zero equals x for x in digits if when x equals n x equals 6 and see how we say this we just do x not in result should work see what's our next one uh three will be land is five and one is in result three equals textbooks and digits and [Music] x okay up next we will do five five is minus five and we'll end in nine [Music] this time we're going to x is a subset of results are we doing this right the other way the 1 is a subset of 3 that's right saying nine is a subset of four that's not right i want to do x that is no that's right that's right we're saying the thing we're looking for is a subset of results before no and the thing we're looking for that we just actually i think we can do superset or oh it was right before i'm confusing myself now um four is a subset of the thing we're looking for in this case we're saying five is a subset of nine okay that's good um i'm sure there's some there's got to be an error in here somewhere but we will uh cross that bridge we get to it uh two we can prop actually just say equals x for x digits if x naught so with that we should have all of the uh entire uh set of digits defined let's test this do a pdb here take a look and see how it looks after one yeah we got here that has no object is subset set yeah that makes sense what line are we on line oh yeah 26. this subsets it okay let's try that again okay you can't do that um okay so we'll do um can't say x okay or if we need to think now let's move our break point up and i will shoot this with so i can say x for x digits if when six three yep okay and what i can't do is say x i say x not equal results fix b what am i looking at uh not equal result sub six do this yep can't say in result okay so we'll do it slightly longer um x not equal subs or default of nine and we need to fix this down here too um maybe we'll just do the exact same thing when x equals 5 and x 9 not equal result 3 full result sub 5. see if that works move this back where it was run again okay so we're here what result looks like one as it has two four is four seven has is looking good like we're solving it okay i'm happy with that um so now we have results now we need now we need to move on to the other side so uh [Music] eisp we're going to reset this to a let's see what are we going to do probably need sets as well on this we're going to want four sets uh equals the isp.split just like before um and then we will do same thing we did before set x for x split now we're now disc is going to be the force so we want them in order and they're going to be multiplied by 10. so now we will loop over so we'll call let's see we can do let's do it so we're going to do x for i comma x and enumerate this we're going to get we're not going to make this x we're going to make this we're going to take x we're going to look up the value in we really should be doing this the other way um ah i hate having to change it all right now but what i really want is the result the key to be [Music] i haven't saved yet um so i've got this result right what i want to be able to do is say if i say result of 1 it gives me that if i can convert that to a string by the way dot join does that work beautiful but what i want to be able to do okay we're going to change around up here real quick we're going to say result um dot join and there there well what i'm doing basically is i'm going to take the one result make it into a string and use it as the key um i'll do equals four equals seven equals eight come here and we're going to actually watch this we can do this with some dim tricks i'm going to ctrl v right again do control b visual block move down and delete that then i can control the move down and i will shift i to insert dot dot join that and hit escape escape and it joins it puts it in all of them come down here [Music] if b again capital i got that that that's looking better do the same thing for these um beat these wait then here this shift eye dot coin [Music] didn't even i thought he was gonna write on the empty row but it didn't um listen i lost my number six nine zero three five two nine zero [Music] [Music] rules to figure out exactly what this one looks like um that is length equals five and one is not in it a one is in it so that's three and this one [Music] is length equals five nine is a subset or it's a subset of nine so that's going to be five and we'll have two this must be two okay so what basically we've done now is we've made it so that we can give it the code and then it will return back the number which is what i want um i want result of dot join x like that and i want it to be times oh my i so because i need that you know the first one in there is going to be actually the thousands place and then there's the hundreds the tens and the zeros so i'm going to have to raise this to [Music] 10 [Music] i'll multiply this by 10 10 to the power of um and then going to be 3 minus i sum of all that total and then i will need total equals zero i'll add it total plus oh this is a messy loop this is a complicated one um for sure there's about a five percent chance this works on the first run [Music] oh uh line 28 where are we that doesn't close at all that here try that again 26. here one what does that mean here one what is this oh his result oh because oh no dude this kills my whole method um because now shoot so the problem i'm having here is i've changed all the how i'm sorry i'm now using these these things as the key but i can't because that now this is no longer right let's undo a lot let's go back to where we were okay um let's think about this way to organize this um i could create a second leave it like this i guess i can come down here and say x say results index of x really be of not join x will that work um let's see what do i have i have a result i should be happy with that right that looks good um i'll note the result is not ordered can i can i do what if instead of making a dick equals we'll just call this uh for x we're in change we just make the result be have to have 10 things now when we're putting things in here it's no longer it's and a it's a um it's a list and not an array might be i can call index see if this works [Music] [Music] efgd [Music] oh because they're not joined i did enjoy i'm not um well that's not what i expected how did i get oh no that is what i expected that's that's actually good what i want um 39 30 83 94. is that one of my oh no i'm on here but 8394 that's what that one is sweet that looks good and i got a new idea for how to handle the rest of this i can get rid of the i can get rid of the enumerate because i'm not going to use math i'm going to use strings results of index there and i'll do string like that then i'll do a join make it into one string then i will make it hint and total plus equals that i'm due to the point that i got rid of total i bet i did total there's been a long one today 69 26 was not right um do a print we can see the now we'll see the um numbers going in 83 94 97 81 39 11 there that one is wrong wow that one how did i miss the one um that is uh all right we're gonna troubleshoot so we'll say grab that one that's wrong well i'm pretty surprised that happened up here we'll do if in line we will debug this thing okay so line equals that correct answer will be 45 48. how does that possibly work four oh yeah okay 45 48. i got 42.48 somehow i messed up the five and the two only in this case though um next that one three is good okay so what i'm looking for for five is that it's what's my rule um subset of nine what happens if i last won't get more than one here wrong and i just got lucky a lot of times there is two that are returning did i do my math did i do my logic wrong plan is five and it is in nine two is not nine you should not be in five should uh uh hoho is three is also in nine isn't it that's the problem i bet um so it needs to be is n9 and x not equal result sub three comment this out for a second to see if we fixed it six one two two nine sounds about right run it with our other input another gold star awesome um so hopefully they were able to follow that but the i'll do my do a kind of a wrap-up summary at the end because i kind of get going for part one it was really simple i'm basically just taking this back half um i'm going to loop over i'm going to split it on space i'm going to loop over all of them i'm going to have the value 1 only when the length is one of these four values which i'm looking for then i can sum up the results which would be just a count of the number there and i will get add that to account and print it part two i had to use logic to figure out what the rule come up with rules for what was going to happen so um start with some processing to handle these beginning ones and to get make a set each one of them and then i can quickly get the first four because they're all unique sizes and then i just needed to use what i knew about those four in addition to the sizes to map out these next pairs of three once i had those i could get the display side and basically convert that into and get the display side figure out what numbers those needed to be and the trick i used here was i used to create an array of the four so i had the four displays created a string instance for each of them i joined them in the turn back into end alternatively what i was thinking originally was i would multiply by 10 to the i minus three or three minus i so it went reverse order but anyway either way that could have worked and uh it worked for me there so uh thanks so much for sticking around to the end you are awesome for doing so and i will talk to you next time thanks [Music] you
Info
Channel: 0xdf
Views: 117
Rating: undefined out of 5
Keywords:
Id: AcGtBPTMgp4
Channel Id: undefined
Length: 40min 29sec (2429 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.