import math text: str with open("test.txt") as f: text = f.read() lines: list[str] = text.splitlines() lines: list[int] = [[int(value) if value != "." else 11 for value in line] for line in lines] print(lines) total: int = 0 def trailhead_tester(ground: list[int], pos_y: int, pos_x: int, ground_y: int, ground_x: int): if ground[pos_y][pos_x] == 9: return 1 min_x: int = max(pos_x - 1, 0) max_x: int = min(pos_x + 1, ground_x) min_y: int = max(pos_y - 1, 0) max_y: int = min(pos_y + 1, ground_y) for y, line in enumerate(ground[min_y:max_y]): for x, value in enumerate(line[min_x:max_x]): if ground[pos_y][pos_x] + 1 == value: return trailhead_tester(ground, pos_y + (max_y - min_y) - 2, pos_x + (max_x - min_x) - 2, ground_x, ground_y) return 0 y_max = len(lines) x_max = len(lines[0]) for y, line in enumerate(lines): for x, value in enumerate(line): if (value == 0): total += trailhead_tester(lines, y, x, y_max, x_max) print(total)