tangled
alpha
login
or
join now
nove.dev
/
aoc-2024
0
fork
atom
retroactive, to derust my rust
0
fork
atom
overview
issues
pulls
pipelines
day4 part2
nove.dev
3 months ago
5214edf7
b2c9b6c0
+59
-7
1 changed file
expand all
collapse all
unified
split
src
lib.rs
+59
-7
src/lib.rs
···
178
178
count.to_string()
179
179
}
180
180
181
181
+
pub fn day4_part2(input: &str) -> String{
182
182
+
let input = string_to_array(input);
183
183
+
let mut count = 0;
184
184
+
for row in 1..input.len()-1 {
185
185
+
for col in 1..input.len()-1 {
186
186
+
if is_x_mas(&input, row, col) {
187
187
+
count += 1;
188
188
+
}
189
189
+
}
190
190
+
}
191
191
+
count.to_string()
192
192
+
}
193
193
+
194
194
+
/* S&M combinations for X-MAS
195
195
+
MS
196
196
+
MS
197
197
+
198
198
+
SM
199
199
+
SM
200
200
+
201
201
+
MM
202
202
+
SS
203
203
+
204
204
+
SS
205
205
+
MM */
206
206
+
//precondition: row and col are one away from the edge
207
207
+
fn is_x_mas(input: &Vec<Vec<char>>, row: usize, col: usize) -> bool {
208
208
+
if input[row][col] != 'A' {
209
209
+
return false;
210
210
+
}
211
211
+
if input[row-1][col-1] == 'M' {
212
212
+
if input[row-1][col+1] == 'M' {
213
213
+
input[row+1][col+1] == 'S' && input[row+1][col-1] == 'S'
214
214
+
} else if input[row+1][col-1] == 'M' {
215
215
+
input[row+1][col+1] == 'S' && input[row-1][col+1] == 'S'
216
216
+
} else {
217
217
+
false
218
218
+
}
219
219
+
} else if input[row-1][col-1] == 'S' {
220
220
+
if input[row-1][col+1] == 'S' {
221
221
+
input[row+1][col+1] == 'M' && input[row+1][col-1] == 'M'
222
222
+
} else if input[row+1][col-1] == 'S' {
223
223
+
input[row+1][col+1] == 'M' && input[row-1][col+1] == 'M'
224
224
+
} else {
225
225
+
false
226
226
+
}
227
227
+
} else {
228
228
+
false
229
229
+
}
230
230
+
231
231
+
}
232
232
+
181
233
//squares only
182
234
fn rotate_string_45degrees_counterclockwise(input: &str) -> String {
183
235
let array_input = string_to_array(input);
···
305
357
assert_eq!(result, "2493");
306
358
}
307
359
308
308
-
// #[test]
309
309
-
// fn day4_part2_test() {
310
310
-
// let test_result = day4::day4_part2(include_str!("../input/day4_part2.test.txt"));
311
311
-
// assert_eq!(test_result, "48");
312
312
-
// let result = day4::day4_part2(include_str!("../input/day4.txt"));
313
313
-
// assert_eq!(result, "62098619");
314
314
-
// }
360
360
+
#[test]
361
361
+
fn day4_part2_test() {
362
362
+
let test_result = day4::day4_part2(include_str!("../input/day4.test.txt"));
363
363
+
assert_eq!(test_result, "9");
364
364
+
let result = day4::day4_part2(include_str!("../input/day4.txt"));
365
365
+
assert_eq!(result, "1890");
366
366
+
}
315
367
316
368
#[test]
317
369
fn day3_part1_test() {