-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4a.py
42 lines (33 loc) · 1 KB
/
day4a.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
import re
count = 0
data = []
# read file and count rows
with open('data/day4.txt') as file:
for line in file:
count+= len(re.findall(r"XMAS", line))
count+= len(re.findall(r"SAMX", line))
data.append(list(line.rstrip('\n')))
# count cols
for i in range(len(data[0])):
col = "".join([row[i] for row in data])
count+= len(re.findall(r"XMAS", col))
count+= len(re.findall(r"SAMX", col))
# get diagonals
max_col = len(data[0])
max_row = len(data)
fdiag = [[] for _ in range(max_row + max_col - 1)]
bdiag = [[] for _ in range(len(fdiag))]
min_bdiag = -max_row + 1
for x in range(max_col):
for y in range(max_row):
fdiag[x+y].append(data[y][x])
bdiag[x-y-min_bdiag].append(data[y][x])
for line in fdiag:
str = "".join(line)
count+= len(re.findall(r"XMAS", str))
count+= len(re.findall(r"SAMX", str))
for line in bdiag:
str = "".join(line)
count+= len(re.findall(r"XMAS", str))
count+= len(re.findall(r"SAMX", str))
print(count)