-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday04.py
executable file
·61 lines (42 loc) · 1.21 KB
/
day04.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2024, 4)
fin = advent.get_input()
grid = read_char_matrix(fin)
ans1 = ans2 = 0
def direc(grid, r, c, dr, dc):
word = ''
rr, cc = r, c
for i in range(4):
rr, cc = r + i * dr, c + i * dc
if rr < 0 or cc < 0 or rr >= len(grid) or cc >= len(grid[0]):
continue
word += grid[rr][cc]
return word == 'XMAS'
def gridchar(grid, r, c):
if r < 0 or c < 0 or r >= len(grid) or c >= len(grid[0]):
return ''
return grid[r][c]
def mas(grid, r, c):
a = gridchar(grid, r - 1, c - 1)
a += gridchar(grid, r, c)
a += gridchar(grid, r + 1, c + 1)
b = gridchar(grid, r + 1, c - 1)
b += gridchar(grid, r, c)
b += gridchar(grid, r - 1, c + 1)
return a in ('MAS', 'SAM') and b in ('MAS', 'SAM')
for r, row in enumerate(grid):
for c, char in enumerate(row):
ans1 += direc(grid, r, c, 0, 1) \
+ direc(grid, r, c, 0, -1) \
+ direc(grid, r, c, 1, 0) \
+ direc(grid, r, c, -1, 0) \
+ direc(grid, r, c, 1, 1) \
+ direc(grid, r, c, 1, -1) \
+ direc(grid, r, c, -1, 1) \
+ direc(grid, r, c, -1, -1)
advent.print_answer(1, ans1)
for r, row in enumerate(grid):
for c, char in enumerate(row):
ans2 += mas(grid, r, c)
advent.print_answer(2, ans2)