-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.py
More file actions
54 lines (43 loc) · 1.29 KB
/
day4.py
File metadata and controls
54 lines (43 loc) · 1.29 KB
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
### Advent of code - Day 4 ###
INPUT_FILENAME = 'day4_input.txt'
def find_X(matrix:list) -> list:
result = []
for i, row in enumerate(matrix):
result+= [(i,j) for j, item in enumerate(row) if item == 'X']
return result
def build_matrix() -> list:
matrix = []
with open(INPUT_FILENAME, 'r') as file:
for line in file:
matrix.append([x for x in line if x != '\n'])
return matrix
def is_out_of_range(start:tuple, inc:tuple, size:int, rows:int) -> bool:
return (start[0]+3*inc[0] < 0) or (start[1]+3*inc[1] < 0) or (start[0]+3*inc[0] >= rows) or (start[1]+3*inc[1] >= size)
def count_xmas(matrix:list, start:tuple) -> int:
result = 0
directions = [
(-1,0), # N
(-1,1), # NE
(0,1), # E
(1,1), # SE
(1,0), # S
(1,-1), # SW
(0,-1), # W
(-1,-1) # NW
]
for dir in directions:
if is_out_of_range(start, dir, len(matrix[0]), len(matrix)):
continue
if matrix[start[0]+dir[0]][start[1]+dir[1]] == 'M' and \
matrix[start[0]+2*dir[0]][start[1]+2*dir[1]] == 'A' and \
matrix[start[0]+3*dir[0]][start[1]+3*dir[1]] == 'S':
result += 1
return result
def main():
result = 0
input = build_matrix()
x_list = find_X(input)
for x in x_list:
result+=count_xmas(input,x)
print(f"XMAS={result}")
main()