Day 08 (glamorized)

This commit is contained in:
Sebastian Seedorf
2021-12-08 11:47:51 +01:00
parent a18bb716ba
commit 7dd2b4ce45

View File

@@ -32,37 +32,26 @@
lines = (x.strip() for x in open("input.txt")) lines = (x.strip() for x in open("input.txt"))
result = 0 result = 0
MAPPING = {
(5, 2, 3): 3,
(5, 1, 3): 5,
(5, 1, 2): 2,
(6, 2, 4): 9,
(6, 2, 3): 0,
(6, 1, 3): 6
}
for line in lines: for line in lines:
input, output = tuple(map(lambda x: x.split(), line.split(' | '))) input, output = tuple(map(lambda x: list(map(lambda v: ''.join(sorted(v)), x.split())), line.split(' | ')))
input.sort(key=len) input.sort(key=len)
numbers = {} num1, num4 = input[0], input[2]
numbers[1] = set(input[0]) numbers = {input[0]: 1, input[2]: 4, input[1]: 7, input[9]: 8}
numbers[4] = set(input[2])
numbers[7] = set(input[1])
numbers[8] = set(input[9])
for seg in input: for seg in input:
vals = set(seg) compare = (len(seg), sum(x in num1 for x in seg), sum(x in num4 for x in seg))
compare = (len(seg), len(numbers[1].intersection(vals)), len(numbers[4].intersection(vals))) if compare in MAPPING:
if compare == (5, 2, 3): numbers[seg] = MAPPING[compare]
numbers[3] = vals
elif compare == (5, 1, 3):
numbers[5] = vals
elif compare == (5, 1, 2):
numbers[2] = vals
elif compare == (6, 2, 4):
numbers[9] = vals
elif compare == (6, 2, 3):
numbers[0] = vals
elif compare == (6, 1, 3):
numbers[6] = vals
# prepare comparison
numbers = {''.join(sorted(v)): k for k, v in numbers.items()}
output = (''.join(sorted(v)) for v in output)
# substitute
result += int(''.join(str(numbers[v]) for v in output)) result += int(''.join(str(numbers[v]) for v in output))
print(numbers, output)
print(result) print(result)