157 tokens in Python for part 2 by Abbas Moosajee
Download solution
map_dict_p2={}
forrow_no,row_datainenumerate(open(0)):
forcol_no,cellinenumerate(row_data):
ifcell=='S':
start_pos=row_no,col_no
map_dict_p2[row_no,col_no]=cell
fromfunctoolsimportlru_cache
@lru_cache()
defcount_from(pos,depth=0):
row,col=pos
depth+=1
cell=map_dict_p2.get(pos)
ifcellisNone:
return1
ifcellin'.S':
returncount_from((row+1,col),depth)
elifcell=='^':
left=count_from((row,col-1),depth)
right=count_from((row,col+1),depth)
returnleft+right
return0
print(count_from(start_pos))