Part 2
This commit is contained in:
parent
14a77cd34f
commit
c89d892343
1 changed files with 42 additions and 4 deletions
|
@ -1,3 +1,5 @@
|
|||
use std::cmp::{max, min};
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
extern crate nom;
|
||||
|
@ -32,6 +34,13 @@ struct RevealGroup {
|
|||
blue: Option<usize>,
|
||||
}
|
||||
|
||||
impl RevealGroup {
|
||||
fn power(&self) -> usize {
|
||||
println!("reveal group power for {:?}", self);
|
||||
self.red.unwrap_or(0) * self.green.unwrap_or(0) * self.blue.unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Reveal>> for RevealGroup {
|
||||
fn from(value: Vec<Reveal>) -> Self {
|
||||
// TODO: this obviously won't work if the vector contains multiple reveals of the same kind
|
||||
|
@ -58,6 +67,24 @@ struct Game {
|
|||
reveal_groups: Vec<RevealGroup>,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn made_possible(&self) -> RevealGroup {
|
||||
let mut red = 0;
|
||||
let mut green = 0;
|
||||
let mut blue = 0;
|
||||
for rg in &self.reveal_groups {
|
||||
red = max(rg.red.unwrap_or(0), red);
|
||||
green = max(rg.green.unwrap_or(0), green);
|
||||
blue = max(rg.blue.unwrap_or(0), blue);
|
||||
}
|
||||
RevealGroup {
|
||||
red: Some(red),
|
||||
green: Some(green),
|
||||
blue: Some(blue),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn int(input: &str) -> IResult<&str, usize> {
|
||||
map_res(take_while_m_n(1, 16, |c: char| c.is_numeric()), |s| {
|
||||
usize::from_str_radix(s, 10)
|
||||
|
@ -79,11 +106,9 @@ fn reveal_group(input: &str) -> IResult<&str, RevealGroup> {
|
|||
}
|
||||
|
||||
fn game(input: &str) -> IResult<&str, Game> {
|
||||
println!("that belle");
|
||||
let (input, id) = int(tag("Game ")(input)?.0)?;
|
||||
let (input, reveal_groups) = separated_list0(tag("; "), reveal_group)(tag(": ")(input)?.0)?;
|
||||
|
||||
println!("{id} {:?}", reveal_groups);
|
||||
Ok((input, Game { id, reveal_groups }))
|
||||
}
|
||||
|
||||
|
@ -114,7 +139,10 @@ impl AoCSolution for Day2 {
|
|||
}
|
||||
|
||||
fn part2(input: Self::Input) -> Self::Solution {
|
||||
return 0;
|
||||
input
|
||||
.lines()
|
||||
.map(|s| game(s).unwrap().1.made_possible().power())
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,6 +163,16 @@ Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"#
|
|||
),
|
||||
8
|
||||
);
|
||||
assert_eq!(Day2::part2(r#""#.into()), 0);
|
||||
assert_eq!(
|
||||
Day2::part2(
|
||||
r#"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"#
|
||||
.into()
|
||||
),
|
||||
2286
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue