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/rust/day2/src/lib.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 2023/rust/day2/src/lib.rs (limited to '2023/rust/day2/src') diff --git a/2023/rust/day2/src/lib.rs b/2023/rust/day2/src/lib.rs new file mode 100644 index 0000000..e18469c --- /dev/null +++ b/2023/rust/day2/src/lib.rs @@ -0,0 +1,99 @@ +pub fn part1(input: &str) -> u32 { + let mut res: u32 = 0; + + for (i, line) in input.trim_end().split('\n').enumerate() { + let game = line.split(": ").collect::>()[1]; + let mut flag = true; + for point in game.split(";") { + for _play in point.split(',') { + let play = _play.trim(); + let play_s: Vec<_> = play.split(' ').collect(); + let (p, c) = (play_s[0].parse::().unwrap(), play_s[1].to_string()); + if c.chars().nth(0).unwrap() == 'r' && p > 12 { + flag = false; + } else if c.chars().nth(0).unwrap() == 'g' && p > 13 { + flag = false; + } else if c.chars().nth(0).unwrap() == 'b' && p > 14 { + flag = false; + } + } + } + + if flag { + res += (i + 1) as u32; + } + } + + res +} + +pub fn part2(input: &str) -> u32 { + let mut res: u32 = 0; + + for line in input.trim_end().split('\n') { + let game = line.split(": ").collect::>()[1]; + // r, g, b + let mut balls: [u32; 3] = [0, 0, 0]; + for point in game.split(";") { + for _play in point.split(',') { + let play = _play.trim(); + let play_s: Vec<_> = play.split(' ').collect(); + let (p, c) = (play_s[0].parse::().unwrap(), play_s[1].to_string()); + + let bi = match c.chars().nth(0).unwrap() { + 'r' => 0, + 'g' => 1, + 'b' => 2, + _ => panic!("wtf is this color?!"), + }; + + balls[bi] = if balls[bi] < p { p } else { balls[bi] }; + } + } + + println!("{:?}", balls); + let mut tot = 1; + + for j in balls { + if j != 0 { + tot *= j; + } + } + + res += tot; + } + res +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn example_part1() { + let input = include_str!("../example.txt"); + let result = part1(input); + assert_eq!(result, 8); + } + + #[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