Implemented day 2.
This commit is contained in:
130
src/two/mod.rs
Normal file
130
src/two/mod.rs
Normal file
@@ -0,0 +1,130 @@
|
||||
use std::{path::Path, str::FromStr, cmp::max};
|
||||
|
||||
use crate::input::{AdventError, read_into_vec};
|
||||
|
||||
impl FromStr for Color {
|
||||
type Err = AdventError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(match s {
|
||||
"red" => Color::Red,
|
||||
"green" => Color::Green,
|
||||
"blue" => Color::Blue,
|
||||
_ => return Err(AdventError("No matching Color enum.".to_string()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Group {
|
||||
type Err = AdventError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let split = s.split(' ').collect::<Vec<&str>>();
|
||||
Ok(Group{count: u32::from_str(split[0])?, color: Color::from_str(split[1])?})
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Pull {
|
||||
type Err = AdventError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut res = Vec::<Group>::new();
|
||||
for group in s.split(';') {
|
||||
let group = group.trim();
|
||||
res.push(Group::from_str(group)?);
|
||||
}
|
||||
Ok(Pull{groups: res})
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Game {
|
||||
type Err = AdventError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let split = s.split(':').collect::<Vec<&str>>();
|
||||
let id = u32::from_str(split[0].split(' ').collect::<Vec<&str>>()[1])?;
|
||||
let pulls = split[1];
|
||||
let mut res = Vec::<Pull>::new();
|
||||
for pull in pulls.split(',') {
|
||||
let pull = pull.trim();
|
||||
res.push(Pull::from_str(pull)?);
|
||||
}
|
||||
Ok(Game{id: id, pulls: res})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Color {
|
||||
Red,
|
||||
Green,
|
||||
Blue
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Group {
|
||||
count: u32,
|
||||
color: Color,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Pull {
|
||||
groups: Vec<Group>
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Game {
|
||||
id: u32,
|
||||
pulls: Vec<Pull>
|
||||
}
|
||||
|
||||
|
||||
pub fn two_one() -> Result<(), AdventError> {
|
||||
let games: Vec<Game> = read_into_vec(Path::new("resources/input2.txt"))?;
|
||||
let mut sum: u32 = 0;
|
||||
|
||||
let max_red = 12;
|
||||
let max_green = 13;
|
||||
let max_blue = 14;
|
||||
|
||||
for game in games.iter() {
|
||||
let mut valid: bool = true;
|
||||
for pull in game.pulls.iter() {
|
||||
for group in pull.groups.iter() {
|
||||
match group.color {
|
||||
Color::Red => if group.count > max_red { valid = false },
|
||||
Color::Green => if group.count > max_green { valid = false },
|
||||
Color::Blue => if group.count > max_blue { valid = false },
|
||||
}
|
||||
if valid == false { break };
|
||||
}
|
||||
if valid == false { break };
|
||||
valid = true;
|
||||
}
|
||||
if valid { sum += game.id }
|
||||
}
|
||||
|
||||
Ok(println!("{}", sum))
|
||||
}
|
||||
|
||||
pub fn two_two() -> Result<(), AdventError> {
|
||||
let games: Vec<Game> = read_into_vec(Path::new("resources/input2.txt"))?;
|
||||
let mut power: u32 = 0;
|
||||
|
||||
for game in games.iter() {
|
||||
let mut max_nr_red = 0;
|
||||
let mut max_nr_green = 0;
|
||||
let mut max_nr_blue = 0;
|
||||
for pull in game.pulls.iter() {
|
||||
for group in pull.groups.iter() {
|
||||
match group.color {
|
||||
Color::Red => max_nr_red = max(max_nr_red, group.count),
|
||||
Color::Green => max_nr_green = max(max_nr_green, group.count),
|
||||
Color::Blue => max_nr_blue = max(max_nr_blue, group.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
power += max_nr_red * max_nr_green * max_nr_blue;
|
||||
}
|
||||
|
||||
Ok(println!("{}", power))
|
||||
}
|
||||
Reference in New Issue
Block a user