seeing this puzzle in glowing's jobs page
https://glowing.com/jobs/mobiledeveloper#mtop
here's my solution
seeing this puzzle in glowing's jobs page
https://glowing.com/jobs/mobiledeveloper#mtop
here's my solution
| 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) |