66 lines
1.2 KiB
Python
66 lines
1.2 KiB
Python
from lib import get_data
|
|
from itertools import permutations
|
|
|
|
data = get_data(__file__).strip()
|
|
|
|
code_to_num = {
|
|
"cf": 1,
|
|
"acf": 7,
|
|
"bcdf": 4,
|
|
"acdeg": 2,
|
|
"acdfg": 3,
|
|
"abdfg": 5,
|
|
"abcefg": 0,
|
|
"abdefg": 6,
|
|
"abcdfg": 9,
|
|
"abcdefg": 8,
|
|
}
|
|
|
|
|
|
def translate(inn, mapping):
|
|
out = ""
|
|
for c in inn:
|
|
out += mapping[c]
|
|
return "".join(sorted(out))
|
|
|
|
|
|
def decode(inn, out):
|
|
s = "abcdefg"
|
|
m = None
|
|
for p in permutations(s):
|
|
m = {k: v for k, v in zip(p, s)}
|
|
all = set(code_to_num.keys())
|
|
for in_code in inn:
|
|
out_code = translate(in_code, m)
|
|
if out_code in all:
|
|
all.remove(out_code)
|
|
|
|
if len(all) == 0:
|
|
break
|
|
else:
|
|
m = None
|
|
|
|
assert m is not None
|
|
|
|
o = 0
|
|
d = 1000
|
|
for out_code in out:
|
|
out_code = translate(out_code, m)
|
|
v = code_to_num[out_code]
|
|
o += d * v
|
|
d //= 10
|
|
return o
|
|
|
|
|
|
s1 = 0
|
|
s2 = 0
|
|
for line in data.splitlines():
|
|
inn, out = line.strip().split(" | ")
|
|
inn = inn.split()
|
|
out = out.split()
|
|
s1 += sum(1 for v in out if len(v) in [2, 3, 4, 7])
|
|
s2 += decode(inn, out)
|
|
|
|
print(s1)
|
|
print(s2)
|