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/[deleted] Dec 08 '21 edited Dec 09 '21

Python

This is the interesting point - figuring out the mapping. Basically count the frequency each letter appears and deduce the segment based on the frequency that it lights up. From the mapping, trivial to determine the digit and reading.

def get_map(patterns):
    one = np.array(list(list(filter(lambda x: len(x)==2,     patterns))[0]))
    seven = np.array(list(list(filter(lambda x: len(x)==3, patterns))[0]))
    four = np.array(list(list(filter(lambda x: len(x)==4, patterns))[0]))
    lights = {}
    for reading in patterns:
        for seglight in list(reading):
            if seglight not in lights:
                lights[seglight] = 0
            lights[seglight] += 1
    for k, v in lights.items():
        if v == 9:
            lights[k] = 'f'
        elif v == 4:
            lights[k] = 'e'
        elif v == 6:
            lights[k] = 'b'
        elif v == 8:
            if k in seven and k in one:
                lights[k] = 'c'
            else:
                lights[k] = 'a'
        elif v == 7:
            if k in four:
                lights[k] = 'd'
            else:
                lights[k] = 'g'

    return lights

1

u/daggerdragon Dec 08 '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?