Day 2: Cube Conundrum
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots
🔓 Edit: Post has been unlocked after 6 minutes
Just getting started with Rust, part 1 took a long time. Really amazed when I saw part 2, just needed to add 2 lines and was done due to the approach I had taken. Feedback more than welcome!
use std::{ cmp, fs, io::{BufRead, BufReader}, }; fn main() { cube_conundrum_complete(); } fn cube_conundrum_complete() { // Load the data file let filename = "./data/input_data/day_2_cubes.txt"; let file = match fs::File::open(filename) { Ok(f) => f, Err(e) => { eprintln!("Error opening file: {}", e); return; } }; let reader = BufReader::new(file); // iniatiate final results let mut total = 0; let mut total_power = 0; // loop over the games in the file for _line in reader.lines() { // Handle the data and extract game number and maximum number of cubes per color let (game_number, max_green, max_red, max_blue) = cube_conundrum_data_input(_line.unwrap()); // Calculate the power for the day 2 result total_power += max_green * max_red * max_blue; //Calculate if the game was possible with the given number of cubes let result = cube_conundrum_game_possible(game_number, max_green, max_red, max_blue); total += result; } // print the final results println!("total part 1: {}", total); println!("total part 2: {}", total_power); } fn cube_conundrum_data_input(game_input: String) -> (i32, i32, i32, i32) { // Split the game number from the draws let (game_part, data_part) = game_input.split_once(":").unwrap(); // Select the number of the round and parse into an integer let game_number: i32 = game_part .split_once(" ") .unwrap() .1 .parse::() .expect("could not parse gamenumber to integer"); // Split the data part into a vector both split on , and ; cause we only care about he maximum per color let parts: Vec<&str> = data_part .split(|c| c == ',' || c == ';') .map(|part| part.trim()) .collect(); // Set the intial values for the maximum per color to 0 let (mut max_green, mut max_red, mut max_blue) = (0, 0, 0); // Loop over the different draws split them into color and nubmer of cubes, update maximum number of cubes for part in parts.iter() { let (nr_cubes_text, color) = part.split_once(" ").unwrap(); let nr_cubes = nr_cubes_text .parse::() .expect("could not parse to integer"); match color { "green" => max_green = cmp::max(max_green, nr_cubes), "red" => max_red = cmp::max(max_red, nr_cubes), "blue" => max_blue = cmp::max(max_blue, nr_cubes), _ => println!("unknown color: {}", color), }; } return (game_number, max_green, max_red, max_blue); } fn cube_conundrum_game_possible( game_number: i32, max_green: i32, max_red: i32, max_blue: i32, ) -> i32 { // Compare the number of seen cubes per game with the proposed number. Return the game number if it was possible, otherwise 0 let (comparison_red, comparison_green, comparison_blue) = (12, 13, 14); if max_green > comparison_green || max_red > comparison_red || max_blue > comparison_blue { return 0; }; game_number }