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};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
|
||||||
@@ -49,26 +49,12 @@ fn starts(a: &str, pattern: &str) -> Vec<usize> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day19, part2)]
|
#[aoc(day19, part2)]
|
||||||
fn part2((transformations, target): &(Vec<Transformation>, String)) -> usize {
|
fn part2((_transformations, target): &(Vec<Transformation>, String)) -> usize {
|
||||||
search("e", transformations, target)
|
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;
|
||||||
fn search(base: &str, transformations: &[Transformation], target: &str) -> usize {
|
let car = target.split("Ar").count() - 1;
|
||||||
let mut seen = HashSet::new();
|
l - crn - car - 2 * cy - 1
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user