Skip to content

Commit 4edc204

Browse files
committed
Use multithreading to speed things up as each spring is independent
1 parent 459dea9 commit 4edc204

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pie
110110
| 9 | [Mirage Maintenance](https://adventofcode.com/2023/day/9) | [Source](src/year2023/day09.rs) | 19 |
111111
| 10 | [Pipe Maze](https://adventofcode.com/2023/day/10) | [Source](src/year2023/day10.rs) | 60 |
112112
| 11 | [Cosmic Expansion](https://adventofcode.com/2023/day/11) | [Source](src/year2023/day11.rs) | 12 |
113-
| 12 | [Hot Springs](https://adventofcode.com/2023/day/12) | [Source](src/year2023/day12.rs) | 1150 |
113+
| 12 | [Hot Springs](https://adventofcode.com/2023/day/12) | [Source](src/year2023/day12.rs) | 514 |
114114
| 13 | [Point of Incidence](https://adventofcode.com/2023/day/13) | [Source](src/year2023/day13.rs) | 64 |
115115
| 14 | [Parabolic Reflector Dish](https://adventofcode.com/2023/day/14) | [Source](src/year2023/day14.rs) | 661 |
116116
| 15 | [Lens Library](https://adventofcode.com/2023/day/15) | [Source](src/year2023/day15.rs) | 83 |

src/year2023/day12.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@
119119
//! This is equivalent to the prefix sum approach described above but a little clearer to
120120
//! understand however slower to calculate.
121121
use crate::util::parse::*;
122+
use std::sync::atomic::{AtomicU64, Ordering};
123+
use std::thread;
122124

123-
type Input<'a> = Vec<(&'a [u8], Vec<usize>)>;
125+
type Spring<'a> = (&'a [u8], Vec<usize>);
124126

125-
pub fn parse(input: &str) -> Input<'_> {
127+
pub fn parse(input: &str) -> Vec<Spring<'_>> {
126128
input
127129
.lines()
128130
.map(|line| {
@@ -134,15 +136,29 @@ pub fn parse(input: &str) -> Input<'_> {
134136
.collect()
135137
}
136138

137-
pub fn part1(input: &Input<'_>) -> u64 {
139+
pub fn part1(input: &[Spring<'_>]) -> u64 {
138140
solve(input, 1)
139141
}
140142

141-
pub fn part2(input: &Input<'_>) -> u64 {
142-
solve(input, 5)
143+
pub fn part2(input: &[Spring<'_>]) -> u64 {
144+
// Break the work into roughly equally size batches.
145+
let threads = thread::available_parallelism().unwrap().get();
146+
let size = input.len().div_ceil(threads);
147+
let batches: Vec<_> = input.chunks(size).collect();
148+
149+
// Use as many cores as possible to parallelize the calculation.
150+
let shared = AtomicU64::new(0);
151+
152+
thread::scope(|scope| {
153+
for batch in batches {
154+
scope.spawn(|| shared.fetch_add(solve(batch, 5), Ordering::Relaxed));
155+
}
156+
});
157+
158+
shared.load(Ordering::Relaxed)
143159
}
144160

145-
pub fn solve(input: &Input<'_>, repeat: usize) -> u64 {
161+
pub fn solve(input: &[Spring<'_>], repeat: usize) -> u64 {
146162
let mut result = 0;
147163
let mut pattern = Vec::new();
148164
let mut springs = Vec::new();

0 commit comments

Comments
 (0)