1.py (1568B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 4 def parse(): 5 G = [] 6 guard = (None, None) 7 r = 0 8 for l in sys.stdin: 9 G.append([x for x in l.strip()]) 10 if "^" in l: 11 guard = (r, l.index("^")) 12 r += 1 13 14 if not guard[0]: 15 raise Exception("Unable to find Guard") 16 17 return G, guard 18 19 def turn(dir): 20 if dir == "^": 21 return ">" 22 elif dir == ">": 23 return "v" 24 elif dir == "v": 25 return "<" 26 elif dir == "<": 27 return "^" 28 else: 29 raise Exception(f"Bad direction {dir}") 30 31 def move(G, guard, dir): 32 if dir == "^": 33 next = (guard[0] - 1, guard[1]) 34 elif dir == "v": 35 next = (guard[0] + 1, guard[1]) 36 elif dir == ">": 37 next = (guard[0], guard[1] + 1) 38 elif dir == "<": 39 next = (guard[0], guard[1] - 1) 40 else: 41 raise Exception(f"Bad direction {dir}") 42 43 if next[0] >= len(G) or next[0] < 0 or next[1] >= len(G[0]) or next[1] < 0: 44 # off the map 45 return ((None, None), None) 46 47 if G[next[0]][next[1]] == "#": 48 dir = turn(dir) 49 return (guard, dir) 50 51 return (next, dir) 52 53 def pprint(G, guard, dir): 54 for i in range(len(G)): 55 for j in range(len(G[0])): 56 if (i, j) == guard: 57 print(dir, end='') 58 else: 59 print(G[i][j], end='') 60 print("") 61 62 if __name__ == '__main__': 63 G, guard = parse() 64 dir = "^" 65 66 seen = set() 67 while guard[0] is not None: 68 seen.add(guard) 69 guard, dir = move(G, guard, dir) 70 71 print(len(seen))