Files
advent-of-code-2023/day_02/src/lib.rs
2023-12-01 22:38:12 -07:00

49 lines
1.8 KiB
Rust

pub fn part1(input: &str) -> String {
let output: u32 = input.lines().map(|line| {
let mut iter = line.split(":");
let game_id = iter.next().unwrap().split(" ").last().unwrap().parse::<u32>().unwrap();
let results = iter.next().unwrap().split(&[';', ',']).map(|result| {
let mut result_iter = result.trim().split(" ");
let count = result_iter.next().unwrap().parse::<u32>().unwrap();
let color = result_iter.next().unwrap();
if color == "red" && count > 12 {
false
} else if color == "green" && count > 13 {
false
} else if color == "blue" && count > 14 {
false
} else {
true
}
}).all(|x| {x});
match results {
true => game_id,
false => 0,
}
}).sum();
format!("{}", output)
}
pub fn part2(input: &str) -> String {
let games = input.lines().map(|line| {
let mut iter = line.split(":");
let results_iter = iter.next().unwrap().split(&[';', ',']).map(|result| {
let mut result_iter = result.trim().split(" ");
let count = result_iter.next().unwrap().parse::<u32>().unwrap();
let color = result_iter.next().unwrap();
(count, color)
});
let red_max = results_iter.clone().filter(|x| {x.1 == "red"}).map(|x| {x.0}).max().unwrap();
let green_max = results_iter.clone().filter(|x| {x.1 == "green"}).map(|x| {x.0}).max().unwrap();
let blue_max = results_iter.clone().filter(|x| {x.1 == "blue"}).map(|x| {x.0}).max().unwrap();
red_max * green_max * blue_max
});
games.sum::<u32>().to_string()
}
pub mod prelude {
pub use super::part1;
pub use super::part2;
}