Advent of code - Day 8

Objective

A seven segment display is a common type of electronic display that uses seven electronic segments to display a digit.

Seven segment display

For this challenge, the signals were messed up, and the solution involved decoding the signals.

Part 1

How many times do the digits 1, 4, 7 or 8 appear.

digit_length_map = {2: '1', 3: '7', 4: '4', 7: '8'}

def day8_1(puzzle_input: List[Tuple]):
    return sum([len([output
                     for output in outputs
                     if len(output) in [2, 3, 4, 7]])
                for signals, outputs in puzzle_input
                ])

Part 2

For each line contain a list of 10 signals and 4 outputs - translate the character outputs to digits.

The solution involved finding the digit based on the length of the string e.g. only the number 4 can be represented by 4 characters, or by seeing which segments of each string overlapped.

def day8_2(puzzle_input: List[Tuple]):
    # Store the digits that we know the lengths of
    decoded_outputs = []
    for signals, outputs in puzzle_input:
        codes = {digit_length_map.get(len(o)): set(o)
                 for o in list(signals) + list(outputs)
                 if len(o) in digit_length_map
                 }

        digits = ''
        for output in outputs:
            outs = set(output)
            if len(output) == 5:
                if outs.issuperset(codes['7']):
                    # 3 contains all the segments in 7
                    digits += '3'
                elif outs.issuperset(codes['4'] - codes['1']):
                    # 5 contains the segents in 4 not in 1
                    digits += '5'
                else:
                    digits += '2'
            elif len(output) == 6:
                if outs.issuperset(codes['4']):
                    # 9 contains the segments in 4
                    digits += '9'
                elif len(outs & codes['1']) == 1:
                    # 6 contains one segment of 1
                    digits += '6'
                else:
                    digits += '0'
            else:
                digits += digit_length_map[len(outs)]

        decoded_outputs.append(int(digits))
    return sum(decoded_outputs)
Dwight Gunning

Dwight Gunning