summaryrefslogtreecommitdiff
path: root/2024/day4/solve2.py
blob: 8866fc0cb5462a64498f9787830cd167af0cf754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)