Add day two, holy shit I overcomplicated this so much lol
This commit is contained in:
parent
42861d1149
commit
489f76161e
1 changed files with 87 additions and 1 deletions
|
@ -83,9 +83,95 @@ fn day_1(input: String) {
|
|||
|
||||
println!("{result:?}");
|
||||
}
|
||||
/// Struct for Day 2
|
||||
#[derive(Default, Debug)]
|
||||
struct Game {
|
||||
id: usize,
|
||||
red: usize,
|
||||
green: usize,
|
||||
blue: usize,
|
||||
max_red: usize,
|
||||
max_green: usize,
|
||||
max_blue: usize,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn new(id: usize) -> Self {
|
||||
Game {
|
||||
id,
|
||||
max_red: 12,
|
||||
max_blue: 14,
|
||||
max_green: 13,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn check(&self) -> bool {
|
||||
self.red <= self.max_red && self.blue <= self.max_blue && self.green <= self.max_green
|
||||
}
|
||||
|
||||
fn add_red(&mut self, count: usize) -> &mut Self {
|
||||
if count > self.red {
|
||||
self.red = count;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
fn add_blue(&mut self, count: usize) -> &mut Self {
|
||||
if count > self.blue {
|
||||
self.blue = count;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
fn add_green(&mut self, count: usize) -> &mut Self {
|
||||
if count > self.green {
|
||||
self.green = count;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn day_2(input: String) {
|
||||
todo!()
|
||||
let result: Vec<Game> = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut line = line.strip_prefix("Game ").unwrap().split(':');
|
||||
let id = line.next().unwrap().to_string().parse().unwrap();
|
||||
let draws = line
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(';')
|
||||
.map(|v| v.split(',').collect::<Vec<&str>>())
|
||||
.collect::<Vec<Vec<&str>>>();
|
||||
|
||||
let mut game = Game::new(id);
|
||||
|
||||
for mut draw in draws {
|
||||
while let Some(balls) = draw.pop() {
|
||||
let mut balls = balls.split_whitespace();
|
||||
let count = balls.next().unwrap().to_string().parse().unwrap();
|
||||
let color = balls.next().unwrap();
|
||||
|
||||
match color {
|
||||
"red" => game.add_red(count),
|
||||
"blue" => game.add_blue(count),
|
||||
"green" => game.add_green(count),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
game
|
||||
})
|
||||
.collect();
|
||||
|
||||
let result1: usize = result.iter().filter(|g| g.check()).map(|v| v.id).sum();
|
||||
let result2: usize = result.iter().map(|g| g.red * g.blue * g.green).sum();
|
||||
|
||||
println!("{result1}");
|
||||
println!("{result2}")
|
||||
}
|
||||
|
||||
fn day_3(input: String) {
|
||||
|
|
Loading…
Reference in a new issue