Member-only story
Seven Segment Search: Day 8: Advent of Code 2021 — Python Solution
The day 8 challenge is not that straight. A lengthy question and understanding may take longer for some. This article will try to simplify the question and works toward the solution.

If you are not familiar with Advent of Code, I highly recommend solving it yourself before looking at these solutions.
Understand the problem
This problem focuses on detecting the digit from a set of characters just like Seven-Segment displays. Here is the example of 0-9
digits:

The input divides into two parts, signal patterns and four-digit output value. That means, based on the signal; you need to identify the four-digit number from a given set of characters for each digit in output value. The challenge is that the signal is distorted and cannot determine which character set represents which digit.
One of the inputs from the list of sample inputs, I found a little easy to understand:
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
Part 1
The first part is relatively easy as always; Instead of decoding the random set of characters to digitize, we need to count 1
, 4
, 7
, and 8
. Why these four numbers? Because they have a relatively distinct number of character count.

ans = sum(
len(chars) in (2, 3, 4, 7)
for line in inputs
for chars in line.split('|')[1].strip().split()
)
print(f'Times do digits 1, 4, 7, or 8 appear: {ans}')
That’s it! Simple and straight.
Part 2
In part 2, you must decode all the four-digit output values from the distorted input signals. Let’s list down what all information we have, or we can analyze looking at the existing data:
- We can decode
1
,4
,7
, and8
digits by looking at the length of the characters. The same…