day 19 part 2
This commit is contained in:
37
src/day19.rs
37
src/day19.rs
@@ -1,4 +1,7 @@
|
|||||||
use std::{collections::HashSet, fs::read_to_string};
|
use std::{
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
fs::read_to_string,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn day_main() {
|
pub fn day_main() {
|
||||||
let input = read_to_string("input/day19.txt").unwrap();
|
let input = read_to_string("input/day19.txt").unwrap();
|
||||||
@@ -29,16 +32,38 @@ fn possible(line: &str, towels: &HashSet<&str>) -> bool {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(_input: &str) -> RiddleResult {
|
fn countp<'a>(
|
||||||
0
|
line: &'a str,
|
||||||
|
towels: &HashSet<&str>,
|
||||||
|
solved: &mut HashMap<&'a str, usize>,
|
||||||
|
) -> usize {
|
||||||
|
if line.is_empty() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if let Some(count) = solved.get(line) {
|
||||||
|
return *count;
|
||||||
|
}
|
||||||
|
let mut summed = 0;
|
||||||
|
for t in towels.iter() {
|
||||||
|
if let Some(suffix) = line.strip_prefix(t) {
|
||||||
|
summed += countp(suffix, towels, solved);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
solved.insert(line, summed);
|
||||||
|
summed
|
||||||
|
}
|
||||||
|
fn part2(input: &str) -> RiddleResult {
|
||||||
|
let (a, b) = input.split_once("\n\n").unwrap();
|
||||||
|
let towels: HashSet<&str> = a.split(", ").collect();
|
||||||
|
b.lines()
|
||||||
|
.map(|line| countp(line, &towels, &mut HashMap::new()))
|
||||||
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use regex::bytes::SetMatchesIntoIter;
|
|
||||||
|
|
||||||
use crate::day19::possible;
|
use crate::day19::possible;
|
||||||
|
|
||||||
use super::{part1, part2};
|
use super::{part1, part2};
|
||||||
@@ -68,6 +93,6 @@ bbrgwb
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test2() {
|
fn test2() {
|
||||||
assert_eq!(part2(TEST_INPUT), 0);
|
assert_eq!(part2(TEST_INPUT), 16);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user