diff options
Diffstat (limited to '2024/day4/solve2.py')
-rw-r--r-- | 2024/day4/solve2.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/2024/day4/solve2.py b/2024/day4/solve2.py new file mode 100644 index 0000000..8866fc0 --- /dev/null +++ b/2024/day4/solve2.py @@ -0,0 +1,33 @@ +def is_xmas(lines: list[str], row_idx: int, col_idx: int) -> bool: + if lines[row_idx][col_idx] != 'A': + return False + + if row_idx == 0 or col_idx == 0 or row_idx == len(lines) - 1 or col_idx == len(lines[0]) - 1: + return False + + found_mas_count = 0 + for row_step in range(-1, 2, 2): + for column_step in range(-1, 2, 2): + current_corner = lines[row_idx + row_step][col_idx + column_step] + if current_corner == 'M': + if lines[row_idx - row_step][col_idx - column_step] == 'S': + found_mas_count += 1 + else: + return False + elif current_corner != 'S': + return False + + return found_mas_count == 2 + +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)): + if is_xmas(data, row, col): + result += 1 + +print(result)
\ No newline at end of file |