Complete part on of 2023 day 2

This commit is contained in:
Timothy Warren 2023-12-05 14:23:04 -05:00
parent 678f25efb5
commit c655326e5b

View File

@ -13,10 +13,8 @@ fn parse_colors(raw_colors: Vec<&str>) -> Cubes {
let mut green = 0usize; let mut green = 0usize;
let mut blue = 0usize; let mut blue = 0usize;
println!("{:#?}", raw_colors);
for raw_color in raw_colors { for raw_color in raw_colors {
let color_split: Vec<&str> = raw_color.split(' ').collect(); let color_split: Vec<&str> = raw_color.split(' ').map(|s| s.trim()).collect();
let color_split: Vec<&str> = color_split.into_iter().filter(|s| s.len() > 0).collect(); let color_split: Vec<&str> = color_split.into_iter().filter(|s| s.len() > 0).collect();
let [num_str, color] = color_split[..] else { let [num_str, color] = color_split[..] else {
panic!("Bad color row") panic!("Bad color row")
@ -67,15 +65,28 @@ fn is_valid(reference: &Cubes, comparison: &Cubes) -> bool {
} }
fn validate_games(reference: &Cubes, games: CubeMap) -> Vec<usize> { fn validate_games(reference: &Cubes, games: CubeMap) -> Vec<usize> {
games
.into_iter()
.filter(|(_, cubes)| cubes.iter().all(|c| is_valid(reference, c)))
.map(|(id, _)| id)
.collect()
} }
fn main() { fn main() {
let file_str = include_str!("input.txt"); let file_str = include_str!("input.txt");
let mut map: CubeMap = HashMap::new(); let mut map: CubeMap = HashMap::new();
let reference = Cubes {
red: 12,
green: 13,
blue: 14,
};
file_str file_str
.split('\n') .split('\n')
.for_each(|line| parse_game(line, &mut map)); .for_each(|line| parse_game(line, &mut map));
println!("{:#?}", map);
let valid = validate_games(&reference, map);
let sum: usize = valid.into_iter().sum();
println!("Part 1 Sum of valid games: {}", sum);
} }