All checks were successful
continuous-integration/drone/push Build is passing
64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
|
|
|
|
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()
|
|
digit = ''
|
|
code = {}
|
|
for d in inp:
|
|
if len(d) == 2:
|
|
code['1'] = sorted(d)
|
|
elif len(d) == 4:
|
|
code['4'] = sorted(d)
|
|
elif len(d) == 3:
|
|
code['7'] = sorted(d)
|
|
elif len(d) == 7:
|
|
code['8'] = sorted(d)
|
|
for d in inp:
|
|
if len(d) == 6:
|
|
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'] = sorted(d)
|
|
elif len(d) == 5:
|
|
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'] = sorted(d)
|
|
assert len(code) == 10
|
|
for d in out:
|
|
for k in code:
|
|
if code[k] == sorted(d):
|
|
digit += k
|
|
break
|
|
result += int(digit)
|
|
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)
|