-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.py
182 lines (151 loc) · 5.12 KB
/
day4.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import re
matrix_list = []
total_result = 0
regex_expression = r"(?=(XMAS))|(?=(SAMX))"
part2_regex_expression = r"(?=(MAS))|(?=(SAM))"
test = [["XMAS"], ["SAMX"], ["JOAO"], ["LAPA"]]
example = [
"MMMSXXMASM",
"MSAMXMSMSA",
"AMXSXMAAMM",
"MSAMASMSMX",
"XMASAMXAMM",
"XXAMMXXAMA",
"SMSMSASXSS",
"SAXAMASAAA",
"MAMMMXMMMM",
"MXMXAXMASX",
]
working_list = []
with open("./day4/input", "r") as file:
for line in file:
matrix_list.append(list(map(str, line.rstrip())))
def clean_up(input_list):
for line in input_list:
working_list.append(list(map(str, line)))
def look_results(clean_list):
result = 0
for idx1 in range(1, len(clean_list) - 1, 1):
for idx2 in range(1, len(clean_list[idx1]) - 1, 1):
value = clean_list[idx1][idx2]
if value == "A":
if (
clean_list[idx1 - 1][idx2 - 1] == "M"
and clean_list[idx1 + 1][idx2 + 1] == "S"
):
if (
clean_list[idx1 - 1][idx2 + 1] == "M"
and clean_list[idx1 + 1][idx2 - 1] == "S"
):
result += 1
if (
clean_list[idx1 - 1][idx2 + 1] == "S"
and clean_list[idx1 + 1][idx2 - 1] == "M"
):
result += 1
if (
clean_list[idx1 - 1][idx2 - 1] == "S"
and clean_list[idx1 + 1][idx2 + 1] == "M"
):
if (
clean_list[idx1 - 1][idx2 + 1] == "M"
and clean_list[idx1 + 1][idx2 - 1] == "S"
):
result += 1
if (
clean_list[idx1 - 1][idx2 + 1] == "S"
and clean_list[idx1 + 1][idx2 - 1] == "M"
):
result += 1
return result
print(look_results(matrix_list))
def rotate_list_90d(input_list):
result_list = []
for line in input_list:
split_string = [c for c in line[0]]
result_list.append(split_string)
return [["".join(row)] for row in zip(*reversed(result_list))]
def rotate_list_45d(
direction: str, input_list: list, input_matrix: list = []
) -> dict[str, list]:
result_list = [[] for _ in range(len(input_list) * 2 - 1)]
matrix_output = [[] for _ in range(len(input_list) * 2 - 1)]
for idx1, h_line in enumerate(input_list):
line = h_line[0]
f_line = line[::-1] if direction == "ccw" else line
for idx2, c in enumerate(f_line):
result_list[idx2 + idx1].append(c)
if len(input_matrix):
matrix_output[idx2 + idx1].append(input_matrix[idx1][idx2])
if direction == "ccw":
formatted_list = [["".join(row)] for row in result_list]
else:
formatted_list = [["".join(row)[::-1]] for row in result_list]
return {"result": formatted_list, "matrix": matrix_output}
def create_grid_pattern(input_list: list) -> list:
result = []
for idx1, line in enumerate(input_list):
line_string: str = line[0]
block = []
for idx2 in range(len(line_string)):
block.append({"r": idx1, "c": idx2})
result.append(block)
return result
# [
# ['XMAS'],
# ['SAMX'],
# ['JOAO'],
# ['LAPA']
# ]
#
# [
# ['S'],
# ['AX'],
# ['MMO'],
# ['XAAA'],
# ['SOP'],
# ['JA'],
# ['L']
# ]
# [
# ['X'],
# ['SM'],
# ['JAA'],
# ['LOMS'],
# ['AAX'],
# ['PO'],
# ['A']
# ]
def find_elements_in_list(input_list: list) -> int:
result = 0
for h_line in input_list:
found = re.findall(regex_expression, h_line[0])
result += len(found)
return result
def find_elements_iter(input_list: list):
iter_result = []
for idx, h_line in enumerate(input_list):
iter_result.append([])
for m in re.finditer(part2_regex_expression, h_line[0]):
g1_index = m.span(1)[0]
g2_index = m.span(2)[0]
if g1_index != -1:
iter_result[idx].append(g1_index + 1)
elif g2_index != -1:
iter_result[idx].append(g2_index + 1)
return iter_result
# example_grid_pattern = create_grid_pattern(example)
# rotated_list_45_ccw = rotate_list_45d("ccw", example, example_grid_pattern)
# rotated_list_45_cw = rotate_list_45d("cw", example, example_grid_pattern)
# find_indexes_1 = find_elements_iter(rotated_list_45_ccw["result"])
# print(find_indexes_1)
# find_indexes_2 = find_elements_iter(rotated_list_45_cw["result"])
# print(find_indexes_2)
# return elements from list in original position
# total_result += find_elements_in_list(matrix_list)
# rotate the original list 90 degrees and find results
# total_result += find_elements_in_list(rotate_list_90d(matrix_list))
# rotate the original list 45 degrees and find results
# total_result += find_elements_in_list(rotate_list_45d("ccw", matrix_list))
# total_result += find_elements_in_list(rotate_list_45d("cw", matrix_list))
# print(total_result)