Robert Mion

Posted on

# Seven Segment Search

## The task at hand

### Solve for X where

`X = the sum of several decoded 4-digit numbers`

### Input is

• A multi-line string
• Each line contains characters: `abcdefg` and `|`

### It represents

• Encryptions of the 10 digits: `0-9`
• A secret number between `123-9876`

## Returning to my favorite puzzle this year

• I solved both parts of this challenge on the day it was released
• I thoroughly enjoyed analyzing each digit enough to discover at least one way to identify each of the seven sections
• At the time, I was just happy enough to get 2 stars
• This time around, I was excited to review my code and create another simulator

## Part 1: Counting the unique digits

``````Split the input into lines
For each line
Split the line at the | character
Keep the second string (containing four space-separated strings
Split the second string at the ' ' character, into an array of four strings
For each string in the array
Increment an accumulating value - starting from 0 - if the string's length is any of: 2, 3, 4, 7
Return the accumulated value
For each value
Increment an accumulating value - starting from 0
Return the accumulated value
``````

## Part 2: Decrypting each digit

``````Split the input into lines
For each line
Split the line at the | character and store each one in a variable: digits and code
With the string of digits, perform the steps below and save the result in a variable, mappings:
Identify the three horizontal bar characters by filtering all digit strings for the ones with length of five - 2, 3 and 5 - then filtering for the characters they share: top, middle and bottom bars
Identify the string that is digit four by filtering all digit strings for the one with length 4
Identify the d segment by filtering the horizontal bars for the single character shared by them and 4: the middle bard
Identify the string that is digit one by filtering all digit strings for the one with length 2
Identify the b segment by filtering 4 for the single character that's in 4 but not in 1 (amended to include d)
Identify the string that is digit seven by filtering all digit strings for the one with length 3
Identify the a segment by filtering 7 for the single character that's in 7 but not in 1
Identify the g segment by finding the character that isn't a or d in the horizontal bars
Identify the string that is digit five by filtering all digit strings for the one with length 5 which also is the only one with the b segment
Identify the c segment by filtering 1 for the single character not shared by 5
Identify the f segment by filtering 1 for the single character that isn't the c segment
Identify the string that is digit eight by filtering all digit strings for the one with length 7
Identify the e segment by filtering 8 for the only character that isn't a,b,c,d,f or g
Return an array of ten arrays, where each array contains the characters denoting each digit
Update code to the result of the following operation:
For each of the four strings in code
Update each string to the result of the following operation:
Find and return the location of the item in mappings that matches these two conditions:
1. The length of the current string is equal to the length of the item in mappings
2. Every character from the item in mappings matches every character in the current string
Combine the location numbers into a string of four numbers
Coerce the string into a number
For each number
Increment an accumulating value - starting from 0
Return the accumulated value
``````

## A fun adventure in cryptography

• "Through a little deduction"...I discovered one way to decrypt each segment that the letters in any line represent
• Through a lot of trial and error...I wrote an algorithm that achieved the same feat
• Returning to this puzzle now offered an equally rewarding opportunity to visualize my algorithm - and make it interactive!

DEV Community

## Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.