diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-02-09 11:42:04 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-02-09 11:42:04 +0100 |
commit | db2686c798378738020a0951a521358586e21aa0 (patch) | |
tree | 8d585ceb9bdc302ae10a2458cc343be40d60acb7 /2023/day4/src | |
parent | 67dff579e91c13ef40ecb17d14497d145861c5e7 (diff) |
Add day2 in OCaml
Diffstat (limited to '2023/day4/src')
-rw-r--r-- | 2023/day4/src/lib.rs | 132 |
1 files changed, 0 insertions, 132 deletions
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<String> { - 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<String> = parse(input); - for card in cards { - let numbers: Vec<String> = card.split('|').map(|x| x.trim().to_string()).collect(); - assert_eq!(numbers.len(), 2); - - let nums: Vec<Vec<_>> = numbers - .iter() - .map(|x| { - x.replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::<u32>().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<String> = parse(input); - let mut incr: Vec<u32> = Vec::new(); - incr.resize_with(cards.len(), Default::default); - - let mut i = 0; - for card in cards { - let numbers: Vec<String> = card.split('|').map(|x| x.trim().to_string()).collect(); - assert_eq!(numbers.len(), 2); - - let nums: Vec<Vec<_>> = numbers - .iter() - .map(|x| { - x.replace(" ", " ") - .split(' ') - .map(|x| x.trim().parse::<u32>().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); - } -} |