diff --git a/02/.gitignore b/02/.gitignore new file mode 100644 index 0000000..3e06fc3 --- /dev/null +++ b/02/.gitignore @@ -0,0 +1 @@ +day02 diff --git a/02/README.md b/02/README.md new file mode 100644 index 0000000..d99d4c7 --- /dev/null +++ b/02/README.md @@ -0,0 +1,11 @@ +# [Day 2](https://adventofcode.com/2023/day/2) +:gift::gift: + +Today's language: **Rust** + +My very first rust program! + +```shell +rustc day02.rs +./day02 +``` diff --git a/02/day02.rs b/02/day02.rs new file mode 100644 index 0000000..a40193b --- /dev/null +++ b/02/day02.rs @@ -0,0 +1,60 @@ +use std::io::{self, BufRead}; + +fn main() { + let max_red: u32 = 12; + let max_green: u32 = 13; + let max_blue: u32 = 14; + + let lines = read_lines("input.txt"); + let mut sum_ind_possible: u32 = 0; + let mut sum_ind_minpower: u32 = 0; + for line in lines { + let mut game_possible = true; + let Ok(game) = line else { panic!("Line not ok"); }; + let Some(colon) = game.find(':') else { panic!("Could not find ':'"); }; + let game_index = &game[5..colon].parse::().unwrap(); + let mut red = 0; + let mut green = 0; + let mut blue = 0; + for samples in game[colon+2..].split("; ") { + for color_value in samples.split(", ") { + let mut iter = color_value.split(' '); + let count; + if let Some(c) = iter.next() { count = c.parse::().unwrap(); } else { panic!("Invalid line: {}", game); }; + let Some(color) = iter.next() else { panic!("Invalid line: {}", game); }; + // println!("Color={}, value={}", color, count); + let do_color = |value: u32, colorvalue: &mut u32, max_color: u32| { + if value > *colorvalue { + *colorvalue = value; + } + return value <= max_color; + }; + game_possible &= match color { + "red" => do_color(count, &mut red, max_red), + "green" => do_color(count, &mut green, max_green), + "blue" => do_color(count, &mut blue, max_blue), + _ => false, + }; + } + } + if game_possible { sum_ind_possible += game_index }; + sum_ind_minpower += red * green * blue; + } + println!("Sum of indices of possible games: {}", sum_ind_possible); + println!("Sum of power of minimum sets: {}", sum_ind_minpower); +} + +// fn read_lines

(filename: P) -> io::Result>> +// where P: AsRef, { +// let file = std::fs::File::open(filename)?; +// Ok(std::io::BufReader::new(file).lines()) +// } + +fn read_lines

(filename: P) -> io::Lines> +where P: AsRef, { + return match std::fs::File::open(filename) { + Err(why) => panic!("Could not open file. {}", why), + Ok(file) => std::io::BufReader::new(file).lines() + }; +} +