From ada00b863bf3a7337b6116c926263fe2a1888e57 Mon Sep 17 00:00:00 2001 From: dobiadi Date: Sat, 2 Dec 2023 21:18:18 +0100 Subject: [PATCH] Day2 Rust --- day2/rust/.gitignore | 1 + day2/rust/Cargo.lock | 7 +++ day2/rust/Cargo.toml | 8 +++ day2/rust/src/main.rs | 135 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 day2/rust/.gitignore create mode 100644 day2/rust/Cargo.lock create mode 100644 day2/rust/Cargo.toml create mode 100644 day2/rust/src/main.rs diff --git a/day2/rust/.gitignore b/day2/rust/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/day2/rust/.gitignore @@ -0,0 +1 @@ +target diff --git a/day2/rust/Cargo.lock b/day2/rust/Cargo.lock new file mode 100644 index 0000000..b21cc6a --- /dev/null +++ b/day2/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/day2/rust/Cargo.toml b/day2/rust/Cargo.toml new file mode 100644 index 0000000..1ec6963 --- /dev/null +++ b/day2/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day2/rust/src/main.rs b/day2/rust/src/main.rs new file mode 100644 index 0000000..8102844 --- /dev/null +++ b/day2/rust/src/main.rs @@ -0,0 +1,135 @@ +struct Game { + id: usize, + grabs: Vec, +} + +struct Grab { + cubes: Vec, +} + +struct Cube { + count: usize, + color: Color, +} + +#[derive(Debug)] +enum Color { + RED, + GREEN, + BLUE, + UNKNOWN, +} + +fn main() { + let stdin = std::io::stdin(); + + let mut games: Vec = Vec::new(); + + // Read and parse input + for line in stdin.lines() { + let line = line.unwrap(); + + let (game_id_str, grab_strs) = line.split_once(":").unwrap(); + + let game_id = game_id_str.split_once(" ").unwrap().1.parse().unwrap(); + + let mut grabs = Vec::new(); + + for grab_str in grab_strs.split(";") { + let mut cubes = Vec::new(); + + for cube in grab_str.split(",").map(|str| str.trim()) { + let (count, color) = cube.split_once(' ').unwrap(); + + let count = count.parse().unwrap(); + let color = match color { + "red" => Color::RED, + "green" => Color::GREEN, + "blue" => Color::BLUE, + _ => Color::UNKNOWN, + }; + + cubes.push(Cube { count, color }) + } + + grabs.push(Grab { cubes }); + } + + games.push(Game { id: game_id, grabs }) + } + + // Part 1 + let red_cubes = 12; + let green_cubes = 13; + let blue_cubes = 14; + let mut part1 = 0; + + for game in &games { + let mut possible = true; + for grab in &game.grabs { + for cube in &grab.cubes { + match cube.color { + Color::RED => { + if cube.count > red_cubes { + possible = false; + break; + } + } + Color::GREEN => { + if cube.count > green_cubes { + possible = false; + break; + } + } + Color::BLUE => { + if cube.count > blue_cubes { + possible = false; + break; + } + } + Color::UNKNOWN => (), + } + } + + if !possible { + break; + } + } + + if possible { + part1 += game.id; + } + } + + println!("{}", part1); + + // Part 2 + let mut part2 = 0; + + for game in &games { + let mut min_reds = 0; + let mut min_greens = 0; + let mut min_blues = 0; + + for grab in &game.grabs { + for cube in &grab.cubes { + match cube.color { + Color::RED => { + min_reds = std::cmp::max(min_reds, cube.count); + } + Color::GREEN => { + min_greens = std::cmp::max(min_greens, cube.count); + } + Color::BLUE => { + min_blues = std::cmp::max(min_blues, cube.count); + } + Color::UNKNOWN => (), + } + } + } + + part2 += min_reds * min_greens * min_blues; + } + + println!("{}", part2); +}