Advent of Code, Day 8

No update for the past week. It was a slow one, marred by low motivation and energy. I am hoping this week is stronger. In the meantime...

I have found motivation in one new "project" that started this week: attempting to complete the entirety of this year's Advent of Code. I've taken on this challenge for three reasons:

  • refresh my Python skills 🐍
  • improve my problem-solving and algorithm-building abilities 🧮
  • and have a little fun! 🎄

I'll try to discuss my solutions here every day, if possible. I'm nowhere near fast enough to try to compete for the leaderboard; this is all learning and enjoying the challenge of solving challenges with code.

Today's challenge is here. While the first part was quick and easy, the second part proved to be much more tricky. Essentially, given a sequence of letters (input) that correspond to different segments of a seven-segment display (and are not in order), I had to determine which letter went to which segment in order to decode the four-digit output for each input.

Each entry consists of ten unique signal patterns, a | delimiter, and finally the four digit output value. Within an entry, the same wire/segment connections are used (but you don't know what the connections actually are). The unique signal patterns correspond to the ten different ways the submarine tries to render a digit using the current wire/segment connections.

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

Fortunately, there was enough context to make this a logic puzzle. I came up with a reasonably solid method for decoding the input this morning, but settled on a different sequence of steps after I started coding.

My solution

  • First, I separated and alphabetically sorted each input and output string, and sorted the input by string length.
  • Next, I started comparing some of the strings to find common and unique letters, which corresponded to the common and unique segments of certain numbers on the display. For example, after figuring out which six-letter string represented '9', I determined which letter was missing from that string. That missing letter corresponded to the bottom left segment, which I arbitrarily designated s5.
  • By using the strings representing '0', '1', '4', '6', '7', and '9', I was able to definitively match a letter to each segment. After that, I used those segment characters to build a dictionary for decoding the output:

decoder_dict = {'0': 'abdefg', '1': 'be', ...}

  • A quick loop through each of the four strings of the output and a type conversion later, and I had a list of integers for each of the outputs, and quickly summed them all up to solve the puzzle.

Take-aways

I think what I did well in today's challenge was to break a seemingly big problem down into manageable chunks. While I bet there are faster ways to arrive at the solution than the one I took, I wanted to make sure my logic was sound after each step was coded. I also spent time diagramming and checking my logic on paper both before and during the coding process to make sure I stayed on the right track.

This approach, along with plenty of print statements for debugging, helped me squash the bugs I encountered along the way. I didn't run into any major roadblocks, and it took me about 6 hours to solve from reading the problem to entering the correct solution. I'm pleased that I was able to finish and earn my second gold star for the day!

See my solution code in my GitHub repository. 🔥

So far: 8 days, 16 gold stars (2 stars possible each day)!

13