|
| 1 | +//! [aoc](https://adventofcode.com/2024/day/19) |
| 2 | +
|
| 3 | +use crate::aoc::{PuzzleInput, PuzzleMetaData, PuzzleResult}; |
| 4 | +use core::cmp::Reverse; |
| 5 | +use std::cmp::min; |
| 6 | +use std::collections::{BinaryHeap, HashMap, HashSet}; |
| 7 | + |
| 8 | +pub fn metadata() -> PuzzleMetaData<'static> { |
| 9 | + PuzzleMetaData { |
| 10 | + year: 2024, |
| 11 | + day: 19, |
| 12 | + title: "Linen Layout", |
| 13 | + solution: ("247", "692596560138745"), |
| 14 | + example_solutions: vec![("6", "16")], |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type ItemType = i64; |
| 19 | + |
| 20 | +pub fn solve(input: PuzzleInput) -> PuzzleResult { |
| 21 | + // ---------- Parse and Check input |
| 22 | + if input.len() < 3 || !input[1].is_empty() { |
| 23 | + Err("patterns and designs must be separated by an empty line")?; |
| 24 | + } |
| 25 | + let patterns = input[0].split(", ").collect::<Vec<_>>(); |
| 26 | + let max_pattern = patterns.iter().map(|&x| x.len()).max().unwrap(); |
| 27 | + let designs = &input[2..]; |
| 28 | + // ---------- Part 1 + 2 |
| 29 | + let mut lookup = HashSet::new(); |
| 30 | + for &pattern in &patterns { |
| 31 | + lookup.insert(pattern.to_owned()); |
| 32 | + } |
| 33 | + let mut ans1 = 0; |
| 34 | + let mut ans2 = 0; |
| 35 | + for &design in designs { |
| 36 | + let mut found_sol = false; |
| 37 | + let mut has_partial = HashMap::<usize, ItemType>::new(); |
| 38 | + let mut q = BinaryHeap::new(); |
| 39 | + has_partial.insert(0, 1); |
| 40 | + q.push(Reverse(0)); |
| 41 | + while let Some(Reverse(pos)) = q.pop() { |
| 42 | + let prev_sols = *has_partial.get(&pos).unwrap_or(&1); |
| 43 | + if pos == design.len() { |
| 44 | + if !found_sol { |
| 45 | + ans1 += 1; |
| 46 | + found_sol = true; |
| 47 | + } |
| 48 | + ans2 += *has_partial.get(&pos).unwrap_or(&1); |
| 49 | + continue; |
| 50 | + } |
| 51 | + let max_to_pos = min(design.len(), pos + max_pattern); |
| 52 | + for to_pos in (pos + 1)..=max_to_pos { |
| 53 | + if !lookup.contains(&design[pos..to_pos]) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + if !has_partial.contains_key(&to_pos) { |
| 57 | + q.push(Reverse(to_pos)); |
| 58 | + } |
| 59 | + has_partial |
| 60 | + .entry(to_pos) |
| 61 | + .and_modify(|counter| *counter += prev_sols) |
| 62 | + .or_insert(prev_sols); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + Ok((ans1.to_string(), ans2.to_string())) |
| 67 | +} |
| 68 | + |
| 69 | +// ------------------------------------------------------------ |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + use crate::aoc::runner::tests::*; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn example1() { |
| 77 | + test_case(metadata, solve, 1); |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn puzzle() { |
| 82 | + test_case(metadata, solve, 0); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn invalid_single_line() { |
| 87 | + test_invalid_msg( |
| 88 | + &[&"a, b", &"a", &"ab"], |
| 89 | + solve, |
| 90 | + "patterns and designs must be separated by an empty line", |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments