I was trying, just for fun, to solve http://code.google.com/codejam/contest/dashboard?c=2075486#s=p4
I can easily solve the small set, but I struggle to solve the big one (my implementation would use approx. 30GB or RAM, which is a bit too much. Any hint or direction is appreciated!
def solve_maze(case_no):
case = cases[case_no]
case_no += 1
edges = case['edges']
#quick and dirty check. Can we reach the goal?
if case['goal'] not in sum([i.values() for i in edges.values()],[]):
return "Case #%s: Infinity" % case_no
last_position = 1
l = 0
states = set()
memory = [1 for i in range(1,1+case['goal'])]
while not last_position==case['goal']:
direction = memory[last_position]
memory[last_position] *= -1
move = edges[last_position][direction]
states.add(hash((move,tuple(memory))))
l+=1
if len(states) != l:
return "Case #%s: Infinity" % case_no
last_position = move
else:
return "Case #%s: %d" % (case_no,len(states))
with open("/xxx/test-small.in","rb") as f:
inp = f.read()[:-1].split("\n")
N = int(inp[0])
cases = []
it = 0
for line in inp[1:]:
if line.find(" ") == -1:
#print int(line)
cases.append({"goal":int(line),"edges":{}})
it = 0
else:
it+=1
cases[-1]['edges'][it] = dict(zip([1,-1],map(int,line.split())))
for i in range(len(cases)):
print solve_maze(i)