From f1755e2bd371e391957a56c7c76eb1c8eb329fce Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Mon, 4 Dec 2023 21:08:43 +0100 Subject: [PATCH] day 2 --- 2023/02/README.md | 11 +++++ 2023/02/rust/main.rs | 106 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 2023/02/README.md create mode 100644 2023/02/rust/main.rs diff --git a/2023/02/README.md b/2023/02/README.md new file mode 100644 index 0000000..14bf1fe --- /dev/null +++ b/2023/02/README.md @@ -0,0 +1,11 @@ +# 02 + +Learned about callbacks in rust. + +
+ Solutions +
    +
  1. 2348
  2. +
  3. 76008
  4. +
+
diff --git a/2023/02/rust/main.rs b/2023/02/rust/main.rs new file mode 100644 index 0000000..30b6477 --- /dev/null +++ b/2023/02/rust/main.rs @@ -0,0 +1,106 @@ +#![feature(test)] + +use std::cmp; + +extern crate test; + +#[cfg(test)] +mod tests { + use super::*; + use test::Bencher; + + #[bench] + fn bench_a(b: &mut Bencher) { + b.iter(|| part_a(INPUT)); + } + + #[bench] + fn bench_b(b: &mut Bencher) { + b.iter(|| part_b(INPUT)); + } +} + +const INPUT: &str = include_str!("../input.txt"); +const TEST: &str = include_str!("../test.txt"); + +#[derive(Debug)] +struct Round { + red: usize, + blue: usize, + green: usize, +} + +fn max_for_game(s: &str, calc: impl Fn(Round) -> usize) -> usize { + let max = s + .split(";") + .map(|round| { + // Max count + let mut r = Round { + blue: 0, + green: 0, + red: 0, + }; + for pick in round.split(",") { + let parts: Vec<&str> = pick.trim().split(" ").collect(); + let num: usize = parts[0].parse().unwrap(); + match parts[1] { + "blue" => r.blue = num, + "green" => r.green = num, + "red" => r.red = num, + _ => panic!("Invalid color {}", parts[1]), + } + } + r + }) + .reduce(|a, b| Round { + red: cmp::max(a.red, b.red), + blue: (cmp::max(a.blue, b.blue)), + green: (cmp::max(a.green, b.green)), + }) + .unwrap(); + calc(max) +} + +fn parse_game_b(s: &str) -> usize { + let splitted: Vec<&str> = s.split(":").collect(); + max_for_game(splitted[1], |x| x.blue * x.green * x.red) +} + +fn part_a(input: &str) { + let result: usize = input + .trim() + .split("\n") + .map(|line| { + let splitted: Vec<&str> = line.split(":").collect(); + max_for_game(splitted[1], |r| { + if r.red <= 12 && r.green <= 13 && r.blue <= 14 { + splitted[0].split_at(5).1.parse().unwrap() + } else { + 0 + } + }) + }) + .sum(); + println!("{result}"); +} + +fn part_b(input: &str) { + let result: usize = input + .trim() + .split("\n") + .map(|line| { + return parse_game_b(line); + }) + .sum(); + println!("{result}"); +} + +fn main() { + println!("Part A:"); + part_a(TEST); + part_a(INPUT); + + println!("\nPart B:"); + part_b(TEST); + part_b(INPUT); +}