Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Binary & Complex solution in Speedy category for Forgetful Prisoner by DiZ
def find_path(scanner, memory, mem_size=100):
from random import choice
if not memory: memory = 3
origin = memory % 4 - 2 & 3 # LSB
max_size = memory & (1 << mem_size - 1) # MSB
def search(memory):
# Extract information
visit_counter = {1j ** d: 0 for d in allowed_moves}
current, mem_copy = 0, memory
while mem_copy > 3:
current -= 1j ** (mem_copy & 3)
if current in visit_counter:
visit_counter[current] += 1
mem_copy >>= 2
# Choose best move
less_visited = min(visit_counter.values())
best_moves = [d for d in allowed_moves if visit_counter[1j ** d] == less_visited]
return choice(best_moves)
allowed_moves = [d for d in range(4) if scanner["SENW"[d]] and d != origin]
if not allowed_moves:
direction = origin # Dead end
elif len(allowed_moves) == 1:
direction = allowed_moves[0] # Straight line
else:
direction = search(memory)
return "SENW"[direction], max_size | (memory << 2 | direction) & max_size - 1
Nov. 6, 2014
Comments: