|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use crate::solution::{AocError, Solution}; |
| 4 | + |
| 5 | +fn parse(input: &str) -> Result<(Vec<String>, Vec<String>), AocError> { |
| 6 | + let (towels, designs) = input |
| 7 | + .split_once("\n\n") |
| 8 | + .ok_or_else(|| AocError::parse(input, "Missing towel and designs sections"))?; |
| 9 | + |
| 10 | + let towels: Vec<String> = towels.split(", ").map(|towel| towel.to_owned()).collect(); |
| 11 | + let designs: Vec<String> = designs.lines().map(|towel| towel.to_owned()).collect(); |
| 12 | + |
| 13 | + Ok((towels, designs)) |
| 14 | +} |
| 15 | + |
| 16 | +fn count_possible(towels: &[String], design: &str, cache: &mut HashMap<String, usize>) -> usize { |
| 17 | + if design.is_empty() { |
| 18 | + return 1; |
| 19 | + } |
| 20 | + |
| 21 | + if let Some(&count) = cache.get(design) { |
| 22 | + return count; |
| 23 | + } |
| 24 | + |
| 25 | + let possible = towels |
| 26 | + .iter() |
| 27 | + .filter_map(|towel| design.strip_suffix(towel)) |
| 28 | + .map(|remaining| count_possible(towels, remaining, cache)) |
| 29 | + .sum(); |
| 30 | + |
| 31 | + cache.insert(design.to_owned(), possible); |
| 32 | + |
| 33 | + possible |
| 34 | +} |
| 35 | + |
| 36 | +pub struct Day19; |
| 37 | +impl Solution for Day19 { |
| 38 | + type A = usize; |
| 39 | + type B = usize; |
| 40 | + |
| 41 | + fn default_input(&self) -> &'static str { |
| 42 | + include_str!("../../../inputs/2024/day19.txt") |
| 43 | + } |
| 44 | + |
| 45 | + fn part_1(&self, input: &str) -> Result<usize, AocError> { |
| 46 | + let (towels, designs) = parse(input)?; |
| 47 | + |
| 48 | + let mut cache = HashMap::new(); |
| 49 | + let possible = designs |
| 50 | + .iter() |
| 51 | + .filter(|design| count_possible(&towels, design, &mut cache) > 0) |
| 52 | + .count(); |
| 53 | + |
| 54 | + Ok(possible) |
| 55 | + } |
| 56 | + |
| 57 | + fn part_2(&self, input: &str) -> Result<usize, AocError> { |
| 58 | + let (towels, designs) = parse(input)?; |
| 59 | + |
| 60 | + let mut cache = HashMap::new(); |
| 61 | + let possible = designs |
| 62 | + .iter() |
| 63 | + .map(|design| count_possible(&towels, design, &mut cache)) |
| 64 | + .sum(); |
| 65 | + |
| 66 | + Ok(possible) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn it_solves_part1_example() { |
| 76 | + assert_eq!( |
| 77 | + Day19.part_1( |
| 78 | + "r, wr, b, g, bwu, rb, gb, br\n\ |
| 79 | + \n\ |
| 80 | + brwrr\n\ |
| 81 | + bggr\n\ |
| 82 | + gbbr\n\ |
| 83 | + rrbgbr\n\ |
| 84 | + ubwu\n\ |
| 85 | + bwurrg\n\ |
| 86 | + brgr\n\ |
| 87 | + bbrgwb" |
| 88 | + ), |
| 89 | + Ok(6) |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn it_solves_part2_example() { |
| 95 | + assert_eq!( |
| 96 | + Day19.part_2( |
| 97 | + "r, wr, b, g, bwu, rb, gb, br\n\ |
| 98 | + \n\ |
| 99 | + brwrr\n\ |
| 100 | + bggr\n\ |
| 101 | + gbbr\n\ |
| 102 | + rrbgbr\n\ |
| 103 | + ubwu\n\ |
| 104 | + bwurrg\n\ |
| 105 | + brgr\n\ |
| 106 | + bbrgwb" |
| 107 | + ), |
| 108 | + Ok(16) |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments