Skip to content

Commit 654931d

Browse files
committed
Parallelize day 6 part 2 (~83.2% improvement)
1 parent 8c187e0 commit 654931d

File tree

4 files changed

+108
-20
lines changed

4 files changed

+108
-20
lines changed

Cargo.lock

+84
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ tinyjson = "2.5.1"
2929

3030
# Solution dependencies
3131
itertools = "0.13.0"
32+
pariter = "0.5.1"
3233
regex = "1.11.1"

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
2020
<!--- benchmarking table --->
2121
## Benchmarks
2222

23-
| Day | Part 1 | Part 2 |
24-
|:---------------------------------------:| :---: | :---: |
25-
| [Day 1](./src/bin/01.rs) | `61.4µs` | `106.8µs` |
26-
| [Day 2](./src/bin/02.rs) | `220.6µs` | `404.8µs` |
27-
| [Day 3](./src/bin/03.rs) | `561.0µs` | `565.5µs` |
28-
| [Day 4](./src/bin/04.rs) | `560.8µs` | `161.9µs` |
29-
| [Day 5](./src/bin/05.rs) | `344.6µs` | `323.6µs` |
30-
| [Day 6](./src/bin/06.rs) (unoptimized) | `494.4µs` | `956.4ms` |
31-
32-
**Total: 960.21ms**
23+
| Day | Part 1 | Part 2 |
24+
| :---: | :---: | :---: |
25+
| [Day 1](./src/bin/01.rs) | `61.4µs` | `106.8µs` |
26+
| [Day 2](./src/bin/02.rs) | `220.6µs` | `404.8µs` |
27+
| [Day 3](./src/bin/03.rs) | `561.0µs` | `565.5µs` |
28+
| [Day 4](./src/bin/04.rs) | `560.8µs` | `161.9µs` |
29+
| [Day 5](./src/bin/05.rs) | `344.6µs` | `323.6µs` |
30+
| [Day 6](./src/bin/06.rs) | `499.6µs` | `160.3ms` |
31+
32+
**Total: 164.11ms**
3333
<!--- benchmarking table --->
3434

3535
---

src/bin/06.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashSet;
22
use itertools::Itertools;
3+
use pariter::{scope, IteratorExt};
34

45
advent_of_code::solution!(6);
56

@@ -107,20 +108,22 @@ pub fn part_one(input: &str) -> Option<u32> {
107108
}
108109

109110
pub fn part_two(input: &str) -> Option<u32> {
110-
let mut obstructions = 0;
111111
let mut map = Map::parse(input);
112112
map.predict_path();
113113
let mut original_path = map.visited_pos;
114114
original_path.remove(&map.starting_pos);
115-
for pos in original_path {
116-
map.obstacles.insert(pos);
117-
map.visited_pos = HashSet::new();
118-
if map.has_loop() {
119-
obstructions += 1;
120-
}
121-
map.obstacles.remove(&pos);
122-
}
123-
Some(obstructions)
115+
Some(scope(|scope| original_path.iter()
116+
.parallel_filter_scoped(scope, move |pos| {
117+
let mut new_obstacles = map.obstacles.clone();
118+
new_obstacles.insert(**pos);
119+
let mut map = Map {
120+
obstacles: new_obstacles,
121+
visited_pos: HashSet::new(),
122+
..map
123+
};
124+
map.has_loop()
125+
})
126+
.count() as u32).unwrap())
124127
}
125128

126129
#[cfg(test)]

0 commit comments

Comments
 (0)