2021 day12/rust: add simple solution
This commit is contained in:
parent
acb52282b6
commit
f41e7a76ea
2 changed files with 67 additions and 0 deletions
8
2021/day12/day12_rs/Cargo.toml
Normal file
8
2021/day12/day12_rs/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day12_rs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
59
2021/day12/day12_rs/src/main.rs
Normal file
59
2021/day12/day12_rs/src/main.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
#![warn(clippy::pedantic)]
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
io::{stdin, BufRead},
|
||||
};
|
||||
|
||||
type Point<'a> = &'a str;
|
||||
|
||||
fn walk<'a>(
|
||||
connections: &HashMap<Point<'a>, HashSet<Point<'a>>>,
|
||||
visited: &[Point<'a>],
|
||||
extra_cave: Option<Point<'a>>,
|
||||
from: Point<'a>,
|
||||
) -> usize {
|
||||
let mut res = 0;
|
||||
|
||||
let mut visited = Vec::from(visited);
|
||||
visited.push(from);
|
||||
|
||||
for target in &connections[from] {
|
||||
if *target == "end" {
|
||||
res += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let is_uppercase = target.chars().all(|c| c.is_ascii_uppercase());
|
||||
let was_visited = visited.contains(target);
|
||||
|
||||
if is_uppercase || !was_visited {
|
||||
res += walk(connections, &visited, extra_cave, target);
|
||||
} else if extra_cave.is_none() {
|
||||
res += walk(connections, &visited, Some(target), target);
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let lines: Vec<_> = stdin().lock().lines().collect::<Result<_, _>>().unwrap();
|
||||
|
||||
let connections: HashMap<Point, HashSet<Point>> =
|
||||
lines.iter().fold(HashMap::new(), |mut map, line| {
|
||||
let mut parts = line.split('-');
|
||||
let p1 = parts.next().unwrap();
|
||||
let p2 = parts.next().unwrap();
|
||||
|
||||
if p2 != "start" {
|
||||
map.entry(p1).or_default().insert(p2);
|
||||
}
|
||||
if p2 != "end" {
|
||||
map.entry(p2).or_default().insert(p1);
|
||||
}
|
||||
map
|
||||
});
|
||||
|
||||
println!("{}", walk(&connections, &[], Some(""), "start"));
|
||||
println!("{}", walk(&connections, &[], None, "start"));
|
||||
}
|
Loading…
Reference in a new issue