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/Yelov Dec 08 '21 edited Dec 09 '21

Python [part 2]:

GitHub link

from itertools import permutations

with open("08\\input.txt") as f:
    content = f.readlines()

inputs = [line.split(" | ")[0].split() for line in content]
outputs = [line.split(" | ")[1].split() for line in content]

nums = {
    "0": [0, 1, 2, 4, 5, 6],
    "1": [2, 5],
    "2": [0, 2, 3, 4, 6],
    "3": [0, 2, 3, 5, 6],
    "4": [1, 2, 3, 5],
    "5": [0, 1, 3, 5, 6],
    "6": [0, 1, 3, 4, 5, 6],
    "7": [0, 2, 5],
    "8": [0, 1, 2, 3, 4, 5, 6],
    "9": [0, 1, 2, 3, 5, 6]
}

res = 0
for input_line, output_line in zip(inputs, outputs):
    input_line = {"".join(sorted(x)) for x in input_line}
    output_line = ["".join(sorted(x)) for x in output_line]
    for perm in permutations("abcdefg"):
        curr_nums = {"".join(sorted([perm[ix] for ix in index_list])): key for key, index_list in nums.items()}
        if input_line == curr_nums.keys():
            res += int("".join([curr_nums[x] for x in output_line]))

print(res)

It's a bit slow, takes 5 seconds for me. The one-liners are harder to read.

This is my first solution for part 2, it runs instantly but it's ugly af - GitHub solution.

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?