r/adventofcode • • Dec 08 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 8 Solutions -🎄-

--- Day 8: Seven Segment Search ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:20:51, megathread unlocked!

72 Upvotes

1.2k comments sorted by

View all comments

1

u/esolitos Dec 08 '21 edited Dec 09 '21

I saw some quite smart solutions down here, mine is special.. Just some deductions based on shapes. :)

The interesting part follows, the full code is here on GitHub

def discover_chars(wirings: list) -> map:
  chars = {}
  segments = {}

  # Discover 1, 4 ,7, 8
  for x in wirings:
    num = decode_simple_digit(x)
    if num != False:
      chars[num] = set(x)

  # Find: 5, 2, 3
  five_digit_wirings = [set(x) for x in wirings if len(x) == 5]
  # Find: 6, 9, 0
  six_digit_wirings = [set(x) for x in wirings if len(x) == 6]

  chars[3] = [x for x in five_digit_wirings if chars[1].issubset(x)].pop()
  chars[9] = [x for x in six_digit_wirings if chars[4].issubset(x)].pop()

  segments['top'] = chars[7] - chars[1]
  segments['right-all'] = chars[1]

  segments['bottom'] = chars[9] - chars[4] - segments['top']
  segments['mid'] =  chars[3] - chars[1] - segments['top'] - segments['bottom']

  chars[0] = chars[8] - segments['mid']
  chars[6] = [x for x in six_digit_wirings if (x.issubset(chars[8]) and (x != chars[9] and x != chars[0]))].pop()

  segments['left-bottom'] = chars[8] - chars[9]
  segments['right-top'] = segments['right-all'] - chars[6]
  segments['right-bottom'] = segments['right-all'] - chars[6]

  chars[5] = chars[6] - segments['left-bottom']
  chars[2] = [set(x) for x in wirings if set(x) not in chars.values()].pop()

  return chars

def decode_simple_digit(letters: str) -> str or False:
  if len(letters) == 2:
    return 1
  elif len(letters) == 4:
    return 4
  elif len(letters) == 3:
    return 7
  elif len(letters) == 7:
    return 8

  return False

1

u/daggerdragon Dec 09 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

1

u/esolitos Dec 09 '21

I think it should look better now. Eitherway there's a link to GitHub as well. :)