-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (38 loc) · 858 Bytes
/
main.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
grid = open('input').read().splitlines()
height = len(grid)
width = len(grid[0])
def valid(x, y):
return 0 <= x and x < width and 0 <= y and y < height
def count1(x, y):
if grid[y][x] != 'X':
return 0
c = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if not valid(x + 3 * dx, y + 3 * dy):
continue
word = ''
for i in range(4):
word += grid[y + i * dy][x + i * dx]
if word == 'XMAS':
c += 1
return c
# 2397
sum = 0
for x in range(width):
for y in range(height):
sum += count1(x, y)
print(sum)
def count2(x, y):
if grid[y][x] != 'A':
return 0
word = grid[y - 1][x - 1] + grid[y - 1][x + 1] + grid[y + 1][x + 1] + grid[y + 1][x - 1]
if word in ['MMSS', 'MSSM', 'SMMS', 'SSMM']:
return 1
return 0
# 1824
sum = 0
for x in range(1, width - 1):
for y in range(1, height - 1):
sum += count2(x, y)
print(sum)