|
| 1 | +def check_for_xmas(puzzle, row, col): |
| 2 | + if puzzle[row][col] != 'X': |
| 3 | + return False |
| 4 | + num_occurrences = 0 |
| 5 | + # Horizontal backwards |
| 6 | + if col > 2: |
| 7 | + if ( |
| 8 | + puzzle[row][col-1] == 'M' |
| 9 | + and puzzle[row][col-2] == 'A' |
| 10 | + and puzzle[row][col-3] == 'S' |
| 11 | + ): |
| 12 | + num_occurrences += 1 |
| 13 | + # Vertical upwards |
| 14 | + if row > 2: |
| 15 | + if ( |
| 16 | + puzzle[row-1][col] == 'M' |
| 17 | + and puzzle[row-2][col] == 'A' |
| 18 | + and puzzle[row-3][col] == 'S' |
| 19 | + ): |
| 20 | + num_occurrences += 1 |
| 21 | + # Horizontal forwards |
| 22 | + if len(puzzle[row]) - col > 3: |
| 23 | + if ( |
| 24 | + puzzle[row][col+1] == 'M' |
| 25 | + and puzzle[row][col+2] == 'A' |
| 26 | + and puzzle[row][col+3] == 'S' |
| 27 | + ): |
| 28 | + num_occurrences += 1 |
| 29 | + # Vertical downwards |
| 30 | + if len(puzzle) - row > 3: |
| 31 | + if ( |
| 32 | + puzzle[row+1][col] == 'M' |
| 33 | + and puzzle[row+2][col] == 'A' |
| 34 | + and puzzle[row+3][col] == 'S' |
| 35 | + ): |
| 36 | + num_occurrences += 1 |
| 37 | + # Diagonal upwards left |
| 38 | + if row > 2 and col > 2: |
| 39 | + if ( |
| 40 | + puzzle[row-1][col-1] == 'M' |
| 41 | + and puzzle[row-2][col-2] == 'A' |
| 42 | + and puzzle[row-3][col-3] == 'S' |
| 43 | + ): |
| 44 | + num_occurrences += 1 |
| 45 | + # Diagonal upwards right |
| 46 | + if row > 2 and len(puzzle[row]) - col > 3: |
| 47 | + if ( |
| 48 | + puzzle[row-1][col+1] == 'M' |
| 49 | + and puzzle[row-2][col+2] == 'A' |
| 50 | + and puzzle[row-3][col+3] == 'S' |
| 51 | + ): |
| 52 | + num_occurrences += 1 |
| 53 | + # Diagonal downwards left |
| 54 | + if len(puzzle) - row > 3 and col > 2: |
| 55 | + if ( |
| 56 | + puzzle[row+1][col-1] == 'M' |
| 57 | + and puzzle[row+2][col-2] == 'A' |
| 58 | + and puzzle[row+3][col-3] == 'S' |
| 59 | + ): |
| 60 | + num_occurrences += 1 |
| 61 | + # Diagonal downwards right |
| 62 | + if len(puzzle) - row > 3 and len(puzzle[row]) - col > 3: |
| 63 | + if ( |
| 64 | + puzzle[row+1][col+1] == 'M' |
| 65 | + and puzzle[row+2][col+2] == 'A' |
| 66 | + and puzzle[row+3][col+3] == 'S' |
| 67 | + ): |
| 68 | + num_occurrences += 1 |
| 69 | + return num_occurrences |
| 70 | + |
| 71 | +def check_for_x_mas(puzzle, row, col): |
| 72 | + if puzzle[row][col] != 'A': |
| 73 | + return False |
| 74 | + if row < 1 or col < 1 or row > len(puzzle) - 2 or col > len(puzzle[row]) - 2: |
| 75 | + return False |
| 76 | + if ( |
| 77 | + ( |
| 78 | + (puzzle[row-1][col-1] == 'M' and puzzle[row+1][col+1] == 'S') |
| 79 | + or (puzzle[row-1][col-1] == 'S' and puzzle[row+1][col+1] == 'M') |
| 80 | + ) |
| 81 | + and |
| 82 | + ( |
| 83 | + (puzzle[row-1][col+1] == 'M' and puzzle[row+1][col-1] == 'S') |
| 84 | + or (puzzle[row-1][col+1] == 'S' and puzzle[row+1][col-1] == 'M') |
| 85 | + ) |
| 86 | + ): |
| 87 | + return True |
| 88 | + return False |
| 89 | + |
| 90 | +puzzle = [] |
| 91 | + |
| 92 | +with open('input.txt', 'r') as file: |
| 93 | + for line in file: |
| 94 | + puzzle.append(list(line.strip())) |
| 95 | + |
| 96 | +# Part 1 |
| 97 | +xmas_count = 0 |
| 98 | +for row in range(len(puzzle)): |
| 99 | + for col in range(len(puzzle[row])): |
| 100 | + xmas_count += check_for_xmas(puzzle, row, col) |
| 101 | +print('Number of XMAS occurrences:', xmas_count) |
| 102 | + |
| 103 | +# Part 2 |
| 104 | +x_mas_count = 0 |
| 105 | +for row in range(len(puzzle)): |
| 106 | + for col in range(len(puzzle[row])): |
| 107 | + x_mas_count += int(check_for_x_mas(puzzle, row, col)) |
| 108 | +print ('Number of X-MAS occurrences:', x_mas_count) |
0 commit comments