Skip to content

Instantly share code, notes, and snippets.

@observerss
Last active January 4, 2016 07:59
Show Gist options
  • Save observerss/8592091 to your computer and use it in GitHub Desktop.
Save observerss/8592091 to your computer and use it in GitHub Desktop.
glowing's job puzzle
S = ' RBB' 'RRBB' 'RRBB' 'RRBB'
E = 'RRBB' 'RBBB' 'R RB' 'RRBB'
T = ' BRB' 'BRBR' 'RBRB' 'BRBR'
def solve_puzzle(A, B):
visited = set([A])
tried = [(A, '')]
pos = 0
while True:
end = len(tried)
for j in range(pos, end):
C, p = tried[j]
i = C.index(' ')
for d in 'LRUD':
N = list(C)
if d == 'L' and i not in [0, 4, 8, 12]:
N[i], N[i-1] = N[i-1], N[i]
elif d == 'R' and i not in [3, 7, 11, 15]:
N[i], N[i+1] = N[i+1], N[i]
elif d == 'U' and i not in [0, 1, 2, 3]:
N[i], N[i-4] = N[i-4], N[i]
elif d == 'D' and i not in [12, 13, 14, 15]:
N[i], N[i+4] = N[i+4], N[i]
else:
continue
N = ''.join(N)
if N not in visited:
if N == B:
return p+d
visited.add(N)
tried.append((N, p+d))
pos = end
print solve_puzzle(S, E)
print solve_puzzle(S, T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment