Created
December 22, 2023 22:04
-
-
Save maestroviktorin/d650d8a828ed3fc6e4bdcf87a0d4bf25 to your computer and use it in GitHub Desktop.
Advent of Code 2023. Challenge 2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Advent of Code 2023. Challenge 2. | |
*/ | |
use regex; | |
use std::fs::read_to_string; | |
const RED: usize = 12; | |
const GREEN: usize = 13; | |
const BLUE: usize = 14; | |
fn main() { | |
let (mut possible_games_amount, mut games_powers_sum) = (0usize, 0usize); | |
read_to_string("challenge-2.txt") | |
.unwrap() | |
.lines() | |
.for_each(|line| { | |
let game = Game::from(line); | |
possible_games_amount += game.verified(RED, GREEN, BLUE); | |
games_powers_sum += game.power(); | |
}); | |
println!( | |
"Task 1: {:?}\nTask 2: {:?}", | |
possible_games_amount, games_powers_sum | |
); | |
} | |
#[derive(Debug)] | |
#[allow(dead_code)] | |
struct Game { | |
id: usize, | |
red: usize, | |
green: usize, | |
blue: usize, | |
} | |
impl Game { | |
fn verified(&self, red_max: usize, green_max: usize, blue_max: usize) -> usize { | |
if self.red <= red_max && self.green <= green_max && self.blue <= blue_max { | |
return self.id; | |
} | |
0 | |
} | |
fn power(&self) -> usize { | |
self.red * self.green * self.blue | |
} | |
} | |
impl From<&str> for Game { | |
fn from(source: &str) -> Self { | |
let re = regex::Regex::new(r"Game (\d+): ").unwrap(); | |
// TODO: Refactor `id` extraction. | |
let id = re | |
.captures_iter(source) | |
.map(|captures| captures.extract()) | |
.map(|(_, [id])| id.parse::<usize>().unwrap()) | |
.collect::<Vec<_>>()[0]; | |
let source = re.replace_all(source, ""); | |
let (mut red, mut green, mut blue) = (0, 0, 0); | |
source.split("; ").for_each(|round_source| { | |
let round = Round::from(round_source); | |
red = round.red.max(red); | |
green = round.green.max(green); | |
blue = round.blue.max(blue); | |
}); | |
Self { | |
id, | |
red, | |
green, | |
blue, | |
} | |
} | |
} | |
struct Round { | |
pub red: usize, | |
pub green: usize, | |
pub blue: usize, | |
} | |
impl From<&str> for Round { | |
fn from(source: &str) -> Self { | |
let (mut red, mut green, mut blue) = (0, 0, 0); | |
source | |
.split(", ") | |
.map(|amount_color| { | |
let amount_color: Vec<_> = amount_color.split(" ").collect(); | |
let (amount, color) = ( | |
amount_color[0].parse::<usize>().unwrap(), | |
Color::from(amount_color[1]), | |
); | |
(amount, color) | |
}) | |
.for_each(|(amount, color)| match color { | |
Color::Red => red += amount, | |
Color::Green => green += amount, | |
Color::Blue => blue += amount, | |
}); | |
Self { red, green, blue } | |
} | |
} | |
enum Color { | |
Red, | |
Green, | |
Blue, | |
} | |
impl From<&str> for Color { | |
fn from(source: &str) -> Self { | |
match source { | |
"red" => Self::Red, | |
"green" => Self::Green, | |
"blue" => Self::Blue, | |
_ => panic!("Unexpected color"), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment