Skip to content

Instantly share code, notes, and snippets.

@khanzf
Last active March 30, 2017 21:29
Show Gist options
  • Save khanzf/19b5baccd4b07d5138ec0a867c0b9454 to your computer and use it in GitHub Desktop.
Save khanzf/19b5baccd4b07d5138ec0a867c0b9454 to your computer and use it in GitHub Desktop.
Draw shape without picking up pen
#!/usr/bin/env python3
import copy
import sys
lines = {
1:[2,3],
2:[1,3,4,6,7],
3:[1,2,5,6,7],
4:[2,5,6,7],
5:[3,7],
6:[2,3,4,7,8],
7:[2,3,5,6,8],
8:[6,7]
}
def check(cstate):
if len(cstate) != 8:
return
for offset in lines:
if sorted(lines[offset]) != sorted(cstate[offset]):
return
print("Solution!")
sys.exit()
def iteration(clocation, cstate):
print("iteration(%s %s)" % (clocation, cstate))
check(cstate)
for ilocation in lines[clocation]:
print("%d -> %d" % (clocation, ilocation))
nstate = copy.deepcopy(cstate)
y = nstate.get(clocation, [])
x = nstate.get(ilocation, [])
if ilocation in y:
continue
y = y + [ilocation]
x = x + [clocation]
nstate[clocation] = y
nstate[ilocation] = x
iteration(ilocation, nstate)
iteration(1, {})
iteration(2, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment