#!/usr/bin/env python3 import sys input_file = sys.argv[1] with open(input_file) as fd: lines = [line.rstrip() for line in fd.readlines()] height = len(lines) width = len(lines[0]) for i in range(height): if "^" in lines[i]: j = lines[i].index("^") break d = 0 directions = [ (-1, 0), # ^ (0, 1), # > (1, 0), # v (0, -1), # < ] vis = [[False] * width for h in range(height)] while True: print(i, j) vis[i][j] = True ii, jj = i + directions[d][0], j + directions[d][1] if ii not in range(height) or jj not in range(width): break if lines[ii][jj] == "#": d += 1 d %= len(directions) continue i, j = ii, jj count = 0 for i in range(height): for j in range(width): if vis[i][j]: count += 1 print(count)