From 66c78818fc5886da8e07c4ee4486baf2de244538 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Wed, 8 Dec 2021 10:18:44 +0100 Subject: [PATCH] 2021 day8/python: add brute force solution --- 2021/day8/bruteforce.py | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 2021/day8/bruteforce.py diff --git a/2021/day8/bruteforce.py b/2021/day8/bruteforce.py new file mode 100755 index 0000000..5dba549 --- /dev/null +++ b/2021/day8/bruteforce.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +import sys +import itertools + +def parse(line): + inputs, outputs = [[num.strip() for num in nums.strip().split(' ')] for nums in line.split('|')] + return (inputs, outputs) + +if __name__ == '__main__': + inp = [parse(i) for i in sys.stdin.readlines()] + + permutations = list(''.join(s) for s in itertools.permutations('abcdefg')) + segments = [ + 'abcefg', + 'cf', + 'acdeg', + 'acdfg', + 'bcdf', + 'abdfg', + 'abdefg', + 'acf', + 'abcdefg', + 'abcdfg', + ] + + acc1 = 0 + acc2 = 0 + for (inputs, outputs) in inp: + acc1 += sum(len(output) in (2,3,4,7) for output in outputs) + + for permutation in permutations: + trans = str.maketrans(permutation, 'abcdefg') + + def translate_segments(digit): + return ''.join(sorted(digit.translate(trans))) + + if all(translate_segments(digit) in segments for digit in inputs): + acc2 += int(''.join( + str(segments.index( + translate_segments(digit) + )) for digit in outputs + )) + break + print(acc1) + print(acc2)