Skip to content

Instantly share code, notes, and snippets.

@dloscutoff dloscutoff/codeSquareGenerator.py Secret
Last active Feb 14, 2017

Embed
What would you like to do?
Generating a 10x10 Ruby code square for the word "haters" (http://codegolf.stackexchange.com/q/35372)
import itertools
permutations = itertools.permutations
combinations = itertools.combinations
combsReplace = itertools.combinations_with_replacement
allLetters = set("haters")
shell = """{0}={1}=?{2}
{4}={3}=?\s
{5}='haters
{9} hate
in hardest
hearts.';;
{7}={6}=
{8}={5}.tr'
',{4};{0}
{10} {6}"""
def fill():
varnames = ["____", "__", "_", "__", "___", "__", "____", "____", "___",
"_____", "_____"]
for n9 in ["nurse", "store"]:
varnames[9] = n9
for n10 in ["print", "puts ", " puts"]:
varnames[10] = n10
req6 = allLetters - set(n10)
for wild6 in combinations("aehrst", 4-len(req6)):
for n6 in permutations(tuple(req6) + wild6):
varnames[6] = "".join(n6)
layout = shell.format(*varnames)
if not partialTest(layout):
continue
req7 = allLetters - set(n6)
for wild7 in combsReplace("aehrst", 4-len(req7)):
for n7 in permutations(tuple(req7) + wild7):
varnames[7] = "".join(n7)
layout = shell.format(*varnames)
if not partialTest(layout):
continue
for n0 in permutations("aehrst", 4):
# NB: n0 could also have repetitions if that would be helpful
if "s" not in n0:
# There has to be an s in n0, because there cannot be one
# in n4 (because it's on a line with ?\s, and there are no
# free slots on that row)
continue
varnames[0] = "".join(n0)
layout = shell.format(*varnames)
if not partialTest(layout):
continue
req4 = allLetters - set(n0)
for wild4 in "aehrt":
for n4 in permutations("".join(req4) + wild4):
varnames[4] = "".join(n4)
layout = shell.format(*varnames)
if not partialTest(layout):
continue
for n5 in permutations("aehs", 2):
# NB: n5 could also have repetitions or r/t if helpful
varnames[5] = "".join(n5)
layout = shell.format(*varnames)
if not partialTest(layout):
continue
req3 = allLetters - set(n4) - set("s")
if len(req3) > 2:
continue
for n3 in permutations(req3):
varnames[3] = "".join(n3)
layout = shell.format(*varnames)
if not partialTest(layout):
continue
req8 = allLetters - set(n5+("t","r"))
for wild8 in combinations("aehrst", 3-len(req8)):
for n8 in permutations(tuple(req8) + wild8):
varnames[8] = "".join(n8)
layout = shell.format(*varnames)
if not partialTest(layout):
continue
req1 = allLetters - set(n0)
for wild1 in combinations("aehrst", 3-len(req1)):
for n1 in permutations(tuple(req1) + wild1):
varnames[1] = "".join(n1[:2])
varnames[2] = n1[2]
layout = shell.format(*varnames)
if not partialTest(layout):
continue
yield layout
varnames[1] = "__"
varnames[2] = "_"
varnames[8] = "___"
varnames[3] = "__"
varnames[5] = "__"
varnames[4] = "___"
varnames[0] = "____"
varnames[7] = "____"
def partialTest(layout):
for i, col in enumerate(zip(*layout.splitlines())):
slots = col.count("_")
missing = allLetters - set(col)
if len(missing) > slots:
return False
return True
i=0
for layout in fill():
i=i+1
print(i)
print(layout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.