All checks were successful
continuous-integration/drone/push Build is passing
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
from collections import defaultdict
|
|
|
|
|
|
def part_1(input):
|
|
result = 0
|
|
for line in input:
|
|
_, out = line.strip().split(' | ')
|
|
out = out.split()
|
|
for d in out:
|
|
if len(d) in [2, 3, 4, 7]:
|
|
result += 1
|
|
print("Part 1 result:", result)
|
|
|
|
|
|
def part_2(input):
|
|
result = 0
|
|
for line in input:
|
|
inp, out = line.strip().split(' | ')
|
|
inp = inp.split()
|
|
out = out.split()
|
|
res = ''
|
|
code = defaultdict(str)
|
|
for d in inp:
|
|
if len(d) == 2:
|
|
code['1'] = d
|
|
elif len(d) == 4:
|
|
code['4'] = d
|
|
elif len(d) == 3:
|
|
code['7'] = d
|
|
elif len(d) == 7:
|
|
code['8'] = 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
|
|
else:
|
|
code['6'] = 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
|
|
else:
|
|
code['2'] = d
|
|
assert len(code) == 10
|
|
for d in out:
|
|
for k in code:
|
|
if sorted(code[k]) == sorted(d):
|
|
res += k
|
|
break
|
|
result += int(res, 10)
|
|
print("Part 2 result:", result)
|
|
|
|
|
|
input = list()
|
|
p = Path(__file__).with_name('input.txt')
|
|
with open(p) as f:
|
|
input = f.readlines()
|
|
part_1(input)
|
|
part_2(input)
|