|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use common::{solution, Answer}; |
| 4 | + |
| 5 | +solution!("Linen Layout", 19); |
| 6 | + |
| 7 | +fn part_a(input: &str) -> Answer { |
| 8 | + let problem = Problem::parse(input); |
| 9 | + let mut sum = 0; |
| 10 | + |
| 11 | + for i in 0..problem.needed.len() { |
| 12 | + if problem.possible(i) { |
| 13 | + sum += 1; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + sum.into() |
| 18 | +} |
| 19 | + |
| 20 | +fn part_b(input: &str) -> Answer { |
| 21 | + let problem = Problem::parse(input); |
| 22 | + let mut sum = 0; |
| 23 | + |
| 24 | + for i in 0..problem.needed.len() { |
| 25 | + sum += problem.ways(i); |
| 26 | + } |
| 27 | + |
| 28 | + sum.into() |
| 29 | +} |
| 30 | + |
| 31 | +struct Problem { |
| 32 | + sources: Vec<String>, |
| 33 | + needed: Vec<String>, |
| 34 | +} |
| 35 | + |
| 36 | +impl Problem { |
| 37 | + fn parse(input: &str) -> Self { |
| 38 | + let (sources, needed) = input.split_once("\n\n").unwrap(); |
| 39 | + let sources = sources.split(", ").map(|x| x.to_owned()).collect(); |
| 40 | + let needed = needed.lines().map(|x| x.to_owned()).collect(); |
| 41 | + |
| 42 | + Self { sources, needed } |
| 43 | + } |
| 44 | + |
| 45 | + fn possible(&self, design: usize) -> bool { |
| 46 | + fn _inner<'a>( |
| 47 | + memo: &mut HashMap<&'a str, bool>, |
| 48 | + expected: &'a str, |
| 49 | + sources: &[String], |
| 50 | + ) -> bool { |
| 51 | + if let Some(&cache) = memo.get(expected) { |
| 52 | + return cache; |
| 53 | + } |
| 54 | + |
| 55 | + if expected.len() == 0 { |
| 56 | + memo.insert(expected, true); |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + for source in sources { |
| 61 | + if expected.len() >= source.len() |
| 62 | + && expected.starts_with(source) |
| 63 | + && _inner(memo, &expected[source.len()..], &sources) |
| 64 | + { |
| 65 | + memo.insert(expected, true); |
| 66 | + return true; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + memo.insert(expected, false); |
| 71 | + false |
| 72 | + } |
| 73 | + |
| 74 | + _inner(&mut HashMap::new(), &self.needed[design], &self.sources) |
| 75 | + } |
| 76 | + |
| 77 | + fn ways(&self, design: usize) -> u64 { |
| 78 | + fn _inner<'a>( |
| 79 | + memo: &mut HashMap<&'a str, u64>, |
| 80 | + expected: &'a str, |
| 81 | + sources: &[String], |
| 82 | + ) -> u64 { |
| 83 | + if let Some(&cache) = memo.get(expected) { |
| 84 | + return cache; |
| 85 | + } |
| 86 | + |
| 87 | + if expected.len() == 0 { |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + let mut ways = 0; |
| 92 | + for source in sources { |
| 93 | + if expected.len() >= source.len() && expected.starts_with(source) { |
| 94 | + ways += _inner(memo, &expected[source.len()..], &sources); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + memo.insert(expected, ways); |
| 99 | + ways |
| 100 | + } |
| 101 | + |
| 102 | + _inner(&mut HashMap::new(), &self.needed[design], &self.sources) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod test { |
| 108 | + use indoc::indoc; |
| 109 | + |
| 110 | + const CASE: &str = indoc! {" |
| 111 | + r, wr, b, g, bwu, rb, gb, br |
| 112 | +
|
| 113 | + brwrr |
| 114 | + bggr |
| 115 | + gbbr |
| 116 | + rrbgbr |
| 117 | + ubwu |
| 118 | + bwurrg |
| 119 | + brgr |
| 120 | + bbrgwb |
| 121 | + "}; |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn part_a() { |
| 125 | + assert_eq!(super::part_a(CASE), 6.into()); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn part_b() { |
| 130 | + assert_eq!(super::part_b(CASE), 16.into()); |
| 131 | + } |
| 132 | +} |
0 commit comments