Day 08 (glamorized)
This commit is contained in:
@@ -21,80 +21,48 @@
|
||||
# 7 = len(3)
|
||||
# 4 = len(4)
|
||||
# 8 = len(7)
|
||||
# b ==cnt(6)
|
||||
# e ==cnt(4)
|
||||
# bd==4 - 1
|
||||
# d ==bd - b
|
||||
# 3 = len(5) and len(&1)==2
|
||||
# 5 = len(5) & b
|
||||
# 2 = len(5) and !3 and !5
|
||||
# 9 = len(6) & !e
|
||||
# 0 = len(6) & !d
|
||||
# 6 = len(6) and !9 and !0
|
||||
# 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
|
||||
|
||||
# a=7-1
|
||||
# f=cnt(9)
|
||||
# c=cnt(8) and !a
|
||||
# g=cnt(7) and !d
|
||||
|
||||
# a b c d e f g
|
||||
# 8 6 8 7 4 9 7
|
||||
from collections import defaultdict, Counter
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
result = 0
|
||||
|
||||
for line in lines:
|
||||
input, output = tuple(map(lambda x: x.split(), line.split(' | ')))
|
||||
input.sort(key=len)
|
||||
numbers = {}
|
||||
|
||||
# length of the numbers
|
||||
lengths = defaultdict(set)
|
||||
for x in input:
|
||||
lengths[len(x)].add(x)
|
||||
|
||||
# count the segments
|
||||
counts = defaultdict(set)
|
||||
for k, v in Counter(''.join(input)).items():
|
||||
counts[v].add(k)
|
||||
|
||||
# easy matching
|
||||
numbers[1] = lengths[2].pop()
|
||||
numbers[4] = lengths[4].pop()
|
||||
numbers[7] = lengths[3].pop()
|
||||
numbers[8] = lengths[7].pop()
|
||||
|
||||
# necessary segment matching
|
||||
b = counts[6].pop()
|
||||
e = counts[4].pop()
|
||||
d = set(numbers[4]).difference(set(numbers[1])).difference({b}).pop()
|
||||
|
||||
for len5 in lengths[5]:
|
||||
# 3
|
||||
if len(set(len5).intersection(set(numbers[1]))) == 2:
|
||||
numbers[3] = len5
|
||||
# 5
|
||||
if b in set(len5):
|
||||
numbers[5] = len5
|
||||
# 2
|
||||
numbers[2] = lengths[5].difference({numbers[3], numbers[5]}).pop()
|
||||
|
||||
for len6 in lengths[6]:
|
||||
# 0
|
||||
if d not in set(len6):
|
||||
numbers[0] = len6
|
||||
# 9
|
||||
if e not in set(len6):
|
||||
numbers[9] = len6
|
||||
# 6
|
||||
numbers[6] = lengths[6].difference({numbers[0], numbers[9]}).pop()
|
||||
numbers[1] = set(input[0])
|
||||
numbers[4] = set(input[2])
|
||||
numbers[7] = set(input[1])
|
||||
numbers[8] = set(input[9])
|
||||
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
|
||||
|
||||
# prepare comparison
|
||||
numbers = {''.join(sorted(v)): k for k, v in numbers.items()}
|
||||
output = list(''.join(sorted(v)) for v in output)
|
||||
output = (''.join(sorted(v)) for v in output)
|
||||
|
||||
# substitute
|
||||
substitute = list(numbers[v] for v in output)
|
||||
result += int(''.join(map(str, substitute)))
|
||||
result += int(''.join(str(numbers[v]) for v in output))
|
||||
|
||||
print(numbers, output)
|
||||
|
||||
print(result)
|
||||
|
||||
Reference in New Issue
Block a user