Add day 12 part 2

This commit is contained in:
kageru 2021-12-12 13:05:38 +01:00
parent 06c8b775fa
commit 1e03a430b9
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -8,8 +8,6 @@ use itertools::Itertools;
const DAY: usize = 12; const DAY: usize = 12;
type Parsed<'a> = HashMap<Node<'a>, Vec<Node<'a>>>; type Parsed<'a> = HashMap<Node<'a>, Vec<Node<'a>>>;
const EMPTY: Vec<Node<'_>> = Vec::new();
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
enum Node<'a> { enum Node<'a> {
Start, Start,
@ -39,29 +37,30 @@ fn parse_input(raw: &str) -> Parsed {
} }
fn part1(parsed: &Parsed) -> usize { fn part1(parsed: &Parsed) -> usize {
possible_paths(parsed, &Node::Start, HashSet::new()) possible_paths(parsed, &Node::Start, HashSet::new(), false)
} }
fn possible_paths<'a>(map: &'a Parsed, position: &'a Node<'a>, mut visited: HashSet<&'a Node<'a>>) -> usize { fn part2(parsed: &Parsed) -> usize {
if position == &Node::End { possible_paths(parsed, &Node::Start, HashSet::new(), true)
return 1; }
}
fn possible_paths<'a>(map: &'a Parsed, position: &'a Node<'a>, mut visited: HashSet<&'a Node<'a>>, small_cave_allowed: bool) -> usize {
if matches!(position, &Node::Small(_)) { if matches!(position, &Node::Small(_)) {
visited.insert(position); visited.insert(position);
} }
map.get(position) map.get(position)
.unwrap() .unwrap()
.iter() .iter()
.filter(|&p| p != &Node::Start) .map(|p| match p {
.filter(|p| !visited.contains(p)) Node::Big(_) => possible_paths(map, p, visited.clone(), small_cave_allowed),
.map(|p| possible_paths(map, p, visited.clone())) Node::Small(_) if !visited.contains(&p) => possible_paths(map, p, visited.clone(), small_cave_allowed),
Node::Small(_) if small_cave_allowed => possible_paths(map, p, visited.clone(), false),
Node::Small(_) | Node::Start => 0,
Node::End => 1,
})
.sum() .sum()
} }
fn part2(parsed: &Parsed) -> usize {
unimplemented!()
}
fn main() { fn main() {
let raw = read_file(DAY); let raw = read_file(DAY);
let input = parse_input(&raw); let input = parse_input(&raw);
@ -119,6 +118,6 @@ start-RW";
test!(with _2: part2() == 103); test!(with _2: part2() == 103);
test!(with _3: part2() == 3509); test!(with _3: part2() == 3509);
bench!(part1() == 4411); bench!(part1() == 4411);
bench!(part2() == 0); bench!(part2() == 136767);
bench_input!(HashMap::len => 13); bench_input!(HashMap::len => 13);
} }