diff --git a/2024/day4/main.py b/2024/day4/main.py new file mode 100644 index 0000000..37b4d95 --- /dev/null +++ b/2024/day4/main.py @@ -0,0 +1,39 @@ +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) + diff --git a/2024/day4/test.text b/2024/day4/test.text new file mode 100644 index 0000000..c41c5ea --- /dev/null +++ b/2024/day4/test.text @@ -0,0 +1,10 @@ +MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX \ No newline at end of file