From 0d8abe63863f2f1e376b0e5fd1d2ef09ef69cd5b Mon Sep 17 00:00:00 2001 From: Himangshu Saikia Date: Thu, 19 Dec 2024 06:22:58 +0100 Subject: [PATCH] 2024_19 --- src/bin/2024_19/main.rs | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/bin/2024_19/main.rs diff --git a/src/bin/2024_19/main.rs b/src/bin/2024_19/main.rs new file mode 100644 index 0000000..07621f8 --- /dev/null +++ b/src/bin/2024_19/main.rs @@ -0,0 +1,84 @@ +use std::collections::HashMap; + +use aoc::{common, io}; + +fn possible_ways(patterns: &[&str], towel: &str, map: &mut HashMap) -> usize { + if towel.is_empty() { + return 1; + } + if map.get(towel).is_some() { + return *map.get(towel).unwrap(); + } + let mut ways = 0; + for p in patterns.iter() { + if p.len() > towel.len() { + continue; + } + if **p == towel[0..p.len()] { + ways += possible_ways(patterns, &towel[p.len()..], map); + } + } + map.insert(towel.to_string(), ways); + ways +} + +fn possible(patterns: &[&str], towel: &str, map: &mut HashMap) -> bool { + if towel.is_empty() { + return true; + } + if map.get(towel).is_some() { + return *map.get(towel).unwrap(); + } + let mut status = false; + for p in patterns.iter() { + if p.len() > towel.len() { + continue; + } + if **p == towel[0..p.len()] { + status |= possible(patterns, &towel[p.len()..], map); + } + } + map.insert(towel.to_string(), status); + status +} + +fn solve(input: &str) -> usize { + let mut ans = 0; + let batches = io::line_batches(input); + let patterns = io::tokenize(batches[0][0], ", "); + //println!("{:?}", patterns); + + let mut cache: HashMap = HashMap::new(); + let mut cache2: HashMap = HashMap::new(); + + for towel in batches[1].iter() { + //println!("Checking {}", towel); + if PART == 1 && possible(&patterns, towel, &mut cache) { + ans += 1; + } + if PART == 2 { + ans += possible_ways(&patterns, towel, &mut cache2); + } + } + ans +} + +fn main() { + let input = common::get_input(); + println!("{input:?}"); + common::timed(&input, solve::<1>, true); + common::timed(&input, solve::<2>, false); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_samples() { + let sample_input = + "r, wr, b, g, bwu, rb, gb, br\n\nbrwrr\nbggr\ngbbr\nrrbgbr\nubwu\nbwurrg\nbrgr\nbbrgwb"; + assert_eq!(solve::<1>(sample_input), 6); + assert_eq!(solve::<2>(sample_input), 16); + } +}