From c7f13f78736ac20af03d7939ca71cae866e7ca18 Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Sat, 2 Dec 2023 13:04:44 +0000 Subject: [PATCH] Day 02b --- Cargo.lock | 81 +++++++++++++++++++++++++++++++++++++++++ day02/Cargo.toml | 1 + day02/src/bin/day02b.rs | 39 ++++++++++++++++++++ day02/src/lib.rs | 2 +- 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 day02/src/bin/day02b.rs diff --git a/Cargo.lock b/Cargo.lock index 7831146..637bba4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,12 @@ dependencies = [ "nom", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "backtrace" version = "0.3.69" @@ -91,6 +97,39 @@ dependencies = [ "tracing-error", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + [[package]] name = "day00" version = "0.1.0" @@ -117,8 +156,15 @@ dependencies = [ "aoc", "color-eyre", "nom", + "rayon", ] +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + [[package]] name = "eyre" version = "0.6.9" @@ -159,6 +205,15 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -211,6 +266,26 @@ version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "regex" version = "1.10.2" @@ -246,6 +321,12 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "sharded-slab" version = "0.1.7" diff --git a/day02/Cargo.toml b/day02/Cargo.toml index 3c9ddc3..497dc7c 100644 --- a/day02/Cargo.toml +++ b/day02/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" aoc = { path = "../aoc" } color-eyre = "0.6.2" nom = "7.1.3" +rayon = "1.8.0" diff --git a/day02/src/bin/day02b.rs b/day02/src/bin/day02b.rs new file mode 100644 index 0000000..0631cf3 --- /dev/null +++ b/day02/src/bin/day02b.rs @@ -0,0 +1,39 @@ +// Copyright (C) 2023 Leandro Lisboa Penz +// 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 { + 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::() + }) + .sum()) +} + +#[test] +fn test() -> Result<()> { + assert_eq!(process(EXAMPLE.as_bytes())?, 2286); + Ok(()) +} + +fn main() -> Result<()> { + color_eyre::install()?; + println!("{}", process(stdin().lock())?); + Ok(()) +} diff --git a/day02/src/lib.rs b/day02/src/lib.rs index 85230ed..4797c32 100644 --- a/day02/src/lib.rs +++ b/day02/src/lib.rs @@ -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,