-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_4.py
160 lines (134 loc) · 4.21 KB
/
day_4.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from datetime import datetime
time_start = datetime.now()
with open('day_4.input', 'r') as file:
matrix = file.readlines()
width = len(matrix[0])
height = len(matrix)
word = ['X', 'M', 'A', 'S']
mark = [["." for j in range(width - 1)] for i in range(height)]
def traverse_top(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_top(x, y - 1, dept + 1)
return False
def traverse_top_right(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_top_right(x + 1, y - 1, dept + 1)
return False
def traverse_right(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_right(x + 1, y, dept + 1)
return False
def traverse_bottem_right(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_bottem_right(x + 1, y + 1, dept + 1)
return False
def traverse_bottem(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_bottem(x, y + 1, dept + 1)
return False
def traverse_bottem_left(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_bottem_left(x - 1, y + 1, dept + 1)
return False
def traverse_left(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_left(x - 1, y, dept + 1)
return False
def traverse_top_left(x, y, dept):
if check_bounds(x, y):
return False
if matrix[y][x] == word[dept]:
if dept == len(word) - 1:
# mark[y][x] = "S"
return True
return traverse_top_left(x - 1, y - 1, dept + 1)
return False
def check_bounds(x, y):
return x < 0 or x > width - 1 or y < 0 or y > height - 1
def part1():
total = 0
for y in range(height):
for x in range(width):
# Search in all 8 directions
if traverse_top(x, y, 0):
total += 1
if traverse_top_right(x, y, 0):
total += 1
if traverse_right(x, y, 0):
total += 1
if traverse_bottem_right(x, y, 0):
total += 1
if traverse_bottem(x, y, 0):
total += 1
if traverse_bottem_left(x, y, 0):
total += 1
if traverse_left(x, y, 0):
total += 1
if traverse_top_left(x, y, 0):
total += 1
print(total)
part1()
# print(matrix)
def search_x_mas(x, y):
if matrix[y - 1][x - 1] == "M":
if matrix[y - 1][x + 1] == "M":
return matrix[y + 1][x + 1] == "S" and matrix[y + 1][x - 1] == "S"
if matrix[y + 1][x - 1] == "M":
return matrix[y + 1][x + 1] == "S" and matrix[y - 1][x + 1] == "S"
return False
if matrix[y - 1][x - 1] == "S":
if matrix[y - 1][x + 1] == "S":
return matrix[y + 1][x + 1] == "M" and matrix[y + 1][x - 1] == "M"
if matrix[y + 1][x - 1] == "S":
return matrix[y + 1][x + 1] == "M" and matrix[y - 1][x + 1] == "M"
return False
return False
def part2():
total = 0
for y in range(1, height - 1):
for x in range(1, width - 1):
if matrix[y][x] == "A":
if search_x_mas(x, y):
total += 1
print(total)
part2()
#
# for row in mark:
# print(" ".join(map(str, row)))
time_end = datetime.now()
print("Total time: ", time_end - time_start)