diff --git a/day08/part2.py b/day08/part2.py index 35ee312..15a85d3 100644 --- a/day08/part2.py +++ b/day08/part2.py @@ -32,37 +32,26 @@ 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: 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) - numbers = {} - numbers[1] = set(input[0]) - numbers[4] = set(input[2]) - numbers[7] = set(input[1]) - numbers[8] = set(input[9]) + num1, num4 = input[0], input[2] + numbers = {input[0]: 1, input[2]: 4, input[1]: 7, input[9]: 8} + 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 + 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] - # 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)