use std::collections::HashMap; #[path = "../lib.rs"] mod lib; fn count_ways(pattern: &str, towels: &Vec<&str>, cache: &mut HashMap) -> usize { if pattern == "" { return 1; } else if let Some(count) = cache.get(pattern) { return *count; } let mut count = 0; for towel in towels.iter() { if let Some(tail) = pattern.strip_prefix(towel) { count += count_ways(tail, towels, cache); } } cache.insert(pattern.to_string(), count); return count; } fn main() { let mut input = lib::iter_input(); let mut cache = HashMap::new(); let line = input.next().unwrap(); let towels = line.split(", ").collect(); assert_eq!(input.next(), Some("".to_string())); let mut sum1 = 0; let mut sum2 = 0; for line in input { let count = count_ways(&line, &towels, &mut cache); if count != 0 { sum1 += 1; } sum2 += count; } println!("part1: {}", sum1); println!("part2: {}", sum2); }