Tidy up day 8 solution
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def part_1(input):
|
||||
@@ -20,39 +19,39 @@ def part_2(input):
|
||||
inp, out = line.strip().split(' | ')
|
||||
inp = inp.split()
|
||||
out = out.split()
|
||||
res = ''
|
||||
code = defaultdict(str)
|
||||
digit = ''
|
||||
code = {}
|
||||
for d in inp:
|
||||
if len(d) == 2:
|
||||
code['1'] = d
|
||||
code['1'] = sorted(d)
|
||||
elif len(d) == 4:
|
||||
code['4'] = d
|
||||
code['4'] = sorted(d)
|
||||
elif len(d) == 3:
|
||||
code['7'] = d
|
||||
code['7'] = sorted(d)
|
||||
elif len(d) == 7:
|
||||
code['8'] = d
|
||||
code['8'] = sorted(d)
|
||||
for d in inp:
|
||||
if len(d) == 6:
|
||||
if len(''.join(set(code['4']).intersection(d))) == 4:
|
||||
code['9'] = d
|
||||
elif len(''.join(set(code['7']).intersection(d))) == 3:
|
||||
code['0'] = d
|
||||
if len(set(code['4']) & set(d)) == 4:
|
||||
code['9'] = sorted(d)
|
||||
elif len(set(code['7']) & set(d)) == 3:
|
||||
code['0'] = sorted(d)
|
||||
else:
|
||||
code['6'] = d
|
||||
code['6'] = sorted(d)
|
||||
elif len(d) == 5:
|
||||
if len(''.join(set(code['7']).intersection(d))) == 3:
|
||||
code['3'] = d
|
||||
elif len(''.join(set(code['4']).intersection(d))) == 3:
|
||||
code['5'] = d
|
||||
if len(set(code['7']) & set(d)) == 3:
|
||||
code['3'] = sorted(d)
|
||||
elif len(set(code['4']) & set(d)) == 3:
|
||||
code['5'] = sorted(d)
|
||||
else:
|
||||
code['2'] = d
|
||||
code['2'] = sorted(d)
|
||||
assert len(code) == 10
|
||||
for d in out:
|
||||
for k in code:
|
||||
if sorted(code[k]) == sorted(d):
|
||||
res += k
|
||||
if code[k] == sorted(d):
|
||||
digit += k
|
||||
break
|
||||
result += int(res, 10)
|
||||
result += int(digit)
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user