Compare commits
2 Commits
0c3ebf803f
...
b4aaa7857f
| Author | SHA1 | Date | |
|---|---|---|---|
| b4aaa7857f | |||
| ee224046a6 |
98
src/day19.rs
Normal file
98
src/day19.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fs::read_to_string,
|
||||
};
|
||||
|
||||
pub fn day_main() {
|
||||
let input = read_to_string("input/day19.txt").unwrap();
|
||||
let input = input.trim();
|
||||
println!(" part1: {}", part1(input));
|
||||
println!(" part2: {}", part2(input));
|
||||
}
|
||||
|
||||
type RiddleResult = usize;
|
||||
|
||||
fn part1(input: &str) -> RiddleResult {
|
||||
let (a, b) = input.split_once("\n\n").unwrap();
|
||||
let towels: HashSet<&str> = a.split(", ").collect();
|
||||
b.lines().filter(|line| possible(line, &towels)).count()
|
||||
}
|
||||
|
||||
fn possible(line: &str, towels: &HashSet<&str>) -> bool {
|
||||
if line.is_empty() {
|
||||
return true;
|
||||
}
|
||||
for t in towels.iter() {
|
||||
if let Some(suffix) = line.strip_prefix(t) {
|
||||
if possible(suffix, towels) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn countp<'a>(
|
||||
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)]
|
||||
mod test {
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::day19::possible;
|
||||
|
||||
use super::{part1, part2};
|
||||
|
||||
const TEST_INPUT: &str = r"r, wr, b, g, bwu, rb, gb, br
|
||||
|
||||
brwrr
|
||||
bggr
|
||||
gbbr
|
||||
rrbgbr
|
||||
ubwu
|
||||
bwurrg
|
||||
brgr
|
||||
bbrgwb
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
assert_eq!(part1(TEST_INPUT), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solver() {
|
||||
let set = HashSet::from_iter(["r", "wr", "b", "g", "bwu", "rb", "gb", "br"]);
|
||||
assert!(possible("bwurrg", &set));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test2() {
|
||||
assert_eq!(part2(TEST_INPUT), 16);
|
||||
}
|
||||
}
|
||||
@@ -15,5 +15,6 @@ pub mod day14;
|
||||
pub mod day15;
|
||||
pub mod day16;
|
||||
pub mod day17;
|
||||
pub mod day19;
|
||||
// PLACEHOLDER
|
||||
pub mod utils;
|
||||
|
||||
@@ -22,6 +22,7 @@ fn main() {
|
||||
(15, day15::day_main),
|
||||
(16, day16::day_main),
|
||||
(17, day17::day_main),
|
||||
(19, day19::day_main),
|
||||
// PLACEHOLDER
|
||||
]);
|
||||
let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok());
|
||||
|
||||
Reference in New Issue
Block a user