We are given two binary sequences with a length of $7$, representing the $7$ segments in the display. A segment in its off state is represented by a $0$, while the on state is represented by a $1$.
When “overlaying” the two seven-segment displays, if a segment is lit, we want to include it in our final digit. This is effectively performing a bitwise OR operation — if a bit is set in either of the two sequences, then we want to light it up. If neither are lit up, then we don’t light it up.
Using the first example,
0111111
OR 1000000
------------
1111111
This allows us to get the overlaid state. Once we have this, we can check against a list of precomputed arrangements for each of the digits $0$ though to $9$. If there is a match, then we return the valid digit that matched.
In these solutions, we use the original string that we are given as input, without needing to convert it to an integer or bitset.
This approach uses integers and bitsets.
Firstly, we perform a bitwise OR of the two inputs $a$ and $b$ to “overlay” the seven segment displays. Then, we can predefine the digits and store them in an array of bitsets.
Then, we can compare the overlaid seven segment display against these digits $0$ to $9$ by doing a bitwise XOR. Remember that a bitwise XOR of the same number is equal to zero. Formally,
$$ x \oplus x = 0 $$
So, if the XOR of the input and one of the digits is zero (which we can check with .none()
or check equality with $0$) then we can print the answer.