#!/usr/bin/env kuroko ''' Day 8, Part 2Part 1 removed, mostly because I didn't keep the code around for it. ''' ## Common template, load data import fileio, kuroko let lines with fileio.open(kuroko.argv[1] if len(kuroko.argv) > 1 else kuroko.argv[0].replace('.krk','.txt'),'r') as f: lines = [line.strip() for line in f.readlines()] def bitand(c, m): for x in m: if x not in c: return False return True def minus(candidate,mask): let out = [] for c in candidate: if c not in mask: out.append(c) return out let output = 0 for line in lines: let left, right = [x.strip() for x in line.split(' | ')] let stuff = left.split(' ') let outs = right.split(' ') let digits = {} let comb = [''.join(sorted(x)) for x in stuff] comb.extend([''.join(sorted(x)) for x in outs]) # 1, 4, 7, 8 have Unique lengths for x in comb: if len(x) == 2: digits[1] = x else if len(x) == 3: digits[7] = x else if len(x) == 4: digits[4] = x else if len(x) == 7: digits[8] = x # 0, 9, and 6 have 6 segments and unique overlaps against 4 and 1 for x in comb: if len(x) == 6: if bitand(x, digits[4]): digits[9] = x else if bitand(x, digits[1]): digits[0] = x else: digits[6] = x # 5 is the only 5-segment that overlaps with 6 for x in comb: if len(x) == 5 and bitand(digits[6], x): digits[5] = x # 2 and 3 have different numbers of strokes that aren't on the right for x in comb: if len(x) == 5 and x != digits[5]: let count = len(minus(x,digits[1])) if count == 3: digits[3] = x else if count == 4: digits[2] = x let backwards = {} for k,v in digits.items(): backwards[v] = str(k) let digit = ''.join(backwards[''.join(sorted(i))] for i in outs) output += int(digit) # Part 2 print(output)