-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.py
66 lines (42 loc) · 1.41 KB
/
04.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
60
61
62
63
64
65
66
from aoc.utilities.fetch import get_input
from aoc.utilities.decorators import solution
FIRST_PATTERN = "XMAS"
SECOND_PATTERNS = ["MAS", "SAM"]
@solution
def solve_first(data):
n, m, ans = len(data), len(data[0]), 0
for i in range(n):
for j in range(m):
ans += check_first(i, j, data)
print(ans)
def check_first(i, j, data):
deltas = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1)]
return sum(map(lambda x: check_first_internal(data, i, j, x[0], x[1], 0), deltas))
def check_first_internal(data, i, j, di, dj, level):
if not (0 <= i < len(data) and 0 <= j < len(data[0])):
return 0
if data[i][j] != FIRST_PATTERN[level]:
return 0
if level == 3:
return 1
return check_first_internal(data, i + di, j + dj, di, dj, level + 1)
@solution
def solve_second(data):
n, m, ans = len(data), len(data[0]), 0
for i in range(n - 2):
for j in range(m - 2):
ans += check_second(i, j, data)
print(ans)
def check_second(i, j, data):
deltas = [(1, 1), (-1, 1)]
starts = [(i, j), (i + 2, j)]
for (di, dj), (si, sj) in zip(deltas, starts):
word = ""
for x in range(3):
word += data[si + di * x][sj + dj * x]
if word not in SECOND_PATTERNS:
return 0
return 1
data = list(map(list, get_input(4).splitlines()))
solve_first(data)
solve_second(data)