From db2686c798378738020a0951a521358586e21aa0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 9 Feb 2024 11:42:04 +0100 Subject: Add day2 in OCaml --- 2023/day13/src/lib.rs | 146 -------------------------------------------------- 1 file changed, 146 deletions(-) delete mode 100644 2023/day13/src/lib.rs (limited to '2023/day13/src') diff --git a/2023/day13/src/lib.rs b/2023/day13/src/lib.rs deleted file mode 100644 index 03af2b8..0000000 --- a/2023/day13/src/lib.rs +++ /dev/null @@ -1,146 +0,0 @@ -pub fn part1(input: &str) -> u32 { - let mut res: u32 = 0; - - let m: Vec> = input - .trim_end() - .split("\n\n") - .map(|x| { - x.split('\n') - .map(|y| y.chars().collect::()) - .collect() - }) - .collect(); - - for s in m { - let n = s.len(); - - // Search by column - // 012345678 - // >< - // #.##..##. - // ..#.##.#. - // ##......# - // ##......# - // ..#.##.#. - // ..##..##. - // #.#.##.#. - // >< - // 012345678 - // - let m = s[0].len(); - let mut mc = 0; - for i in 0..m { - let mut j = m - 1; - while j > i { - let mut s1 = String::new(); - let mut s2 = String::new(); - for k in 0..n { - s1.push(s[k].chars().nth(i).unwrap()); - s2.push(s[k].chars().nth(j).unwrap()); - } - if s1 == s2 { - mc = std::cmp::max(mc, i); - } - j -= 1; - } - } - - let mut check = true; - - let mut i = mc; - let mut j = mc + 1; - - while i > 0 && j < n { - let mut s1 = String::new(); - let mut s2 = String::new(); - for k in 0..n { - s1.push(s[k].chars().nth(i).unwrap()); - if j >= s[k].len() { - break; - } - s2.push(s[k].chars().nth(j).unwrap()); - } - if s1 != s2 { - check = false; - break; - } - i -= 1; - j += 1; - } - - if check { - res += (mc + 1) as u32; - continue; - } - - // Search by row - let mut mc = 0; - for i in 0..(n / 2) + 1 { - for j in ((n / 2)..n).rev() { - if s[i] == s[j] { - mc = std::cmp::max(mc, i); - } - } - } - - let mut check = true; - - let mut i = mc; - let mut j = mc + 1; - - while i > 0 && j < n { - if s[i] != s[j] { - check = false; - break; - } - i -= 1; - j += 1; - } - - if check { - res += (mc + 1) as u32 * 100; - continue; - } - } - - res -} - -// pub fn part2(input: &str) -> u32 { -// let mut res: u32 = 0; -// -// res -// } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn example_part1() { - let input = include_str!("../example.txt"); - let result = part1(input); - assert_eq!(result, 405); - } - - #[test] - fn input_part1() { - let input = include_str!("../input.txt"); - let result = part1(input); - assert_eq!(result, 2331); - } - // - // #[test] - // fn example_part2() { - // let input = include_str!("../example.txt"); - // let result = part2(input); - // assert_eq!(result, 2286); - // } - // - // #[test] - // fn input_part2() { - // let input = include_str!("../input.txt"); - // let result = part2(input); - // assert_eq!(result, 71585); - // } -} -- cgit v1.2.3-18-g5258