diff --git a/day_04/program.py b/day_04/program.py index 50822b8..4535839 100755 --- a/day_04/program.py +++ b/day_04/program.py @@ -47,11 +47,32 @@ def get_xmas_appearances(grid: list) -> int: for col in range(len(grid[0])): for direction in directions: if check_xmas_from_pos(grid, row, col, direction, xmas_letters): - print(f"Found XMAS at position ({row}, {col}) in direction {direction}") + # print(f"Found XMAS at position ({row}, {col}) in direction {direction}") xmas_appearances += 1 return xmas_appearances +def check_cross(grid: list, row: int, col: int) -> bool: + a1 = grid[row - 1][col - 1] + a2 = grid[row + 1][col + 1] + b1 = grid[row - 1][col + 1] + b2 = grid[row + 1][col - 1] + if (a1 == "M" and a2 == "S") or (a1 == "S" and a2 == "M"): + if (b1 == "M" and b2 == "S") or (b1 == "S" and b2 == "M"): + return True + return False + + +def get_cross_mas_appearances(grid: list) -> int: + cross_mas_appearances = 0 + for row in range(1, len(grid) - 1): + for col in range(1, len(grid[0]) - 1): + if grid[row][col] == "A": + if check_cross(grid, row, col): + cross_mas_appearances += 1 + return cross_mas_appearances + + def main(): # lines = get_lines("sample-input.txt") lines = get_lines("input.txt") @@ -59,6 +80,9 @@ def main(): grid = get_grid(lines) xmas_appearances = get_xmas_appearances(grid) print(f"Part 1: The word XMAS appears {xmas_appearances} times.") + cross_mas_appearances = get_cross_mas_appearances(grid) + print(f"Part 2: The cross-MAS pattern appears {cross_mas_appearances} times.") + if __name__ == '__main__': main()