From c01a96c05d90c62bcc074ae85a5b60b3ee71b65c Mon Sep 17 00:00:00 2001 From: wieerwill Date: Sat, 2 Dec 2023 11:08:06 +0100 Subject: [PATCH] solve Day02 in rust --- .gitignore | 4 ++- Day02/rust/Cargo.toml | 8 ++++++ Day02/rust/src/main.rs | 57 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 Day02/rust/Cargo.toml create mode 100644 Day02/rust/src/main.rs diff --git a/.gitignore b/.gitignore index 00209f6..44baee0 100644 --- a/.gitignore +++ b/.gitignore @@ -172,4 +172,6 @@ Cargo.lock **/*.rs.bk # MSVC Windows builds of rustc generate these, which store debugging information -*.pdb \ No newline at end of file +*.pdb + +helper.md \ No newline at end of file diff --git a/Day02/rust/Cargo.toml b/Day02/rust/Cargo.toml new file mode 100644 index 0000000..1ec6963 --- /dev/null +++ b/Day02/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/Day02/rust/src/main.rs b/Day02/rust/src/main.rs new file mode 100644 index 0000000..c4f65ea --- /dev/null +++ b/Day02/rust/src/main.rs @@ -0,0 +1,57 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; +use std::collections::HashMap; + +fn parse_game_data(line: &str) -> (i32, HashMap) { + let parts: Vec<&str> = line.split(": ").collect(); + let game_id = parts[0].split_whitespace().nth(1).unwrap().parse::().unwrap(); + let mut color_counts = HashMap::new(); + color_counts.insert("red".to_string(), 0); + color_counts.insert("green".to_string(), 0); + color_counts.insert("blue".to_string(), 0); + + let subsets = parts[1].split("; "); + for subset in subsets { + let colors = subset.split(", "); + for color in colors { + let color_parts: Vec<&str> = color.split_whitespace().collect(); + let count = color_parts[0].parse::().unwrap(); + let color_name = color_parts[1]; + let max_count = color_counts.get(color_name).unwrap_or(&0).max(&count); + color_counts.insert(color_name.to_string(), *max_count); + } + } + + (game_id, color_counts) +} + +fn calculate_power_of_set(game_data: &HashMap) -> i32 { + game_data.get("red").unwrap_or(&0) * game_data.get("green").unwrap_or(&0) * game_data.get("blue").unwrap_or(&0) +} + +fn process_games(file_path: &Path) -> i32 { + let file = File::open(file_path).unwrap(); + let reader = io::BufReader::new(file); + + let mut sum_of_powers = 0; + for line in reader.lines() { + let line = line.unwrap(); + let (_, game_data) = parse_game_data(&line); + let power = calculate_power_of_set(&game_data); + sum_of_powers += power; + } + + sum_of_powers +} + +fn main() { + // Test the function with test data + let test_result = process_games(Path::new("../test.txt")); + assert_eq!(test_result, 2286, "Test failed: expected sum of powers to be 2286, but got {}", test_result); + println!("Test Passed: Sum of powers for the minimum cube sets is {}\n", test_result); + + // Process the actual input file + let result = process_games(Path::new("../input.txt")); + println!("From input.txt: Sum of powers for the minimum cube sets is {}", result); +}