This commit is contained in:
73
day-08/day-08.py
Normal file
73
day-08/day-08.py
Normal file
@@ -0,0 +1,73 @@
|
||||
#!/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) == 2:
|
||||
result += 1
|
||||
elif len(d) == 4:
|
||||
result += 1
|
||||
elif len(d) == 3:
|
||||
result += 1
|
||||
elif len(d) == 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
|
||||
if '1' not in code:
|
||||
code['1'] = ''.join(set(code['4']).intersection(code['7']))
|
||||
if '8' not in code:
|
||||
code['8'] = 'abcdefg'
|
||||
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
|
||||
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)
|
||||
Reference in New Issue
Block a user