Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
14-liner: simple solution in Clear category for Forgetful Prisoner by przemyslaw.daniel
def find_path(scanner, memory):
neighbors = {"S": 1, "N": -1, "E": 1j, "W": -1j}
bin2dir = {'00': 'S', '01': 'N', '10': 'E', '11': 'W'}
memory, dir2bin = format(memory, '0100b'), dict(map(reversed, bin2dir.items()))
path = [bin2dir[memory[6:][2*x:2*x+2]] for x in range(47)]
length, visited, pos = int(memory[:6], 2), dict(), 0
for d in path[:length]:
pos -= neighbors[d]
visited[pos] = visited.get(pos, 0)+1
move = [(k, visited.get(neighbors[k], 0)) for k, v in scanner.items() if v > 0]
direct, _ = sorted(move, key=lambda x: x[1])[0]
length = length+1 if length < 47 else 47
memory = ''.join([format(length, '06b')]+[dir2bin[x] for x in [direct]+path[:46]])
return direct, int(memory, 2)
March 10, 2017
Comments: