Compare commits
No commits in common. "f41e7a76eacfc52de9a7ce9a7c16de03e0779b9b" and "37293ab29e5abf1451cec7a85652bff5eee324a7" have entirely different histories.
f41e7a76ea
...
37293ab29e
4 changed files with 0 additions and 93 deletions
|
@ -1,2 +0,0 @@
|
|||
4411
|
||||
136767
|
|
@ -1,24 +0,0 @@
|
|||
yb-start
|
||||
de-vd
|
||||
rj-yb
|
||||
rj-VP
|
||||
OC-de
|
||||
MU-de
|
||||
end-DN
|
||||
vd-end
|
||||
WK-vd
|
||||
rj-de
|
||||
DN-vd
|
||||
start-VP
|
||||
DN-yb
|
||||
vd-MU
|
||||
DN-rj
|
||||
de-VP
|
||||
yb-OC
|
||||
start-rj
|
||||
oa-MU
|
||||
yb-de
|
||||
oa-VP
|
||||
jv-MU
|
||||
yb-MU
|
||||
end-OC
|
|
@ -1,8 +0,0 @@
|
|||
[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]
|
|
@ -1,59 +0,0 @@
|
|||
#![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