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/day4/src/lib.rs | 132 --------------------------------------------------- 1 file changed, 132 deletions(-) delete mode 100644 2023/day4/src/lib.rs (limited to '2023/day4/src') diff --git a/2023/day4/src/lib.rs b/2023/day4/src/lib.rs deleted file mode 100644 index ca38b56..0000000 --- a/2023/day4/src/lib.rs +++ /dev/null @@ -1,132 +0,0 @@ -fn parse(input: &str) -> Vec { - input - .trim_end() - .split('\n') - .map(|x| { - let b = x.to_string(); - let k = b.split(':').last().unwrap(); - - return k.trim().to_string(); - }) - .collect() -} - -pub fn part1(input: &str) -> u32 { - let mut res: u32 = 0; - - let cards: Vec = parse(input); - for card in cards { - let numbers: Vec = card.split('|').map(|x| x.trim().to_string()).collect(); - assert_eq!(numbers.len(), 2); - - let nums: Vec> = numbers - .iter() - .map(|x| { - x.replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::().unwrap()) - .collect() - }) - .collect(); - - assert_eq!(nums.len(), 2); - let (winnings, plays) = (&nums[0], &nums[1]); - - let mut k = 0.5; - for win in winnings { - if plays.contains(&win) { - k *= 2.; - } - } - res += k as u32; - } - - res -} - -// -// -// -// 1 -> 4 | x -// 2 -> 2 | y x -// 3 -> 2 | y y y x -// 4 -> 1 | y y y y y y y x -// 5 -> 0 | y y y y y y y y y y y y y x -// 6 -> 0 | x -// -// -pub fn part2(input: &str) -> u32 { - let mut res: u32 = 0; - - let cards: Vec = parse(input); - let mut incr: Vec = Vec::new(); - incr.resize_with(cards.len(), Default::default); - - let mut i = 0; - for card in cards { - let numbers: Vec = card.split('|').map(|x| x.trim().to_string()).collect(); - assert_eq!(numbers.len(), 2); - - let nums: Vec> = numbers - .iter() - .map(|x| { - x.replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::().unwrap()) - .collect() - }) - .collect(); - - assert_eq!(nums.len(), 2); - let (winnings, plays) = (&nums[0], &nums[1]); - - let mut k = 0; - for win in winnings { - if plays.contains(&win) { - k += 1; - } - } - - for j in (i + 1)..(i + 1 + k) { - incr[j] += 1 + incr[i]; - } - - res += 1 + incr[i]; - i += 1; - } - - res -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn example_part1() { - let input = include_str!("../example.txt"); - let result = part1(input); - assert_eq!(result, 13); - } - - #[test] - fn input_part1() { - let input = include_str!("../input.txt"); - let result = part1(input); - assert_eq!(result, 25174); - } - - #[test] - fn example_part2() { - let input = include_str!("../example.txt"); - let result = part2(input); - assert_eq!(result, 30); - } - - #[test] - fn input_part2() { - let input = include_str!("../input.txt"); - let result = part2(input); - assert_eq!(result, 6420979); - } -} -- cgit v1.2.3-18-g5258