Advent of Code 2023 - Day 02
I'ts that time of year again, the Advent of Code! I'm completing the challenges this year using Rust 🦀 Day 2 was a little bit easier than day 1.
I'll be posting them here, just to document how I'm solving each challenge, and I highly encourage you to check them out before reading my solutions!
Here's the Day 2 Challenge
BTW: Complete Part 1 to have access to Part 2 😉
Solution for Part 1
// https://gist.github.com/Incognitowski/78f872c6612bb4731daedc420cbac976
use std::fs::File;
use std::io::{Result, BufRead, BufReader, Lines};
use std::path::Path;
use std::str::FromStr;
fn main() {
let input_file_path = "absolute_path_to_your_input_file";
let mut curr_sum = 0;
let iterator = for_each_line(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
let max_red = 12;
let max_green = 13;
let max_blue = 14;
for line in iterator.flatten() {
if let Some((game, combs)) = line.split_once(':') {
let game_throws = combs.trim().split(';').map(|c| c.trim()).collect::<Vec<&str>>();
let mut game_is_invalid = false;
'throws: for throws in game_throws {
let throws = throws.split(',').map(|c| c.trim()).collect::<Vec<&str>>();
for throw in throws {
match throw.split_once(' ') {
Some((num, "red")) => if (i32::from_str(num)).unwrap() > max_red {
game_is_invalid = true;
break 'throws;
},
Some((num, "green")) => if (i32::from_str(num)).unwrap() > max_green {
game_is_invalid = true;
break 'throws;
}
Some((num, "blue")) => if (i32::from_str(num)).unwrap() > max_blue {
game_is_invalid = true;
break 'throws;
}
None => continue,
_ => continue
}
}
}
if !game_is_invalid {
if let Some((_, game_id)) = game.split_once(' ') {
curr_sum += i32::from_str(game_id).unwrap_or(0);
}
}
}
}
println!("The sum of all valid game IDs is: {}", curr_sum);
}
fn for_each_line<P: AsRef<Path>>(filename: P) -> Result<Lines<BufReader<File>>> {
let file = File::open(filename)?;
Ok(BufReader::new(file).lines())
}
Solution for Part 2
use std::fs::File;
use std::io::{Result, BufRead, BufReader, Lines};
use std::path::Path;
use std::str::FromStr;
fn main() {
let input_file_path = "absolute_path_to_your_input_file";
let mut curr_sum = 0;
let iterator = for_each_line(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
for line in iterator.flatten() {
if let Some((_, combs)) = line.split_once(':') {
let game_throws = combs.trim().split(';').map(|c| c.trim()).collect::<Vec<&str>>();
let mut highest_red = 0;
let mut highest_green = 0;
let mut highest_blue = 0;
for throws in game_throws {
let throws = throws.split(',').map(|c| c.trim()).collect::<Vec<&str>>();
for throw in throws {
match throw.split_once(' ') {
Some((num, "red")) => if i32::from_str(num).unwrap() > highest_red {
highest_red = i32::from_str(num).unwrap();
},
Some((num, "green")) => if i32::from_str(num).unwrap() > highest_green {
highest_green = i32::from_str(num).unwrap();
}
Some((num, "blue")) => if i32::from_str(num).unwrap() > highest_blue {
highest_blue = i32::from_str(num).unwrap();
}
None => continue,
_ => continue
}
}
}
curr_sum += highest_red * highest_green * highest_blue;
}
}
println!("The sum of every set power is: {}", curr_sum);
}
fn for_each_line<P: AsRef<Path>>(filename: P) -> Result<Lines<BufReader<File>>> {
let file = File::open(filename)?;
Ok(BufReader::new(file).lines())
}