69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# a
|
|
# b c
|
|
# d
|
|
# e f
|
|
# g
|
|
|
|
# 1 c f 2
|
|
# 7 a c f 3
|
|
# 4 bcd f 4
|
|
# 2 a cde g 5
|
|
# 3 a cd fg 5
|
|
# 5 ab d fg 5
|
|
# 0 abc efg 6
|
|
# 6 ab defg 6
|
|
# 9 abcd fg 6
|
|
# 8 abcdefg 7
|
|
|
|
# 1 = len(2)
|
|
# 7 = len(3)
|
|
# 4 = len(4)
|
|
# 8 = len(7)
|
|
# 3 = len(5) and len(&1)==2 and len(&4)==3
|
|
# 5 = len(5) and len(&1)==1 and len(&4)==3
|
|
# 2 = len(5) and len(&1)==1 and len(&4)==2
|
|
# 9 = len(6) and len(&1)==2 and len(&4)==4
|
|
# 0 = len(6) and len(&1)==2 and len(&4)==3
|
|
# 6 = len(6) and len(&1)==1 and len(&4)==3
|
|
|
|
|
|
lines = (x.strip() for x in open("input.txt"))
|
|
result = 0
|
|
|
|
for line in lines:
|
|
input, output = tuple(map(lambda x: x.split(), line.split(' | ')))
|
|
input.sort(key=len)
|
|
numbers = {}
|
|
numbers[1] = set(input[0])
|
|
numbers[4] = set(input[2])
|
|
numbers[7] = set(input[1])
|
|
numbers[8] = set(input[9])
|
|
for seg in input:
|
|
vals = set(seg)
|
|
compare = (len(seg), len(numbers[1].intersection(vals)), len(numbers[4].intersection(vals)))
|
|
if compare == (5, 2, 3):
|
|
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))
|
|
|
|
print(numbers, output)
|
|
|
|
print(result)
|