76 lines
1.5 KiB
Python
76 lines
1.5 KiB
Python
![]() |
#!/usr/bin/python
|
||
|
|
||
|
import sys
|
||
|
import re
|
||
|
|
||
|
dm = {
|
||
|
0: 'abcefg',
|
||
|
1: 'cf',
|
||
|
2: 'acdeg',
|
||
|
3: 'acdfg',
|
||
|
4: 'bcdf',
|
||
|
5: 'abdfg',
|
||
|
6: 'abdefg',
|
||
|
7: 'acf',
|
||
|
8: 'abcdefg',
|
||
|
9: 'abcdfg',
|
||
|
|
||
|
}
|
||
|
count = 0
|
||
|
|
||
|
def intersect(a, b):
|
||
|
if a is None:
|
||
|
return 0
|
||
|
if b is None:
|
||
|
return 0
|
||
|
res = set(a).intersection(b)
|
||
|
return len(res)
|
||
|
|
||
|
def map_X(x, digits, result):
|
||
|
if x in result:
|
||
|
return result
|
||
|
v = list(result.values())
|
||
|
for i in digits:
|
||
|
if i in v:
|
||
|
continue
|
||
|
if len(i) != len(dm[x]):
|
||
|
continue
|
||
|
#print("mapping %s for %s" % (x, i, ))
|
||
|
i0 = []
|
||
|
for (k,v) in result.items():
|
||
|
i1 = intersect(dm[k], dm[x])
|
||
|
i2 = intersect(v, i)
|
||
|
i0.append(i1 == i2)
|
||
|
#print(i0)
|
||
|
if len(i0) == sum(i0):
|
||
|
result[x] = i
|
||
|
break
|
||
|
return result
|
||
|
|
||
|
result = []
|
||
|
with open("input01.txt","r") as f:
|
||
|
for line in f:
|
||
|
maps = dict()
|
||
|
tokens = [''.join(sorted(x)) for x in line.strip().split(' ')]
|
||
|
#tokens = line.strip().split(' ')
|
||
|
|
||
|
for _ in range(10):
|
||
|
maps = map_X(1,tokens[0:10], maps)
|
||
|
maps = map_X(4,tokens[0:10], maps)
|
||
|
maps = map_X(7,tokens[0:10], maps)
|
||
|
maps = map_X(8,tokens[0:10], maps)
|
||
|
maps = map_X(9,tokens[0:10], maps)
|
||
|
maps = map_X(6,tokens[0:10], maps)
|
||
|
maps = map_X(0,tokens[0:10], maps)
|
||
|
maps = map_X(2,tokens[0:10], maps)
|
||
|
maps = map_X(3,tokens[0:10], maps)
|
||
|
maps = map_X(5,tokens[0:10], maps)
|
||
|
|
||
|
maps_r = {v:k for (k,v) in maps.items()}
|
||
|
|
||
|
line_esult = ''
|
||
|
for n in tokens[11:]:
|
||
|
line_esult = line_esult + str(maps_r[n])
|
||
|
result.append(int(line_esult))
|
||
|
|
||
|
print(sum(result))
|