-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.rs
87 lines (76 loc) · 1.4 KB
/
day23.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::solutions::Solution;
use crate::utils::graphs::graph::Graph;
pub struct Day23;
impl Solution for Day23 {
fn part_one(&self, input: &str) -> String {
self.parse(input)
.cycles_3_elements()
.iter()
.filter(|set| set.iter().any(|c| c.starts_with("t")))
.count()
.to_string()
}
fn part_two(&self, input: &str) -> String {
self.parse(input)
.maximal_cliques()
.iter()
.max_by_key(|cycle| cycle.len())
.unwrap()
.join(",")
}
}
impl Day23 {
fn parse<'a>(&self, input: &'a str) -> Graph<&'a str> {
let mut graph: Graph<&str> = Graph::undirected();
input.lines().for_each(|line| {
let (a, b) = line.split_once('-').unwrap();
graph.add_edge(a, b);
});
graph
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day23::Day23;
use crate::solutions::Solution;
const EXAMPLE: &str = r#"kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn"#;
#[test]
fn part_one_example() {
assert_eq!("7", Day23.part_one(EXAMPLE));
}
#[test]
fn part_two_example() {
assert_eq!("co,de,ka,ta", Day23.part_two(EXAMPLE));
}
}