perf: use par_iter

This commit is contained in:
Lucy 2024-12-08 10:08:09 +01:00
parent 8d74075bd9
commit e01ba930ff
4 changed files with 73 additions and 10 deletions

52
Cargo.lock generated
View file

@ -31,6 +31,7 @@ name = "aoc2024"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"color-eyre", "color-eyre",
"rayon",
"regex", "regex",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -94,6 +95,37 @@ dependencies = [
"tracing-error", "tracing-error",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]] [[package]]
name = "eyre" name = "eyre"
version = "0.6.12" version = "0.6.12"
@ -219,6 +251,26 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.1" version = "1.11.1"

View file

@ -19,6 +19,7 @@ day7 = []
[dependencies] [dependencies]
color-eyre = "0.6.3" color-eyre = "0.6.3"
rayon = "1.10.0"
regex = "1.11.1" regex = "1.11.1"
tracing = "0.1.41" tracing = "0.1.41"
tracing-subscriber = "0.3.19" tracing-subscriber = "0.3.19"

View file

@ -1,5 +1,8 @@
use std::collections::HashSet; use std::collections::HashSet;
use rayon::iter::ParallelBridge;
use rayon::prelude::*;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
enum Direction { enum Direction {
Up, Up,
@ -98,11 +101,14 @@ pub fn pt2(input: &str) -> usize {
let mut initial_direction = Direction::Up; let mut initial_direction = Direction::Up;
let original_grid: Vec<_> = parse_grid(input, &mut initial_direction, &mut initial_position); let original_grid: Vec<_> = parse_grid(input, &mut initial_direction, &mut initial_position);
let mut result = 0; original_grid
for (y, row) in original_grid.iter().enumerate() { .iter()
for (x, _) in row.iter().enumerate() { .enumerate()
.flat_map(|(y, row)| (0..row.len()).map(move |x| (x, y)))
.par_bridge()
.filter(|&(x, y)| {
if (x, y) == initial_position { if (x, y) == initial_position {
continue; return false;
} }
let mut grid = original_grid.clone(); let mut grid = original_grid.clone();
grid[y][x] = Field::Blocked; grid[y][x] = Field::Blocked;
@ -158,12 +164,9 @@ pub fn pt2(input: &str) -> usize {
Field::Guard(_) => unreachable!(), Field::Guard(_) => unreachable!(),
} }
} }
if !is_valid { !is_valid
result += 1; })
}; .count()
}
}
result
} }
fn parse_grid( fn parse_grid(

View file

@ -1,6 +1,9 @@
use rayon::iter::ParallelBridge;
use rayon::prelude::*;
pub fn pt1(input: &str) -> usize { pub fn pt1(input: &str) -> usize {
input input
.lines() .lines()
.par_bridge()
.map(|line| line.split_once(": ").unwrap()) .map(|line| line.split_once(": ").unwrap())
.map(|(res, args)| { .map(|(res, args)| {
( (
@ -25,6 +28,9 @@ fn can_calibrate((res, args): &(usize, Vec<usize>)) -> bool {
} else { } else {
current_res *= arg; current_res *= arg;
} }
if current_res > *res {
return false;
}
i >>= 1; i >>= 1;
} }
current_res == *res current_res == *res
@ -57,6 +63,7 @@ fn can_calibrate_concat((res, args): &(usize, Vec<usize>)) -> bool {
pub fn pt2(input: &str) -> usize { pub fn pt2(input: &str) -> usize {
input input
.lines() .lines()
.par_bridge()
.map(|line| line.split_once(": ").unwrap()) .map(|line| line.split_once(": ").unwrap())
.map(|(res, args)| { .map(|(res, args)| {
( (