From b5185d38b2479389279367d1887e9f4b6909ae0a Mon Sep 17 00:00:00 2001 From: Dominic Date: Wed, 13 Dec 2023 00:15:49 +0100 Subject: [PATCH] day 12 part 2: too slow bruteforce attempt --- Cargo.lock | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/day12.rs | 28 ++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 64b3d2c..4e64d89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,7 @@ dependencies = [ "indexmap", "itertools", "paste", + "rayon", ] [[package]] @@ -49,6 +50,12 @@ dependencies = [ "yansi", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bit-vec" version = "0.6.3" @@ -80,6 +87,39 @@ dependencies = [ "stacker", ] +[[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 = "either" version = "1.9.0" @@ -127,6 +167,15 @@ version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.18.0" @@ -166,6 +215,32 @@ dependencies = [ "proc-macro2", ] +[[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 = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "stacker" version = "0.1.15" diff --git a/Cargo.toml b/Cargo.toml index 1227dce..60e5afd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ chumsky = "0.9" indexmap = "2" itertools = "0.12" paste = "1" +rayon = "1.8" diff --git a/src/bin/day12.rs b/src/bin/day12.rs index 38e8642..f20da63 100644 --- a/src/bin/day12.rs +++ b/src/bin/day12.rs @@ -2,6 +2,7 @@ use aoc23::read; use chumsky::{prelude::*, text::int}; +use rayon::iter::{IntoParallelRefIterator as _, ParallelIterator as _}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] enum Cell { @@ -105,5 +106,32 @@ fn main() -> anyhow::Result<()> { .sum::() ); + println!( + "{}", + grid.par_iter() + .map(|row| { + let cells: Vec = row + .cells + .iter() + .chain(row.cells.iter()) + .chain(row.cells.iter()) + .chain(row.cells.iter()) + .chain(row.cells.iter()) + .copied() + .collect(); + let expected: Vec = row + .expected + .iter() + .chain(row.expected.iter()) + .chain(row.expected.iter()) + .chain(row.expected.iter()) + .chain(row.expected.iter()) + .copied() + .collect(); + combinations(None, &cells, 0, &expected) + }) + .sum::() + ); + Ok(()) }