59 lines
1.4 KiB
Python
59 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
import fileinput
|
|
import time
|
|
|
|
|
|
lines = list(fileinput.input())
|
|
|
|
t = time.time_ns()
|
|
|
|
part2 = 0
|
|
for line in lines:
|
|
poss = {}
|
|
|
|
digits, output = (l.split() for l in str(line).split(" | "))
|
|
|
|
one = set(next(digit for digit in digits if len(digit) == 2))
|
|
four = set(next(digit for digit in digits if len(digit) == 4))
|
|
|
|
for digit in digits:
|
|
digit = "".join(sorted(digit))
|
|
digit_set = set(digit)
|
|
|
|
overlap_one = len(digit_set & one)
|
|
overlap_four = len(digit_set & four)
|
|
|
|
if len(digit) == 2: # 1
|
|
poss[digit] = 1
|
|
elif len(digit) == 3: # 7
|
|
poss[digit] = 7
|
|
elif len(digit) == 4: # 4
|
|
poss[digit] = 4
|
|
elif len(digit) == 5: # 2, 3, 5, 6
|
|
if overlap_four == 2:
|
|
poss[digit] = 2
|
|
elif overlap_one == 1:
|
|
poss[digit] = 5
|
|
else:
|
|
poss[digit] = 3
|
|
elif len(digit) == 6: # 0, 6, 9
|
|
if overlap_four == 4:
|
|
poss[digit] = 9
|
|
elif overlap_one == 2:
|
|
poss[digit] = 0
|
|
else:
|
|
poss[digit] = 6
|
|
elif len(digit) == 7:
|
|
poss[digit] = 8
|
|
|
|
result = 0
|
|
for digit in output:
|
|
digit = "".join(sorted(digit))
|
|
result = (result * 10) + poss[digit]
|
|
|
|
part2 += result
|
|
|
|
t2 = time.time_ns()
|
|
print(part2)
|
|
print(t2 - t, "ns")
|
|
print((t2 - t) // 1000, "µs")
|