#!/usr/bin/env python3 import sys def parse(): G = [] guard = (None, None) r = 0 for l in sys.stdin: G.append([x for x in l.strip()]) if "^" in l: guard = (r, l.index("^")) r += 1 if not guard[0]: raise Exception("Unable to find Guard") return G, guard def turn(dir): if dir == "^": return ">" elif dir == ">": return "v" elif dir == "v": return "<" elif dir == "<": return "^" else: raise Exception(f"Bad direction {dir}") def move(G, guard, dir): if dir == "^": next = (guard[0] - 1, guard[1]) elif dir == "v": next = (guard[0] + 1, guard[1]) elif dir == ">": next = (guard[0], guard[1] + 1) elif dir == "<": next = (guard[0], guard[1] - 1) else: raise Exception(f"Bad direction {dir}") if next[0] >= len(G) or next[0] < 0 or next[1] >= len(G[0]) or next[1] < 0: # off the map return ((None, None), None) if G[next[0]][next[1]] == "#": dir = turn(dir) return (guard, dir) return (next, dir) def pprint(G, guard, dir): for i in range(len(G)): for j in range(len(G[0])): if (i, j) == guard: print(dir, end='') else: print(G[i][j], end='') print("") if __name__ == '__main__': G, guard = parse() dir = "^" seen = set() while guard[0] is not None: seen.add(guard) guard, dir = move(G, guard, dir) print(len(seen))