-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mflinn-broad
committed
Dec 12, 2021
1 parent
94d8d1d
commit 8a68bb1
Showing
6 changed files
with
210 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ edition = "2021" | |
|
||
[dependencies] | ||
itertools = "0.10.3" | ||
petgraph = "0.6.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use crate::util; | ||
use petgraph::graphmap::UnGraphMap; | ||
use std::collections::HashSet; | ||
|
||
pub fn run() { | ||
let raw_input = util::read_input("inputs/day12.txt").unwrap(); | ||
let input = process(&raw_input); | ||
|
||
println!("Part 1: {}", part_1(&input)); | ||
println!("Part 2: {}", part_2(&input)); | ||
} | ||
|
||
fn process(input: &str) -> Vec<(&str, &str)> { | ||
input | ||
.lines() | ||
.map(|line| line.trim().split_once("-").unwrap()) | ||
.collect() | ||
} | ||
|
||
fn part_1(input: &Vec<(&str, &str)>) -> usize { | ||
let cave_map = UnGraphMap::<&str, ()>::from_edges(input); | ||
let mut visit_tracker: HashSet<&str> = HashSet::new(); | ||
visit_tracker.insert("start"); | ||
|
||
count_simple_paths(&cave_map, "start", &mut visit_tracker) | ||
} | ||
|
||
fn part_2(input: &Vec<(&str, &str)>) -> usize { | ||
let cave_map = UnGraphMap::<&str, ()>::from_edges(input); | ||
let mut visit_tracker: HashSet<&str> = HashSet::new(); | ||
visit_tracker.insert("start"); | ||
|
||
count_paths_v2(&cave_map, "start", &mut visit_tracker, &mut None) | ||
} | ||
|
||
fn is_small_cave(cave: &str) -> bool { | ||
cave.chars().all(|c| c.is_lowercase()) | ||
} | ||
|
||
fn is_start(cave: &str) -> bool { | ||
cave == "start" | ||
} | ||
|
||
fn is_end(cave: &str) -> bool { | ||
cave == "end" | ||
} | ||
|
||
fn count_simple_paths<'a>( | ||
cave_system: &UnGraphMap<&'a str, ()>, | ||
curr_node: &'a str, | ||
visited_small_caves: &mut HashSet<&'a str>, | ||
) -> usize { | ||
if curr_node == "end" { | ||
visited_small_caves.remove(curr_node); | ||
return 1; | ||
} | ||
|
||
let mut count = 0; | ||
|
||
for connected_cave in cave_system.neighbors(curr_node) { | ||
if is_small_cave(connected_cave) { | ||
if visited_small_caves.contains(connected_cave) { | ||
continue; | ||
} else { | ||
visited_small_caves.insert(connected_cave); | ||
} | ||
} | ||
count += count_simple_paths(&cave_system, connected_cave, visited_small_caves); | ||
visited_small_caves.remove(connected_cave); | ||
} | ||
count | ||
} | ||
|
||
fn count_paths_v2<'a>( | ||
cave_system: &UnGraphMap<&'a str, ()>, | ||
curr_node: &'a str, | ||
visited_small_caves: &mut HashSet<&'a str>, | ||
twive_visited_cave: &mut Option<&'a str>, | ||
) -> usize { | ||
if is_end(curr_node) { | ||
visited_small_caves.remove(curr_node); | ||
return 1; | ||
} | ||
|
||
let mut count = 0; | ||
for connected_cave in cave_system.neighbors(curr_node) { | ||
if is_small_cave(connected_cave) { | ||
if !visited_small_caves.contains(connected_cave) { | ||
visited_small_caves.insert(connected_cave); | ||
} else if twive_visited_cave.is_none() | ||
&& !is_start(connected_cave) | ||
&& !is_end(connected_cave) | ||
{ | ||
*twive_visited_cave = Some(connected_cave); | ||
} else { | ||
continue; | ||
} | ||
} | ||
|
||
if let Some(_) = twive_visited_cave { | ||
count += count_simple_paths(cave_system, connected_cave, visited_small_caves); | ||
} else { | ||
count += count_paths_v2( | ||
cave_system, | ||
connected_cave, | ||
visited_small_caves, | ||
twive_visited_cave, | ||
); | ||
} | ||
|
||
if *twive_visited_cave == Some(connected_cave) { | ||
*twive_visited_cave = None; | ||
} else { | ||
visited_small_caves.remove(connected_cave); | ||
} | ||
} | ||
|
||
count | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
extern crate test; | ||
use test::Bencher; | ||
|
||
#[bench] | ||
fn bench_part_1(b: &mut Bencher) { | ||
let raw_input = util::read_input("inputs/day12.txt").unwrap(); | ||
b.iter(|| { | ||
let input = process(&raw_input); | ||
part_1(&input); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn bench_part_2(b: &mut Bencher) { | ||
let raw_input = util::read_input("inputs/day12.txt").unwrap(); | ||
b.iter(|| { | ||
let input = process(&raw_input); | ||
part_2(&input); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod day1; | ||
pub mod day10; | ||
pub mod day11; | ||
pub mod day12; | ||
pub mod day2; | ||
pub mod day3; | ||
pub mod day4; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters