Compare commits

...

2 commits

Author SHA1 Message Date
f41e7a76ea 2021 day12/rust: add simple solution 2021-12-16 11:24:20 +01:00
acb52282b6 2021 day12: add data 2021-12-16 11:23:28 +01:00
4 changed files with 93 additions and 0 deletions

2
2021/data/day12.expected Normal file
View file

@ -0,0 +1,2 @@
4411
136767

24
2021/data/day12.input Normal file
View file

@ -0,0 +1,24 @@
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

View 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]

View 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"));
}