Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Not good solution in Uncategorized category for Paper Dice by Sillte
from collections import Counter, defaultdict
from itertools import accumulate
templates = list()
templates.append("""BAB
#C#
#A#
#C#""")
templates.append("""#A#
BCB
#A#
#C#""")
templates.append("""AB#
#C#
#BA
#C#""")
templates.append("""AB#
#C#
#B#
#CA""")
templates.append("""A##
BCB
#A#
#C#""")
templates.append("""A##
BC#
#AB
#C#""")
templates.append("""A##
BC#
#A#
#CB""")
templates.append("""A##
BC#
#AB
##C""")
templates.append("""AB#
#CA
#B#
#C#""")
templates.append("""#B#
AC#
#BA
#C#""")
templates.append("""A#
B#
AC
#B
#C""")
""" Template check.
"""
assert len(templates) == 11
for template in templates:
d = Counter(template)
assert d["A"] == d["B"] == d["C"] == 2
assert all(len(template[0]) == len(t) for t in template)
print(template)
def trimmer(paper):
#print(paper)
my = min(index for index, row in enumerate(paper) if row.strip(" "))
mY = max(index for index, row in enumerate(paper) if row.strip(" "))
#print(my, mY, " ".strip())
paper = paper[my: mY+1]
mx = min((min(index for index, c in enumerate(row) if c != " ") for row in paper))
Mx = max((max(index for index, c in enumerate(row) if c != " ") for row in paper))
#print(Mx, mx)
return [row[mx:Mx+1] for row in paper]
def rotate(rows):
nc = len(rows)
nr = len(rows[0])
result = [[" "] * nc for _ in range(nr)]
for r in range(nr):
for c in range(nc):
result[r][c] = rows[nc - 1 - c][r]
return list(map(lambda s: "".join(s), result))
def hflip(paper):
return list(str(reversed(row) for row in paper))
def vflip(paper):
return list(reversed(paper))
def template_check(template, paper):
#print(template)
template = template.split("\n")
#print(template)
template_dict = defaultdict(list)
for r in range(len(template)):
for c in range(len(template[0])):
if template[r][c] != "#":
template_dict[template[r][c]].append((r, c))
for key, positions in template_dict.items():
assert len(positions) == 2
r0, c0 = positions[0]
r1, c1 = positions[1]
try:
if int(paper[r0][c0]) + int(paper[r1][c1]) != 7:
return False
except IndexError:
return False
except ValueError:
return False
return True
def paper_dice(paper):
paper = trimmer(paper)
papers = list()
rotates = list()
for _ in range(4):
rotates.append(paper)
paper = rotate(paper)
papers = rotates
papers = papers + list(map(vflip, papers))
papers = papers + list(map(hflip, papers))
for template in templates:
for paper in papers:
if template_check(template, paper):
return True
return False
if __name__ == '__main__':
assert paper_dice([
' ',
'12 ',
' 36 ',
' 54',
' ']) is True, 'zigzag'
assert paper_dice(['123456']) is False, '1 line'
assert paper_dice([
'123 ',
' 456']) is False, '2 lines_wrong'
assert paper_dice([
'126 ',
' 354']) is True, '2 lines_right'
print("Check done.")
pass
May 16, 2018