From 8d235ecbc41a5869ab929c90c05167e4de5202e4 Mon Sep 17 00:00:00 2001 From: BlackDemonFire Date: Sat, 2 Dec 2023 12:53:27 +0100 Subject: [PATCH] Day 2 --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 ++ d02t1/Cargo.toml | 8 ++++++++ d02t1/src/main.rs | 22 ++++++++++++++++++++++ d02t2/Cargo.toml | 8 ++++++++ d02t2/src/main.rs | 24 ++++++++++++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 d02t1/Cargo.toml create mode 100644 d02t1/src/main.rs create mode 100644 d02t2/Cargo.toml create mode 100644 d02t2/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 44615a6..373583b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,3 +9,11 @@ version = "0.1.0" [[package]] name = "d01t2" version = "0.1.0" + +[[package]] +name = "d02t1" +version = "0.1.0" + +[[package]] +name = "d02t2" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index a055adb..f32aa6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,6 @@ resolver = "2" members = [ "d01t1", "d01t2", + "d02t1", + "d02t2", ] diff --git a/d02t1/Cargo.toml b/d02t1/Cargo.toml new file mode 100644 index 0000000..40f9135 --- /dev/null +++ b/d02t1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d02t1" +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/d02t1/src/main.rs b/d02t1/src/main.rs new file mode 100644 index 0000000..2546c0a --- /dev/null +++ b/d02t1/src/main.rs @@ -0,0 +1,22 @@ +fn main() { + let input = include_str!("../input.txt"); + let mut result = 0; + for line in input.lines() { + let (game_id, game_values) = line.split_once(": ").unwrap(); + if !game_values.split("; ").any(|die_set| { + die_set.split(", ").any(|v| { + let (val, color) = v.split_once(" ").unwrap(); + let val = val.parse::().unwrap(); + match color { + "red" => val > 12, + "green" => val > 13, + "blue" => val > 14, + c => panic!("unknown color {c}"), + } + }) + }) { + result += game_id.split_once(" ").unwrap().1.parse::().unwrap(); + } + } + println!("d02t1: {result}"); +} diff --git a/d02t2/Cargo.toml b/d02t2/Cargo.toml new file mode 100644 index 0000000..3da362d --- /dev/null +++ b/d02t2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d02t2" +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/d02t2/src/main.rs b/d02t2/src/main.rs new file mode 100644 index 0000000..f454505 --- /dev/null +++ b/d02t2/src/main.rs @@ -0,0 +1,24 @@ +fn main() { + let input = include_str!("../input.txt"); + let result = input + .lines() + .map(|line| { + let (_game_id, game_values) = line.split_once(": ").unwrap(); + let mut min_red = 0; + let mut min_green = 0; + let mut min_blue = 0; + game_values.replace(";", ",").split(", ").for_each(|v| { + let (val, color) = v.split_once(" ").unwrap(); + let val = val.parse::().unwrap(); + match color { + "red" => min_red = min_red.max(val), + "green" => min_green = min_green.max(val), + "blue" => min_blue = min_blue.max(val), + c => panic!("unknown color {c}"), + } + }); + min_red * min_green * min_blue + }) + .sum::(); + println!("d02t2: {result}"); +}