#!/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 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: input, output = tuple(map(lambda x: list(map(lambda v: ''.join(sorted(v)), x.split())), line.split(' | '))) input.sort(key=len) num1, num4 = input[0], input[2] numbers = {input[0]: 1, input[2]: 4, input[1]: 7, input[9]: 8} for seg in input: compare = (len(seg), sum(x in num1 for x in seg), sum(x in num4 for x in seg)) if compare in MAPPING: numbers[seg] = MAPPING[compare] result += int(''.join(str(numbers[v]) for v in output)) print(result)