# Created by Egor Kostan.
# GitHub: https://github.com/ikostan
# LinkedIn: https://www.linkedin.com/in/egor-kostan/
from kyu_3.line_safari_is_that_a_line.walker_class import Walker
[docs]def line(grid: list) -> bool:
"""
You are given a grid.
You simply need to return true/false if you
can detect a one and only one "valid" line joining those points.
:param grid: which always includes exactly two end-points indicated by X
:return: true/false
"""
if x_counter(grid) != 2:
return False
if not assert_x_has_rout(grid):
return False
walker = Walker(grid)
while not walker.is_done:
walker.move()
if walker.position == 'X':
return True
return False
[docs]def assert_x_has_rout(grid: list) -> bool:
counter = 0
for row_i, row in enumerate(grid):
if counter == 2:
break
for col_i, char in enumerate(row):
if char == 'X':
temp = list()
# up
if row_i - 1 >= 0 and grid[row_i - 1][col_i] in 'X|+':
temp.append(True)
# down
if row_i + 1 < len(grid) and grid[row_i + 1][col_i] in 'X|+':
temp.append(True)
# left
if col_i - 1 >= 0 and grid[row_i][col_i - 1] in 'X+-':
temp.append(True)
# right
if col_i + 1 < len(grid[row_i]) and grid[row_i][col_i + 1] in 'X+-':
temp.append(True)
if len(temp) != 1:
return False
return True
[docs]def x_counter(grid: list) -> int:
counter = 0
for row in grid:
counter += row.count('X')
return counter