Skip to content

Commit

Permalink
day_19 part_2
Browse files Browse the repository at this point in the history
  • Loading branch information
0qln committed Dec 21, 2024
1 parent f4f0546 commit 4603173
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/day_19/part_2/dummy_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
r, wr, b, g, bwu, rb, gb, br

brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
42 changes: 42 additions & 0 deletions src/day_19/part_2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::collections::HashMap;

use itertools::Itertools;

use super::parse;

#[cfg(test)]
mod test;

fn num_compositions<'a>(
available: &Vec<&'a str>,
desired: &'a str,
compositions: &mut HashMap<(&'a str, &'a str), usize>,
) -> usize {
available
.iter()
.map(move |towel| {
let key = (desired, *towel);
if compositions.contains_key(&key) {
*compositions.get(&key).unwrap()
} else {
let value = if *towel == desired {
1
} else if desired.starts_with(towel) {
num_compositions(available, desired[towel.len()..].as_ref(), compositions)
} else {
0
};
compositions.insert(key, value);
value
}
})
.sum()
}

pub fn solve(input: &str) -> usize {
let (available, desired) = parse(input);
desired
.iter()
.map(|desired| num_compositions(&available, desired, &mut HashMap::<_, _>::new()))
.sum()
}
16 changes: 16 additions & 0 deletions src/day_19/part_2/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::solve;
use crate::include_using_path;

#[test]
fn dummy_input() {
let input = include_using_path!("dummy_input.txt");
let result = solve(input);
assert_eq!(result, 16);
}

#[test]
fn solve_puzzle() {
let input = include_using_path!("input.txt");
let result = solve(input);
println!("Result: {}", result);
}

0 comments on commit 4603173

Please sign in to comment.