Completed day 2. Needs speed improvements
This commit is contained in:
48
day_02/src/lib.rs
Normal file
48
day_02/src/lib.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user