-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.rs
62 lines (55 loc) · 1.7 KB
/
day25.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use aoc_runner_derive::aoc;
fn parse(input: &str) -> petgraph::Graph<String, (), petgraph::Undirected> {
let mut graph = petgraph::Graph::default();
let mut nodes = std::collections::HashMap::new();
input.lines().for_each(|line| {
let (origin_node, target_nodes) = line.split_once(':').unwrap();
let origin_node = nodes
.entry(origin_node)
.or_insert_with(|| graph.add_node(origin_node.to_owned()))
.to_owned();
target_nodes.split_whitespace().for_each(|node| {
let node = nodes
.entry(node)
.or_insert_with(|| graph.add_node(node.to_owned()))
.to_owned();
graph.add_edge(node, origin_node, ());
});
});
graph
}
#[aoc(day25, part1)]
#[must_use]
pub fn part1(input: &str) -> usize {
let input = parse(input);
let min_cut_res: Result<_, ()> =
rustworkx_core::connectivity::stoer_wagner_min_cut(&input, |_| Ok(1));
let (min_cut, partition) = min_cut_res.unwrap().unwrap();
assert_eq!(min_cut, 3);
(input.node_count() - partition.len()) * partition.len()
}
// Note: Day 25 does not have a part 2
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
#[test]
pub fn part1_example() {
const SAMPLE: &str = indoc! {"
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr
"};
assert_eq!(part1(SAMPLE), 54);
}
}