-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_2.py
59 lines (54 loc) · 1.89 KB
/
part_2.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def timesXMasAppears() -> int:
with open("./input.txt") as file:
content = file.read()
lines = content.strip().split("\n")
grid = [list(line) for line in lines]
count = 0
for i in range(1, len(grid) - 1):
for j in range(1, len(grid[0]) - 1):
if grid[i][j] == "A":
# Check pattern M.M
# .A.
# S.S
cond = (
grid[i - 1][j - 1] == "M"
and grid[i - 1][j + 1] == "M"
and grid[i + 1][j + 1] == "S"
and grid[i + 1][j - 1] == "S"
)
if cond:
count += 1
# Check pattern S.S
# .A.
# M.M
cond = (
grid[i - 1][j - 1] == "S"
and grid[i - 1][j + 1] == "S"
and grid[i + 1][j + 1] == "M"
and grid[i + 1][j - 1] == "M"
)
if cond:
count += 1
# Check pattern M.S
# .A.
# M.S
cond = (
grid[i - 1][j - 1] == "M"
and grid[i - 1][j + 1] == "S"
and grid[i + 1][j + 1] == "S"
and grid[i + 1][j - 1] == "M"
)
if cond:
count += 1
# Check pattern S.M
# .A.
# S.M
cond = (
grid[i - 1][j - 1] == "S"
and grid[i - 1][j + 1] == "M"
and grid[i + 1][j + 1] == "M"
and grid[i + 1][j - 1] == "S"
)
if cond:
count += 1
return count