Parallelize for more speeeeed

This commit is contained in:
JonOfUs 2023-12-03 13:45:16 +01:00
parent 173dc0f498
commit 66626268af
3 changed files with 97 additions and 3 deletions

87
Cargo.lock generated
View file

@ -7,6 +7,7 @@ name = "advent-of-code-2023"
version = "0.1.0"
dependencies = [
"automod",
"rayon",
"regex",
"scan_fmt",
"serde_json",
@ -21,6 +22,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "automod"
version = "1.0.13"
@ -32,6 +39,51 @@ dependencies = [
"syn",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "itoa"
version = "1.0.9"
@ -44,6 +96,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 = "proc-macro2"
version = "1.0.70"
@ -62,6 +123,26 @@ 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 = "regex"
version = "1.10.2"
@ -106,6 +187,12 @@ dependencies = [
"regex",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.193"

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
automod = "1.0.13"
rayon = "1.8.0"
regex = "1.10.2"
scan_fmt = "0.2.6"
serde_json = "1.0.108"

View file

@ -1,6 +1,8 @@
use rayon::prelude::*;
use regex::Regex;
use std::collections::HashMap;
use std::fs;
use std::sync::{Arc, Mutex};
pub fn solve() {
let path = "res/03/input";
@ -13,11 +15,13 @@ pub fn solve() {
.map(|l| l.chars().collect::<Vec<char>>())
.collect();
let mut possible_gears = HashMap::<(usize, usize), Vec<usize>>::new();
let possible_gears: Arc<Mutex<HashMap<(usize, usize), Vec<usize>>>> =
Arc::new(Mutex::new(HashMap::new()));
let result: usize = contents
.lines()
.enumerate()
.par_bridge()
.map(|(yusize, line)| {
let re = Regex::new(r"(\d+)").unwrap();
let y = yusize as i32;
@ -48,7 +52,8 @@ pub fn solve() {
})
.map(|(x, y)| {
if grid[*y as usize][*x as usize] == '*' {
possible_gears
let mut possible_gears_mut = possible_gears.lock().unwrap();
possible_gears_mut
.entry((*x as usize, *y as usize))
.or_insert(vec![])
.push(number);
@ -70,7 +75,8 @@ pub fn solve() {
println!("Result 1: {}", result);
let result: usize = possible_gears
let possible_gears_mut = possible_gears.lock().unwrap();
let result: usize = possible_gears_mut
.iter()
.filter(|(_, vec)| vec.len() == 2)
.map(|(_, vec)| vec.iter().product::<usize>())