mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Compare commits
2 Commits
84c160cf54
...
0897e2e907
| Author | SHA1 | Date | |
|---|---|---|---|
| 0897e2e907 | |||
| 4d1fdd9cc0 |
19
2021/inputs/12.txt
Normal file
19
2021/inputs/12.txt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
lg-GW
|
||||||
|
pt-start
|
||||||
|
pt-uq
|
||||||
|
nx-lg
|
||||||
|
ve-GW
|
||||||
|
start-nx
|
||||||
|
GW-start
|
||||||
|
GW-nx
|
||||||
|
pt-SM
|
||||||
|
sx-GW
|
||||||
|
lg-end
|
||||||
|
nx-SM
|
||||||
|
lg-SM
|
||||||
|
pt-nx
|
||||||
|
end-ve
|
||||||
|
ve-SM
|
||||||
|
TG-uq
|
||||||
|
end-SM
|
||||||
|
SM-uq
|
||||||
@@ -1,9 +1,102 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
pub fn part1(_input: &mut dyn Read) -> String {
|
use crate::common::LineIter;
|
||||||
todo!()
|
|
||||||
|
type EdgeMap = HashMap<String, Vec<String>>;
|
||||||
|
|
||||||
|
fn read_edges(input: &mut dyn Read) -> EdgeMap {
|
||||||
|
let mut reader = LineIter::new(input);
|
||||||
|
let mut edges = EdgeMap::new();
|
||||||
|
|
||||||
|
while let Some(line) = reader.next() {
|
||||||
|
let (from, to) = line.split_once('-').unwrap();
|
||||||
|
|
||||||
|
edges
|
||||||
|
.entry(from.to_owned())
|
||||||
|
.or_default()
|
||||||
|
.push(to.to_owned());
|
||||||
|
|
||||||
|
edges
|
||||||
|
.entry(to.to_owned())
|
||||||
|
.or_default()
|
||||||
|
.push(from.to_owned());
|
||||||
|
}
|
||||||
|
|
||||||
|
edges
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &mut dyn Read) -> String {
|
fn is_small(cave: &str) -> bool {
|
||||||
todo!()
|
cave.chars().all(|c| c.is_ascii_lowercase())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dfs_routes<'a>(
|
||||||
|
edges: &'a EdgeMap,
|
||||||
|
route: &'_ mut Vec<&'a str>,
|
||||||
|
pos: &'a str,
|
||||||
|
mut small_twice: bool,
|
||||||
|
) -> usize {
|
||||||
|
match pos {
|
||||||
|
"end" => return 1,
|
||||||
|
"start" if !route.is_empty() => return 0,
|
||||||
|
pos if is_small(pos) && route.contains(&pos) => {
|
||||||
|
if small_twice {
|
||||||
|
small_twice = false;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
route.push(pos);
|
||||||
|
|
||||||
|
let routes = edges[pos]
|
||||||
|
.iter()
|
||||||
|
.map(|new_pos| dfs_routes(edges, route, new_pos, small_twice))
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
route.pop();
|
||||||
|
|
||||||
|
routes
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parts_common(input: &mut dyn Read, small_twice: bool) -> String {
|
||||||
|
let edges = read_edges(input);
|
||||||
|
let mut route = Vec::new();
|
||||||
|
|
||||||
|
dfs_routes(&edges, &mut route, "start", small_twice).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &mut dyn Read) -> String {
|
||||||
|
parts_common(input, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2(input: &mut dyn Read) -> String {
|
||||||
|
parts_common(input, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::test_implementation;
|
||||||
|
|
||||||
|
const SAMPLE1: &[u8] = include_bytes!("samples/12.1.txt");
|
||||||
|
const SAMPLE2: &[u8] = include_bytes!("samples/12.2.txt");
|
||||||
|
const SAMPLE3: &[u8] = include_bytes!("samples/12.3.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part1() {
|
||||||
|
test_implementation(part1, SAMPLE1, 10);
|
||||||
|
test_implementation(part1, SAMPLE2, 19);
|
||||||
|
test_implementation(part1, SAMPLE3, 226);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part2() {
|
||||||
|
test_implementation(part2, SAMPLE1, 36);
|
||||||
|
test_implementation(part2, SAMPLE2, 103);
|
||||||
|
test_implementation(part2, SAMPLE3, 3509);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
2021/src/samples/12.1.txt
Normal file
7
2021/src/samples/12.1.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
start-A
|
||||||
|
start-b
|
||||||
|
A-c
|
||||||
|
A-b
|
||||||
|
b-d
|
||||||
|
A-end
|
||||||
|
b-end
|
||||||
10
2021/src/samples/12.2.txt
Normal file
10
2021/src/samples/12.2.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
dc-end
|
||||||
|
HN-start
|
||||||
|
start-kj
|
||||||
|
dc-start
|
||||||
|
dc-HN
|
||||||
|
LN-dc
|
||||||
|
HN-end
|
||||||
|
kj-sa
|
||||||
|
kj-HN
|
||||||
|
kj-dc
|
||||||
18
2021/src/samples/12.3.txt
Normal file
18
2021/src/samples/12.3.txt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
fs-end
|
||||||
|
he-DX
|
||||||
|
fs-he
|
||||||
|
start-DX
|
||||||
|
pj-DX
|
||||||
|
end-zg
|
||||||
|
zg-sl
|
||||||
|
zg-pj
|
||||||
|
pj-he
|
||||||
|
RW-he
|
||||||
|
fs-DX
|
||||||
|
pj-RW
|
||||||
|
zg-RW
|
||||||
|
start-pj
|
||||||
|
he-WI
|
||||||
|
zg-he
|
||||||
|
pj-fs
|
||||||
|
start-RW
|
||||||
Reference in New Issue
Block a user