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/day7/src/lib.rs | 153 --------------------------------------------------- 1 file changed, 153 deletions(-) delete mode 100644 2023/day7/src/lib.rs (limited to '2023/day7/src') diff --git a/2023/day7/src/lib.rs b/2023/day7/src/lib.rs deleted file mode 100644 index 02cd337..0000000 --- a/2023/day7/src/lib.rs +++ /dev/null @@ -1,153 +0,0 @@ -use std::{collections::HashMap, str::FromStr}; - -#[derive(Debug, PartialEq, PartialOrd, Eq, Hash)] -enum HandType { - Five, - Four, - Full, - Three, - Two, - One, - High, -} - -#[derive(Debug)] -struct Play { - hand: Vec, - bid: u32, - hand_type: HandType, -} - -#[derive(Debug)] -struct PlayError; - -impl FromStr for Play { - type Err = PlayError; - - fn from_str(s: &str) -> Result { - if !s.contains(' ') { - return Err(PlayError); - } - - let s: Vec = s.split(' ').map(|x| x.to_string()).collect(); - if s.len() != 2 { - return Err(PlayError); - } - - let mut diffcards: HashMap = HashMap::new(); - let hand = s[0].chars().collect::>(); - - for ch in &hand { - if diffcards.contains_key(&ch) { - *(diffcards.get_mut(ch).unwrap()) += 1; - } else { - diffcards.insert(*ch, 1); - } - } - - let card_points: HashMap = HashMap::from([ - ('A', 13), - ('K', 12), - ('Q', 11), - ('J', 10), - ('T', 9), - ('9', 8), - ('8', 7), - ('7', 6), - ('6', 5), - ('5', 4), - ('4', 3), - ('3', 2), - ('2', 1), - ]); - let hand: Vec<_> = hand.iter().map(|x| *card_points.get(&x).unwrap()).collect(); - - let b: Vec = diffcards.clone().into_values().collect(); - let max_labels: u32 = *b.iter().max().unwrap(); - - let hand_type: HandType = match diffcards.keys().len() { - 1 => HandType::Five, - 2 if max_labels == 4 => HandType::Four, - 2 if max_labels == 3 => HandType::Full, - 3 if max_labels == 3 => HandType::Three, - 3 => HandType::Two, - 4 => HandType::One, - 5 => HandType::High, - _ => panic!("wtf is this?"), - }; - - Ok(Play { - hand, - bid: s[1].parse::().unwrap(), - hand_type, - }) - } -} - -pub fn part1(input: &str) -> u32 { - let mut res: u32 = 0; - - let plays: Vec<_> = input - .trim_end() - .split('\n') - .map(|x| x.parse::().unwrap()) - .collect(); - - let mut hs: HashMap> = HashMap::from([ - (HandType::Five, vec![]), - (HandType::Four, vec![]), - (HandType::Full, vec![]), - (HandType::Three, vec![]), - (HandType::Two, vec![]), - (HandType::One, vec![]), - (HandType::High, vec![]), - ]); - - for play in plays { - if let Some(p) = hs.get_mut(&play.hand_type) { - p.push(play); - } - } - - let mut rank = 1; - - for kind in [ - HandType::High, - HandType::One, - HandType::Two, - HandType::Three, - HandType::Full, - HandType::Four, - HandType::Five, - ] { - hs.get_mut(&kind) - .unwrap() - .sort_by(|a, b| a.hand.partial_cmp(&b.hand).unwrap()); - - for h in hs.get(&kind).unwrap() { - res += rank * h.bid; - rank += 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, 6440); - } - - #[test] - fn input_part1() { - let input = include_str!("../input.txt"); - let result = part1(input); - assert_eq!(result, 248179786); - } -} -- cgit v1.2.3-18-g5258