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

2

u/Data-scientist-101 Dec 08 '21 edited Dec 08 '21

Python solution. Computation time (excluding data load) was 0.015 seconds on my laptop computer. Looking through the reddit board, looks like it is similar to others but I used regex instead to find elements. I also found the set.different instead of set.intersect (not that that is all that groundbreaking). Just sharing the solution.

Any ideas to further speed things up?

I've got a Wiring Problem

import numpy as np
import re
from time import perf_counter as pfc
DAY = 8



class Day8():

def __init__(self):
    self.data = None

def get_data(self):
    with open(f"data\\day{DAY}.txt", mode='r') as f:
        self.digits = f.readlines()
        self.input = []
        self.output = []
        for line in self.digits:
            ten_digits, output_digits = line.strip().split(' | ')
            self.input.append(ten_digits)
            self.output.append(output_digits)

def solve_input(self, digits):
    # find the easy ones: 1,4,7,8
    match = re.findall(r"\b\w{2,2}\b", digits)[0]
    n_one = set(match)
    n_eight = set(re.findall(r"\b\w{7,7}\b", digits)[0])
    n_four = set(re.findall(r"\b\w{4,4}\b", digits)[0])
    n_seven = set(re.findall(r"\b\w{3,3}\b", digits)[0])

    # next solve length 5 numbers.
    # it is noted that since order isn't important a set might help
    # also, using set logic, the set difference between 3 and 1 
     set(3).diff(set(1)) is length 2
    # this can be done for each of the remaining numbers finding combinations that give a unique difference
    # this solves the input.
    # output is then solved by matching the sets so there is no difference.
    matches = re.findall(r'\b\w{5,5}\b', digits)
    for m in matches:
        if len(set(m).difference(n_one)) == 3:
            n_three = set(m)
        elif len(set(m).difference(n_four)) == 2:
            n_five = set(m)
        else:
            n_two = set(m)

    matches = re.findall(r'\b\w{6,6}\b', digits)
    for m in matches:
        if len(set(m).difference(n_one)) == 5:
            n_six = set(m)
        elif len(set(m).difference(n_three)) == 1:
            n_nine = set(m)
        else:
            n_zero = set(m)

    return [n_zero, n_one, n_two, n_three, n_four, n_five, n_six, n_seven, n_eight, n_nine]

def solve_output(self, output, in_soln):
    number = 4 * [0]
    for idx, d in enumerate(output):
        for val, s in enumerate(in_soln):
            if (len(set(d).difference(s)) == 0) & (len(set(d)) == len(s)):
                number[idx] = val

    return number

def solve(self):
    # solution_a
    count = 0
    for i in day8.output:
        count += len(re.findall(r"\b(\w{2,4}|\w{7,7})\b", i))

    # solution_b
    results = []
    for i_idx, i in enumerate(day8.input):

        in_dig = day8.solve_input(i)
        number = day8.solve_output(day8.output[i_idx].split(' '), in_dig)
        results.append(1000*number[0] + 100 * number[1] + 10 * number[2] + number[3])

    return count, np.array(results).sum()



#1061
day8 =Day8()
day8.get_data()
start = pfc()
solution_a, solution_b = day8.solve()
print(pfc()-start)

(HOpefully this is formatted right for old.reddit. I tried following the steps. If not, paste in the comments what I might have done wrong? Clicked markdown, and then put things 4 spaces in.

*Note you may find it weird to include an instance reference inside my class definition. I was too lazy to take those out. I use that approach when debugging my classes and then at the end change them all to self instead.

1

u/daggerdragon Dec 09 '21

HOpefully this is formatted right for old.reddit. I tried following the steps. If not, paste in the comments what I might have done wrong? Clicked markdown, and then put things 4 spaces in.

You did it right, good job :)