retroactive, to derust my rust

day4 part2

+59 -7
+59 -7
src/lib.rs
··· 178 178 count.to_string() 179 179 } 180 180 181 + pub fn day4_part2(input: &str) -> String{ 182 + let input = string_to_array(input); 183 + let mut count = 0; 184 + for row in 1..input.len()-1 { 185 + for col in 1..input.len()-1 { 186 + if is_x_mas(&input, row, col) { 187 + count += 1; 188 + } 189 + } 190 + } 191 + count.to_string() 192 + } 193 + 194 + /* S&M combinations for X-MAS 195 + MS 196 + MS 197 + 198 + SM 199 + SM 200 + 201 + MM 202 + SS 203 + 204 + SS 205 + MM */ 206 + //precondition: row and col are one away from the edge 207 + fn is_x_mas(input: &Vec<Vec<char>>, row: usize, col: usize) -> bool { 208 + if input[row][col] != 'A' { 209 + return false; 210 + } 211 + if input[row-1][col-1] == 'M' { 212 + if input[row-1][col+1] == 'M' { 213 + input[row+1][col+1] == 'S' && input[row+1][col-1] == 'S' 214 + } else if input[row+1][col-1] == 'M' { 215 + input[row+1][col+1] == 'S' && input[row-1][col+1] == 'S' 216 + } else { 217 + false 218 + } 219 + } else if input[row-1][col-1] == 'S' { 220 + if input[row-1][col+1] == 'S' { 221 + input[row+1][col+1] == 'M' && input[row+1][col-1] == 'M' 222 + } else if input[row+1][col-1] == 'S' { 223 + input[row+1][col+1] == 'M' && input[row-1][col+1] == 'M' 224 + } else { 225 + false 226 + } 227 + } else { 228 + false 229 + } 230 + 231 + } 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 - // #[test] 309 - // fn day4_part2_test() { 310 - // let test_result = day4::day4_part2(include_str!("../input/day4_part2.test.txt")); 311 - // assert_eq!(test_result, "48"); 312 - // let result = day4::day4_part2(include_str!("../input/day4.txt")); 313 - // assert_eq!(result, "62098619"); 314 - // } 360 + #[test] 361 + fn day4_part2_test() { 362 + let test_result = day4::day4_part2(include_str!("../input/day4.test.txt")); 363 + assert_eq!(test_result, "9"); 364 + let result = day4::day4_part2(include_str!("../input/day4.txt")); 365 + assert_eq!(result, "1890"); 366 + } 315 367 316 368 #[test] 317 369 fn day3_part1_test() {