diff options
author | Botond Hende <nettingman@gmail.com> | 2024-12-11 10:18:23 +0100 |
---|---|---|
committer | Botond Hende <nettingman@gmail.com> | 2024-12-11 10:18:23 +0100 |
commit | 0a5c1f77d01fc0bd166494787f24562e2fd3a9e9 (patch) | |
tree | 99b0d38ef86653b6ff4aae26381ebdf0895f829d /2024/day04/solve.py | |
parent | cbf5c348db4a693b15e455a23e07072587edf4b0 (diff) |
renamed day folders to two digit format
Diffstat (limited to '2024/day04/solve.py')
-rw-r--r-- | 2024/day04/solve.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/2024/day04/solve.py b/2024/day04/solve.py new file mode 100644 index 0000000..da301c9 --- /dev/null +++ b/2024/day04/solve.py @@ -0,0 +1,43 @@ +def check_direction(lines: list[str], row_idx: int, col_idx: int, row_step: int, column_step: int) -> bool: + column_count = len(lines[0]) + row_count = len(lines) + for step, letter in enumerate("MAS"): + current_row = row_idx + (step + 1) * row_step + current_col = col_idx + (step + 1) * column_step + if current_row >= row_count or current_row < 0: + return False + + if current_col >= column_count or current_col < 0: + return False + + if lines[current_row][current_col] != letter: + return False + + return True + +def get_current_pos_words(lines: list[str], row_idx: int, col_idx: int) -> int: + if lines[row_idx][col_idx] != 'X': + return 0 + + count = 0 + for row_step in range(-1, 2): + for column_step in range(-1, 2): + if row_step == 0 and column_step == 0: + continue + + if check_direction(lines, row_idx, col_idx, row_step, column_step): + count += 1 + + return count + +data = [] +with open("input") as f: + for line in f: + data.append(line.strip()) + +result = 0 +for row, line in enumerate(data): + for col in range(len(line)): + result += get_current_pos_words(data, row, col) + +print(result)
\ No newline at end of file |