Skip to content

Commit ef33f8e

Browse files
committed
[Rust/2023/12] Use ndarray
1 parent 3a72b49 commit ef33f8e

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

Rust/2023/12.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(test, iter_intersperse)]
22

3+
use ndarray::Array2;
34
use rayon::prelude::*;
45

56
type Input = Vec<Report>;
@@ -45,8 +46,8 @@ fn setup(input: &str) -> Input {
4546
}
4647

4748
fn count(report: &Report) -> usize {
48-
let mut dp = vec![vec![0; report.groups.len() + 1]; report.springs.len() + 1];
49-
dp[report.springs.len()][report.groups.len()] = 1;
49+
let mut dp = Array2::zeros((report.springs.len() + 1, report.groups.len() + 1));
50+
dp[[report.springs.len(), report.groups.len()]] = 1;
5051
for i in (0..report.springs.len()).rev() {
5152
for j in 0..report.groups.len() + 1 {
5253
if matches!(report.springs[i], Spring::Damaged | Spring::Unknown)
@@ -56,15 +57,15 @@ fn count(report: &Report) -> usize {
5657
&& (i + report.groups[j] >= report.springs.len()
5758
|| report.springs[i + report.groups[j]] != Spring::Damaged)
5859
{
59-
dp[i][j] += dp[report.springs.len().min(i + report.groups[j] + 1)][j + 1];
60+
dp[[i, j]] += dp[[report.springs.len().min(i + report.groups[j] + 1), j + 1]];
6061
}
6162

6263
if matches!(report.springs[i], Spring::Operational | Spring::Unknown) {
63-
dp[i][j] += dp[report.springs.len().min(i + 1)][j];
64+
dp[[i, j]] += dp[[report.springs.len().min(i + 1), j]];
6465
}
6566
}
6667
}
67-
dp[0][0]
68+
dp[[0, 0]]
6869
}
6970

7071
fn part1(input: &Input) -> usize {

Rust/Cargo.lock

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

Rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rpath = false
2121
[dependencies]
2222
counter = "0.5.7"
2323
itertools = "0.11.0"
24+
ndarray = "0.15.6"
2425
num = "0.4.1"
2526
paste = "1.0.14"
2627
rayon = "1.8.0"

0 commit comments

Comments
 (0)