2024-12-03 23:27:18 -06:00

40 lines
867 B
Python

import sys
with open('test.text', 'r') as file:
data: list[str] = file.readlines()
y_bound = len(data)
x_bound = len(data[0])
# Part 1
def search_all_directions(s: str, x: int, y: int):
print(s)
if s != 'X' and s != 'XM' and s != 'XMA' and s != 'XMAS':
return 0
if x < 0 or y < 0 or x >= x_bound or y >= y_bound:
return 0
# print(x, y)
if data[y][x] != s:
s = s + data[y][x]
print(s)
if s == 'XMAS':
return 1
return (search_all_directions(s, y, x - 1)
+ search_all_directions(s, y, x + 1)
+ search_all_directions(s, y + 1, x)
+ search_all_directions(s, y - 1, x)
)
total = 0
for y, line in enumerate(data):
for x, char in enumerate(line):
if char == 'X':
total += search_all_directions(char, x, y)
print(total)