Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grid=[list(x) for x in open('input').readlines()]
- hash = {(i, j) for i in range(len(grid)) for j in range(len(grid[i])) if grid[i][j] == '#'}
- dirs = { (0, 1): (1, 0), (1, 0): (0, -1), (0, -1): (-1, 0), (-1, 0): (0, 1) }
- start = next((i, j) for i in range(len(grid)) for j in range(len(grid[i])) if grid[i][j] == '^')
- pos, dir, seen = start, (-1,0), set()
- while True:
- seen.add(pos)
- np = (pos[0] + dir[0], pos[1] + dir[1])
- if not (0 <= np[0] < len(grid) and 0 <= np[1] < len(grid[0])):
- break
- if np in hash:
- dir = dirs[dir]
- else:
- pos = np
- loop=0
- for s in seen:
- hashnew=hash | {s}
- pos, dir, old = start, (-1,0), set()
- while True:
- old.add((pos,dir))
- np = (pos[0] + dir[0], pos[1] + dir[1])
- if not (0 <= np[0] < len(grid) and 0 <= np[1] < len(grid[0])):
- break
- if np in hashnew:
- dir = dirs[dir]
- else:
- pos = np
- if (np, dir) in old:
- loop+=1
- break
- print(len(seen))
- print(loop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement