Skip to content

Commit 7c2533c

Browse files
committed
Add par methods to grid using rayon
1 parent 6f0c55c commit 7c2533c

File tree

7 files changed

+87
-3
lines changed

7 files changed

+87
-3
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mygrid/Cargo.lock

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mygrid/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ publish = false
77
name = "mygrid"
88
test = true
99
plugin = false
10+
11+
[dependencies]
12+
rayon = "1.10.0"

mygrid/src/grid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::point::Point;
77
pub struct Grid<T> {
88
pub width: usize,
99
pub height: usize,
10-
content: Vec<T>,
10+
pub(crate) content: Vec<T>,
1111
}
1212

1313
impl<T> Grid<T> {

mygrid/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// contains everything related to grids, points and directions
33
// heavily inspired by the amazing maneatingape repo, from which I learned a lot, plz see:
44
// https://github.com/maneatingape/advent-of-code-rust/blob/main/src/util/point.rs
5+
extern crate rayon;
56

67
pub mod direction;
78
pub mod grid;
9+
pub mod par_grid;
810
pub mod point;

mygrid/src/par_grid.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
2+
use rayon::prelude::*;
3+
4+
use crate::{grid::Grid, point::Point};
5+
6+
impl<T: Sized + Send + Sync + Clone> Grid<T> {
7+
pub fn par_iter(&self) -> impl ParallelIterator<Item = &T> {
8+
self.content.par_iter()
9+
}
10+
11+
pub fn par_iter_item_and_position<'a>(
12+
&'a self,
13+
) -> impl ParallelIterator<Item = (Point, &T)> + 'a {
14+
let width = self.width;
15+
let height = self.height;
16+
self.content
17+
.par_iter()
18+
.enumerate()
19+
.map(move |(i, t)| (Point::new_usize(i / width, i % height), t))
20+
}
21+
}

src/bin/04.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ use mygrid::{
22
direction::{ALL_AROUND, DOWN, LEFT, RIGHT, UP},
33
grid::Grid,
44
};
5+
use rayon::prelude::*;
56

67
advent_of_code::solution!(4);
78

89
pub fn part_one(input: &str) -> Option<u32> {
910
let grid = Grid::new_from_str(input, |c| c);
1011

1112
let res = grid
12-
.iter_item_and_position()
13-
.flat_map(|(point, &c)| ALL_AROUND.iter().map(move |d| (point, c, *d)))
13+
.par_iter_item_and_position()
14+
.flat_map_iter(|(point, &c)| ALL_AROUND.iter().map(move |d| (point, c, *d)))
1415
.filter(|&(_, c, _)| c == 'X')
1516
.filter(|&(point, _, d)| grid.is_in_bounds(point + (d * 3)))
1617
.filter(|&(point, _, d)| {

0 commit comments

Comments
 (0)