68 lines
1.4 KiB
Python
68 lines
1.4 KiB
Python
#!/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_start in range(height):
|
|
if "^" in lines[i_start]:
|
|
j_start = lines[i_start].index("^")
|
|
break
|
|
|
|
directions = [
|
|
(-1, 0), # ^
|
|
(0, 1), # >
|
|
(1, 0), # v
|
|
(0, -1), # <
|
|
]
|
|
|
|
positions: set[tuple[int, int]] = set()
|
|
|
|
i, j = i_start, j_start
|
|
d = 0
|
|
|
|
while True:
|
|
positions.add((i, j))
|
|
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
|
|
|
|
print(len(positions))
|
|
positions.remove((i_start, j_start))
|
|
|
|
tot = 0
|
|
for obstacle in positions:
|
|
i, j = i_start, j_start
|
|
d = 0
|
|
path: set[tuple[int, int, int]] = set()
|
|
|
|
while True:
|
|
state = (i, j, d)
|
|
if state in path:
|
|
loop = True
|
|
tot += 1
|
|
break
|
|
path.add(state)
|
|
ii, jj = i + directions[d][0], j + directions[d][1]
|
|
if ii not in range(height) or jj not in range(width):
|
|
loop = False
|
|
break
|
|
if lines[ii][jj] == "#" or (ii, jj) == obstacle:
|
|
d += 1
|
|
d %= len(directions)
|
|
continue
|
|
i, j = ii, jj
|
|
|
|
print(tot)
|