Skip to content

Commit

Permalink
Day 02b
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 2, 2023
1 parent 7184662 commit c7f13f7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
81 changes: 81 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions day02/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
aoc = { path = "../aoc" }
color-eyre = "0.6.2"
nom = "7.1.3"
rayon = "1.8.0"
39 changes: 39 additions & 0 deletions day02/src/bin/day02b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2023 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use rayon::prelude::*;
use std::io::{stdin, BufRead};

use day02::*;

fn process(bufin: impl BufRead) -> Result<u32> {
let input = parser::parse(bufin)?;
Ok(input
.into_par_iter()
.map(|game| {
game.into_iter()
.fold(Set::new(), |maxset, sets| {
sets.into_iter().fold(maxset, |mut maxset, (color, num)| {
let e = maxset.entry(color).or_default();
*e = std::cmp::max(*e, num);
maxset
})
})
.into_values()
.product::<u32>()
})
.sum())
}

#[test]
fn test() -> Result<()> {
assert_eq!(process(EXAMPLE.as_bytes())?, 2286);
Ok(())
}

fn main() -> Result<()> {
color_eyre::install()?;
println!("{}", process(stdin().lock())?);
Ok(())
}
2 changes: 1 addition & 1 deletion day02/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
";

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum Color {
Red,
Green,
Expand Down

0 comments on commit c7f13f7

Please sign in to comment.