From 57ff6ef0d8d141b8158ef74214166d8886bae9bd Mon Sep 17 00:00:00 2001 From: kleph Date: Thu, 9 Dec 2021 00:02:38 +0100 Subject: [PATCH] Day 8 part 2 --- 8/8_2.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 8/8_2.py diff --git a/8/8_2.py b/8/8_2.py new file mode 100755 index 0000000..6b15c3e --- /dev/null +++ b/8/8_2.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# 2021 - Advent Of Code - 8 part 2 + +# acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf + +def parse_file(file): + numbers_list = [] + outputs = [] + with open(file) as f: + for line in f.readlines(): + numbers, output = line.strip().split(' | ') + numbers_list.append(numbers) + outputs.append(output) + + return numbers_list, outputs + + +def print_numbers(id): + print('identified numbers:') + for i in range(10): + print(f'{i} is {id[i]}') + + +# entries, displays = parse_file('input_example1.txt') +entries, displays = parse_file('input.txt') + +output_sum = 0 +for entry, display in zip(entries, displays): + identified_numbers = [-1] * 10 + words = entry.split(' ') + + # size of pattern for 1, 4, 7 and 8 is unique + identified_numbers[1] = set([x for x in words if len(x) == 2][0]) + identified_numbers[7] = set([x for x in words if len(x) == 3][0]) + identified_numbers[4] = set([x for x in words if len(x) == 4][0]) + identified_numbers[8] = set([x for x in words if len(x) == 7][0]) + + # size 6 pattern that doesn't include 4, nor 1 -> 6 + # size 6 pattern that includes 4 and 1 -> 9 + # the remaining size 6 pattern -> 0 + size_six = set([x for x in words if len(x) == 6]) + for s in size_six: + if identified_numbers[1].issubset(s) and identified_numbers[4].issubset(s): + identified_numbers[9] = s + if not identified_numbers[1].issubset(s) and not identified_numbers[4].issubset(s): + identified_numbers[6] = s + + size_six.remove(identified_numbers[9]) + identified_numbers[9] = set(identified_numbers[9]) + size_six.remove(identified_numbers[6]) + identified_numbers[6] = set(identified_numbers[6]) + identified_numbers[0] = set(next(iter(size_six))) + + # size 5 pattern that includes 7 -> 3 + # size 5 pattern that only missed one element to be a 9 pattern -> 5 + # the remaining size 5 pattern -> 2 + size_five = set([x for x in words if len(x) == 5]) + for s in size_five: + if identified_numbers[7].issubset(s): + identified_numbers[3] = s + size_five.remove(identified_numbers[3]) + identified_numbers[3] = set(identified_numbers[3]) + + for s in size_five: + if len(identified_numbers[9] - set(s)) == 1: + identified_numbers[5] = s + break + size_five.remove(identified_numbers[5]) + identified_numbers[5] = set(identified_numbers[5]) + + identified_numbers[2] = set(next(iter(size_five))) + # print_numbers(identified_numbers) + + words = display.split(' ') + str_num = "" + for d in words: + n = identified_numbers.index(set(d)) + str_num += str(n) + output_sum += int(str_num) + +print(f'output_sum: {output_sum}')