 # Advent of Code 2021 – Day 8

Photo by Any Lane from Pexels

I’m participating in the Advent of Code 2021. Here’s my solutions for Day 8 – Seven Segment Search.

## Problem 1

In the Puzzles for Day 8, the wires on our 7-segment displays are malfunctioning and we need to figure out what it’s really saying. For part 1, we’ll figure out how many times digits 1, 4, 7 and 8 are appearing in the output. Our displays are represented by:

``````  0:      1:      2:      3:      4:
aaaa    ....    aaaa    aaaa    ....
b    c  .    c  .    c  .    c  b    c
b    c  .    c  .    c  .    c  b    c
....    ....    dddd    dddd    dddd
e    f  .    f  e    .  .    f  .    f
e    f  .    f  e    .  .    f  .    f
gggg    ....    gggg    gggg    ....
5:      6:      7:      8:      9:
aaaa    aaaa    aaaa    aaaa    aaaa
b    .  b    .  .    c  b    c  b    c
b    .  b    .  .    c  b    c  b    c
dddd    dddd    ....    dddd    dddd
.    f  e    f  .    f  e    f  .    f
.    f  e    f  .    f  e    f  .    f
gggg    gggg    ....    gggg    gggg``````

and our input comes in the format of:

``cedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf``

The active wires are in the section before the | and the displays are after the pipe. For part 1, we’re only looking at the second part of each line and picking out the easy to identify numbers. For instance, if there are only two active elements, then it has to be a 1, since 1 is the only digit that is represented by two illuminated elements. Similarly for 4 (4 elements), 7 (3 eleements) and 8 (all elements).

### Solution

All my solutions are written in C#. You can find all my solutions in my Git repo.

Part 1 is straightforward. We iterate through the input and look at the active element count for each output. If it’s 2, 3, 4 or 7 characters long, it represents 1, 7, 4 and 8 respectively. We count the total outputs for each, and add them together to get our solution. A simple switch/case statement block seemed to be a good choice for this one.

``````Console.WriteLine("Advent of Code 2021");
Console.WriteLine("Day 8 - Puzzle 1");
//Solution logic goes here
var counts = new Dictionary<int, int>();
foreach (var line in lines)
{
var split = line.Split('|');
var wires = split.Split(' ');
var outputs = split.Split(' ');
//focus on easy digits
foreach (var output in outputs)
{
switch (output.Length)
{
case 2:
//can only be a 1
if (counts.ContainsKey(1))
{
counts++;
}
else
{
counts = 1;
}
break;
case 3:
//can only be a 7
if (counts.ContainsKey(7))
{
counts++;
}
else
{
counts = 1;
}
break;
case 4:
//can only be a 4
if (counts.ContainsKey(4))
{
counts++;
}
else
{
counts = 1;
}
break;
case 5:
break;
case 6:
break;
case 7:
//can only be an 8
if (counts.ContainsKey(8))
{
counts++;
}
else
{
counts = 1;
}
break;
default:
//should not get here
break;
}
}
}
Console.WriteLine(\$"Total times we see 1, 4, 7 or 8: {counts + counts + counts + counts }");

//Stop and wait for enter before exiting

## Problem 2

The second half of the puzzle is the hard one. We need to take the input data and what we learned from the outputs and then use that to deduce all the other numbers. In order to do that, we’ll have to analyze what is learned from part 1 in order to determine what letters actually represent what elements of the display.

### Solution

For this one, it was a matter of figuring out what numbers we could identify based on matches to known numbers. The first thing I did was to sort the character strings by letter. This would make it easy to compare/contrast between strings. Next, I re-ordered the input wire array from shortest length to longest length. That also made it easier for comparing one array element to another.

There are 3 numbers that have 5 segments (2, 3 & 5) and 3 numbers that have 6 segments (0, 6 & 9). I looked at the 5 segment numbers first.

• 3 is the only one that matches both segments of 1, so that was easy.
• 5 was the next easiest, as of the remaining two options, it matches both of the segments of a 4 that aren’t a match to a 1 (middle bar and top left)
• That left 2 as the remaining option for a 5 segment number

Lastly I looked at the 6 segment numbers.

• 6 was easiest to identify as it would not match both elements of a 1
• 9 was next as it would be the only one to match all 4 segments of a number 4
• That left 0 as the final number.
``````using System.Text;
Console.WriteLine("Day 8 - Puzzle 2");
//Solution logic goes here
var counts = new Dictionary<int, int>();
var total = 0;
foreach (var line in lines)
{
var split = line.Split('|');
var wires = split.Trim().Split(' ');
var outputs = split.Trim().Split(' ');
var conversion = new Dictionary<string, string>();
//sort letters in pattern
for (int i = 0; i < 10; i++)
{
wires[i] = String.Concat(wires[i].OrderBy(x => x));
}
for (int j = 0; j < 4; j++)
{
outputs[j] = String.Concat(outputs[j].OrderBy(x => x));
}
//Start identifying segments
Array.Sort(wires, (x, y) => x.Length.CompareTo(y.Length));
conversion["1"] = wires;
conversion["4"] = wires;
conversion["7"] = wires;
conversion["8"] = wires;

var fiveSegs = new List<string>() { wires, wires, wires };
//pick 3 - it's the only 5 count segment with both elements of 1
if (wires.Contains(wires.Substring(0, 1)) && wires.Contains(wires.Substring(1, 1)) && fiveSegs.Contains(wires))
{
conversion["3"] = wires;
fiveSegs.Remove(wires);
}
else if (wires.Contains(wires.Substring(0, 1)) && wires.Contains(wires.Substring(1, 1)) && fiveSegs.Contains(wires))
{
conversion["3"] = wires;
fiveSegs.Remove(wires);
}
else
{
conversion["3"] = wires;
fiveSegs.Remove(wires);
}
//pick 5 - it's the only 5 count segment that will have both the middelAndTopLeft values
//First, grab the two segments in a 4 that aren't in a 1
var middleAndTopLeft = wires.Except(wires).ToList();
if (wires.Contains(middleAndTopLeft) && wires.Contains(middleAndTopLeft))
{
conversion["5"] = wires;
fiveSegs.Remove(wires);
}
else if (wires.Contains(middleAndTopLeft) && wires.Contains(middleAndTopLeft))
{
conversion["5"] = wires;
fiveSegs.Remove(wires);
}
else
{
conversion["5"] = wires;
fiveSegs.Remove(wires);
}
//pick 2 - it's what's left of the 5 segment numbers
conversion["2"] = fiveSegs.First();
var sixSegs = new List<string>() { wires, wires, wires };
//pick 6 - it won't match both elements of 1
if (wires.Except(wires).Count() == 5)
{
conversion["6"] = wires;
sixSegs.Remove(wires);
}
else if (wires.Except(wires).Count() == 5)
{
conversion["6"] = wires;
sixSegs.Remove(wires);
}
else
{
conversion["6"] = wires;
sixSegs.Remove(wires);
}
//pick 9 - it will match all elements of 4
if (wires.Except(wires).Count() == 2 && sixSegs.Contains(wires))
{
conversion["9"] = wires;
sixSegs.Remove(wires);
}
else if (wires.Except(wires).Count() == 2 && sixSegs.Contains(wires))
{
conversion["9"] = wires;
sixSegs.Remove(wires);
}
else
{
conversion["9"] = wires;
sixSegs.Remove(wires);
}
//pick 0 - it's what's left
conversion["0"] = sixSegs.First();
for (var z = 0; z < 4; z++)
{
if (conversion.ContainsValue(outputs[z]))
{