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
|
#!/usr/bin/env python3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
|
|
||||||
def part_1(input):
|
def part_1(input):
|
||||||
@@ -20,39 +19,39 @@ def part_2(input):
|
|||||||
inp, out = line.strip().split(' | ')
|
inp, out = line.strip().split(' | ')
|
||||||
inp = inp.split()
|
inp = inp.split()
|
||||||
out = out.split()
|
out = out.split()
|
||||||
res = ''
|
digit = ''
|
||||||
code = defaultdict(str)
|
code = {}
|
||||||
for d in inp:
|
for d in inp:
|
||||||
if len(d) == 2:
|
if len(d) == 2:
|
||||||
code['1'] = d
|
code['1'] = sorted(d)
|
||||||
elif len(d) == 4:
|
elif len(d) == 4:
|
||||||
code['4'] = d
|
code['4'] = sorted(d)
|
||||||
elif len(d) == 3:
|
elif len(d) == 3:
|
||||||
code['7'] = d
|
code['7'] = sorted(d)
|
||||||
elif len(d) == 7:
|
elif len(d) == 7:
|
||||||
code['8'] = d
|
code['8'] = sorted(d)
|
||||||
for d in inp:
|
for d in inp:
|
||||||
if len(d) == 6:
|
if len(d) == 6:
|
||||||
if len(''.join(set(code['4']).intersection(d))) == 4:
|
if len(set(code['4']) & set(d)) == 4:
|
||||||
code['9'] = d
|
code['9'] = sorted(d)
|
||||||
elif len(''.join(set(code['7']).intersection(d))) == 3:
|
elif len(set(code['7']) & set(d)) == 3:
|
||||||
code['0'] = d
|
code['0'] = sorted(d)
|
||||||
else:
|
else:
|
||||||
code['6'] = d
|
code['6'] = sorted(d)
|
||||||
elif len(d) == 5:
|
elif len(d) == 5:
|
||||||
if len(''.join(set(code['7']).intersection(d))) == 3:
|
if len(set(code['7']) & set(d)) == 3:
|
||||||
code['3'] = d
|
code['3'] = sorted(d)
|
||||||
elif len(''.join(set(code['4']).intersection(d))) == 3:
|
elif len(set(code['4']) & set(d)) == 3:
|
||||||
code['5'] = d
|
code['5'] = sorted(d)
|
||||||
else:
|
else:
|
||||||
code['2'] = d
|
code['2'] = sorted(d)
|
||||||
assert len(code) == 10
|
assert len(code) == 10
|
||||||
for d in out:
|
for d in out:
|
||||||
for k in code:
|
for k in code:
|
||||||
if sorted(code[k]) == sorted(d):
|
if code[k] == sorted(d):
|
||||||
res += k
|
digit += k
|
||||||
break
|
break
|
||||||
result += int(res, 10)
|
result += int(digit)
|
||||||
print("Part 2 result:", result)
|
print("Part 2 result:", result)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user