day 19 b
This commit is contained in:
28
src/day19.rs
28
src/day19.rs
@@ -1,4 +1,4 @@
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
use std::collections::HashSet;
|
||||
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
|
||||
@@ -49,26 +49,12 @@ fn starts(a: &str, pattern: &str) -> Vec<usize> {
|
||||
}
|
||||
|
||||
#[aoc(day19, part2)]
|
||||
fn part2((transformations, target): &(Vec<Transformation>, String)) -> usize {
|
||||
search("e", transformations, target)
|
||||
}
|
||||
|
||||
fn search(base: &str, transformations: &[Transformation], target: &str) -> usize {
|
||||
let mut seen = HashSet::new();
|
||||
seen.insert(base.to_string());
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back((base.to_string(), 0));
|
||||
while let Some((from, depth)) = queue.pop_front() {
|
||||
for possibility in possibilities(transformations, &from) {
|
||||
if possibility == target {
|
||||
return depth + 1;
|
||||
}
|
||||
if seen.insert(possibility.clone()) {
|
||||
queue.push_back((possibility, depth + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
unreachable!("No solution was found");
|
||||
fn part2((_transformations, target): &(Vec<Transformation>, String)) -> usize {
|
||||
let l = target.chars().filter(|c| c.is_uppercase()).count();
|
||||
let cy = target.chars().filter(|c| *c == 'Y').count();
|
||||
let crn = target.split("Rn").count() - 1;
|
||||
let car = target.split("Ar").count() - 1;
|
||||
l - crn - car - 2 * cy - 1
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user