diff --git a/2024/day04/part1.py b/2024/day04/part1.py new file mode 100644 index 0000000..9f4a846 --- /dev/null +++ b/2024/day04/part1.py @@ -0,0 +1,32 @@ +import re + +text: str + +with open("input.txt") as f: + text = f.read() + +lines: list[str] = text.splitlines() + +def search(values: list[list[str]], word: str, coord_x: int, coord_y: int, inc_x: int, inc_y: int): + + array_length = len(values) + line_length = len(values[0]) + + for i in range(len(word)): + x: int = coord_x + i * inc_x + y: int = coord_y + i * inc_y + if (x >= line_length or x < 0) or (y >= array_length or y < 0) or (values[y][x] != word[i]): + return 0 + return 1 + +total: int = 0 + +directions = [(-1, -1), (0, -1), (+1, -1), (+1, 0), (+1, +1), (0, +1), (-1, +1), (-1, 0)] + +for y, line in enumerate(lines): + for x, c in enumerate(line): + if (c == 'X'): + for inc_x, inc_y in directions: + total += search(lines, "XMAS", x, y, inc_x, inc_y) + +print(total) \ No newline at end of file