Skip to content

Commit

Permalink
day19
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Dec 19, 2024
1 parent 5f43257 commit 7ffa9ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ opt-level = 3
[[bench]]
name = "bench"
harness = false

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(day19_series)'] }
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ fi
echo $msg

cargo test test$suffix -- --nocapture
cargo bench bench$suffix
cargo bench bench$suffix --config 'build.rustflags=["--cfg", "day19_series"]'
32 changes: 24 additions & 8 deletions src/day19.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
use rayon::prelude::*;
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};

#[cfg(not(day19_series))]
use rayon::prelude::*;

#[cfg(not(day19_series))]
macro_rules! iter {
($input:expr) => {
$input.par_iter()
};
}

#[cfg(day19_series)]
macro_rules! iter {
($input:expr) => {
$input.iter()
};
}

unsafe fn parse(input: &str) -> (FxHashMap<usize, Vec<&str>>, Vec<&str>) {
let mut lines = input.lines();

Expand Down Expand Up @@ -28,8 +44,7 @@ pub fn part1(input: &str) -> usize {
unsafe fn inner1(input: &str) -> usize {
let (patterns_by_length, designs) = parse(input);

designs
.par_iter()
iter!(designs)
.filter(|design| can_make_design(design, &patterns_by_length))
.count()
}
Expand Down Expand Up @@ -85,8 +100,7 @@ pub fn part2(input: &str) -> u64 {
unsafe fn inner2(input: &str) -> u64 {
let (patterns_by_length, designs) = parse(input);

designs
.par_iter()
iter!(designs)
.map(|design| count_possible_combinations(design, &patterns_by_length))
.sum()
}
Expand All @@ -101,7 +115,9 @@ unsafe fn count_possible_combinations(

for i in (0..design.len()).rev() {
for (&length, patterns) in patterns_by_length.iter() {
if design.len() - i < length {
let next_pos = i + length;

if design.len() < next_pos {
continue;
}

Expand All @@ -112,8 +128,8 @@ unsafe fn count_possible_combinations(
}
}

if *dp.get_unchecked(i + length) > 0 {
*dp.get_unchecked_mut(i) += *dp.get_unchecked(i + length);
if *dp.get_unchecked(next_pos) > 0 {
*dp.get_unchecked_mut(i) += *dp.get_unchecked(next_pos);
break;
}
}
Expand Down

0 comments on commit 7ffa9ad

Please sign in to comment.