Day 8 - Segment displays

Wow, that's a lot of information.

We are going to have to try to work out how to build a mapping from segment displays back to wires, and it feels a lot like a logic puzzle or a suduko.

But wait, before we get started, what are we actually being asked in part 1?

I've been caught by this before, attempting to solve the asked problem, before realising that the problem can be reduced down to a much smaller problem.

One of the things that we can notice is that for a given digit displayed, there's only a certain number of segments on, as it says, if there's 3 signal wires activated, then there's only 1 possible number that can be displayed, the 7.

I think therefore we don't need to know anything about signals or wires. What we're being asked is to turn the words into frequency counts of the length of words, and then find all the ones that are unique numbers.

That's actually really easy

Great. So in this case, we're counting digital signal lengths, and the digit 1 is just 2 long, the digit 7 is just 3 long, the digit 4 is 4 long and the digit 8 is 8 long. So we can just loop through the example input and see if the total works

Part 2 - deducing the wires

So we knew this was coming, and this feels really hard.

But let's work from the patterns that we know, and see if we can work out some facts. We're going to use the test line below to work this as an exmaple:

be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe

We can deduce a 1 from a 7 from a 4 from an 8. If we take the 1 and the 7, the letter in the 7 that isn't in the 1 must be the top line a. Our 1 in this case is be and our 7 is ebd. Therefore d=>a

Now we also have only 2 letters that can result in either c or f lamps being lit. In our case this is be.

One of those letters is the difference between the 6, 9 and 0. All of 6,9 and 0 have 7 digits lit, but the 6 will be missing either the c or the f.
So we're looking at cfbegad, fbgacbe, and we're missing one. Annoyingly, we're missing the 6 in our test case, so we cant determine this.

Whichever one is missing one of the lines that was in the 1, must be the c.

Further the f is lit in every single digit apart from the 2. If we can find a single 5 digit example that has one letter missing whcih is present everywhere else, from the subset from the 1, than that's probbaly the f. In our case fabcd is missing the e, so e=>f.

Going back to our 1, we now know that e=>f, so therefore b=> c

We can use the missing f in 2 to find a 5 long digit that's missing an f. We don't seem to have one in our data set.

We can use the missing c in 6 to find the 6 long digit thats missing a c. We already knew we were missing a 6 though.

At this point I need a lie down... I am struggling to do this once, the fact that the wires and the lamps are boht named after letters is making life really confusing, and even if I can do this once on paper, I'm not confident that I can do this in code.

The problem here is one that I can recognise, it's a form of constraint solving. If I can encode the constraints, like The one and the seven share 2 in common, and the one differnt maps to a then you can feed the system the input and it will reduce down to only the possibilities that match the rules.

The way to program this is to create a dictionary of "possible values" for each lamp, which starts as "all possible values" and then, for exmaple, take the 2 input one, that makes the 1 light up, and modify the possible values for lamps c and f to be only those inputs. You can also then remove those two values from the other possibilities.