File tree 4 files changed +108
-20
lines changed
4 files changed +108
-20
lines changed Original file line number Diff line number Diff line change @@ -29,4 +29,5 @@ tinyjson = "2.5.1"
29
29
30
30
# Solution dependencies
31
31
itertools = " 0.13.0"
32
+ pariter = " 0.5.1"
32
33
regex = " 1.11.1"
Original file line number Diff line number Diff line change @@ -20,16 +20,16 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
20
20
<!-- - benchmarking table --->
21
21
## Benchmarks
22
22
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 **
33
33
<!-- - benchmarking table --->
34
34
35
35
---
Original file line number Diff line number Diff line change 1
1
use std:: collections:: HashSet ;
2
2
use itertools:: Itertools ;
3
+ use pariter:: { scope, IteratorExt } ;
3
4
4
5
advent_of_code:: solution!( 6 ) ;
5
6
@@ -107,20 +108,22 @@ pub fn part_one(input: &str) -> Option<u32> {
107
108
}
108
109
109
110
pub fn part_two ( input : & str ) -> Option < u32 > {
110
- let mut obstructions = 0 ;
111
111
let mut map = Map :: parse ( input) ;
112
112
map. predict_path ( ) ;
113
113
let mut original_path = map. visited_pos ;
114
114
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 ( ) )
124
127
}
125
128
126
129
#[ cfg( test) ]
You can’t perform that action at this time.
0 commit comments