-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): add solutions for 2023 day 2
- Loading branch information
1 parent
33447e7
commit 2662826
Showing
2 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use regex::Regex; | ||
|
||
use advent_of_code::utils::challenges::prelude::*; | ||
|
||
lazy_static! { | ||
#[derive(Debug)] | ||
static ref REVEAL_MATCHER: Regex = Regex::new(r"(?x) | ||
(?P<amount>\d+)\ (?P<color>\w+) | ||
").unwrap(); | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
struct GameRound { | ||
pub red: u64, | ||
pub green: u64, | ||
pub blue: u64, | ||
} | ||
|
||
// Might as well re-use existing struct type | ||
type CubeSet = GameRound; | ||
|
||
impl GameRound { | ||
fn is_playable(&self, config: &CubeSet) -> bool { | ||
config.red >= self.red | ||
&& config.green >= self.green | ||
&& config.blue >= self.blue | ||
} | ||
} | ||
|
||
impl From<&str> for GameRound { | ||
fn from(str: &str) -> Self { | ||
let mut round = GameRound::default(); | ||
|
||
for cap in REVEAL_MATCHER.captures_iter(str) { | ||
let amount = cap["amount"].parse::<u64>().unwrap(); | ||
let color = &cap["color"]; | ||
|
||
match color { | ||
"red" => { round.red += amount } | ||
"green" => { round.green += amount } | ||
"blue" => { round.blue += amount } | ||
_ => unimplemented!() | ||
} | ||
} | ||
|
||
round | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Game { | ||
pub id: u64, | ||
pub rounds: Vec<GameRound> | ||
} | ||
|
||
impl Game { | ||
fn is_playable(&self, config: &CubeSet) -> bool { | ||
self.rounds.iter().all(|round| round.is_playable(config)) | ||
} | ||
|
||
fn minimum_cubeset(&self) -> CubeSet { | ||
let mut minimums = CubeSet::default(); | ||
for round in &self.rounds { | ||
minimums.red = minimums.red.max(round.red); | ||
minimums.green = minimums.green.max(round.green); | ||
minimums.blue = minimums.blue.max(round.blue); | ||
} | ||
minimums | ||
} | ||
|
||
fn power(&self) -> u64 { | ||
let minimums = self.minimum_cubeset(); | ||
minimums.red * minimums.green * minimums.blue | ||
} | ||
} | ||
|
||
fn parse(input: &PuzzleInput) -> Vec<Game> { | ||
input.trim().lines().enumerate().map(|(index, line)| { | ||
let rounds = line.split(";").map(GameRound::from).collect(); | ||
Game { id: (index + 1) as u64, rounds } | ||
}).collect() | ||
} | ||
|
||
fn part_one(input: &PuzzleInput, _args: &RawPuzzleArgs) -> Solution { | ||
let games = parse(input); | ||
|
||
let available = CubeSet { | ||
red: 12, | ||
green: 13, | ||
blue: 14, | ||
}; | ||
|
||
let possible = games.iter().filter(|game| game.is_playable(&available)); | ||
Answer(possible.map(|game| game.id).sum()) | ||
} | ||
|
||
fn part_two(input: &PuzzleInput, _args: &RawPuzzleArgs) -> Solution { | ||
let games = parse(input); | ||
Answer(games.iter().map(|game| game.power()).sum()) | ||
} | ||
|
||
solve!(part_one, part_two); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ macro_rules! preload_challenges { | |
01 | ||
) | ||
2023 ( | ||
01 | ||
01, 02 | ||
) | ||
} | ||
}; | ||
|