Created
December 8, 2021 11:04
-
-
Save cwhy/a235c9bd200a201ddc4977acb458f901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from collections import Counter | |
def get_table(m): | |
invert_counter = {v: i for i, v in Counter(''.join(m)).items()} | |
digit_table = { | |
'e': invert_counter[4], | |
'b': invert_counter[6], | |
'f': invert_counter[9], | |
} | |
def rm_from_str(s): | |
return s.translate(str.maketrans('', '', ''.join(digit_table.values()))) | |
digit_table['c'] = rm_from_str(m[0]) | |
digit_table['a'] = rm_from_str(m[1]) | |
digit_table['d'] = rm_from_str(m[2]) | |
digit_table['g'] = rm_from_str(m[-1]) | |
return {v: i for i, v in digit_table.items()} | |
with open('input8.txt') as file: | |
content = file.readlines() | |
digit_list = ['abcefg', 'cf', 'acdeg', 'acdfg', 'bcdf', 'abdfg', 'abdefg', 'acf', 'abcdefg', 'abcdfg'] | |
inverted_digit_list = {v: str(i) for i, v in enumerate(digit_list)} | |
numbers = [int(''.join( | |
inverted_digit_list[''.join(sorted(w.translate(str.maketrans(get_table(tuple(sorted(sorted(["".join(sorted(list(s))) for s in in_str.split(" ")], key= lambda x: x[0]), key=len)))))))] | |
for w in tuple("".join(sorted(list(s))) for s in out_str.split(" ")))) | |
for in_str, out_str in (row.strip().split(' | ') for row in content)] | |
print(sum(numbers)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment