Created
December 12, 2021 22:04
-
-
Save spielkind/b42fcff217a727b7c34d104ce895bea1 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
#!/bin/python | |
entries = [] | |
with open('day8.txt') as f: | |
for line in f: | |
entry = [] | |
for part in line.strip().split('|'): | |
entry.append([sorted(list(segments)) for segments in part.strip().split()]) | |
entries.append(entry) | |
count = sum(len(list(filter(lambda s: len(s) in [2, 3, 4, 7], entry[1]))) for entry in entries) | |
print('Part One:', count) | |
digits = [] | |
for entry in entries: | |
conf = {num: [] for num in range(10)} | |
(signals, values) = (entry[0], entry[1]) | |
while len(list(filter(lambda c: conf[c] == [], conf))): | |
for segments in filter(lambda s: s not in conf.values(), signals): | |
if len(segments) == 2: conf[1] = segments | |
if len(segments) == 3: conf[7] = segments | |
if len(segments) == 4: conf[4] = segments | |
if len(segments) == 7: conf[8] = segments | |
if len(segments) == 5 and set(conf[1]).issubset(segments): conf[3] = segments | |
if len(segments) == 6 and set(conf[4]).issubset(segments): conf[9] = segments | |
if len(segments) == 6 and not set(conf[7]).issubset(segments): conf[6] = segments | |
if len(segments) == 6 and set(conf[7]).issubset(segments) and not set(conf[4]).issubset(segments): conf[0] = segments | |
if len(segments) == 5 and set(segments).issubset(conf[6]): conf[5] = segments | |
if len(segments) == 5 and not set(segments).issubset(conf[6]) and not set(conf[1]).issubset(segments): conf[2] = segments | |
digit = int(''.join([str(d) for segments in values for d, s in conf.items() if s == segments])) | |
digits.append(digit) | |
print('Part Two:', sum(digits)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment