-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.rs
163 lines (143 loc) · 4.11 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::collections::{HashMap, HashSet};
use crate::solution::{AocError, Solution};
fn parse(input: &str) -> Result<HashMap<&str, HashSet<&str>>, AocError> {
let mut network: HashMap<&str, HashSet<&str>> = HashMap::new();
for row in input.lines() {
let (first, second) = row
.split_once("-")
.ok_or_else(|| AocError::parse(row, "Invalid network node"))?;
network.entry(first).or_default().insert(second);
network.entry(second).or_default().insert(first);
}
Ok(network)
}
pub struct Day23;
impl Solution for Day23 {
type Part1 = u32;
type Part2 = String;
fn default_input(&self) -> &'static str {
include_str!("../../../inputs/2024/day23.txt")
}
fn part_1(&self, input: &str) -> Result<u32, AocError> {
let network = parse(input)?;
let mut networks_of_three: HashSet<Vec<&str>> = HashSet::new();
for (first, edges) in network.iter() {
if first.starts_with("t") {
for second in edges {
for third in network[second].intersection(edges) {
let mut group = vec![*first, *second, *third];
group.sort();
networks_of_three.insert(group);
}
}
}
}
Ok(networks_of_three.len() as u32)
}
fn part_2(&self, input: &str) -> Result<String, AocError> {
let network = parse(input)?;
let mut largest = network
.iter()
.map(|(current, edges)| {
let mut interconnected = vec![*current];
for edge in edges {
if interconnected
.iter()
.all(|node| network[edge].contains(node))
{
interconnected.push(*edge);
}
}
interconnected
})
.max_by(|a, b| a.len().cmp(&b.len()))
.ok_or_else(|| AocError::logic("No groups found"))?;
largest.sort();
let password = largest.join(",");
Ok(password)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_solves_part1_example() {
assert_eq!(
Day23.part_1(
"kh-tc\n\
qp-kh\n\
de-cg\n\
ka-co\n\
yn-aq\n\
qp-ub\n\
cg-tb\n\
vc-aq\n\
tb-ka\n\
wh-tc\n\
yn-cg\n\
kh-ub\n\
ta-co\n\
de-co\n\
tc-td\n\
tb-wq\n\
wh-td\n\
ta-ka\n\
td-qp\n\
aq-cg\n\
wq-ub\n\
ub-vc\n\
de-ta\n\
wq-aq\n\
wq-vc\n\
wh-yn\n\
ka-de\n\
kh-ta\n\
co-tc\n\
wh-qp\n\
tb-vc\n\
td-yn"
),
Ok(7)
);
}
#[test]
fn it_solves_part2_example() {
assert_eq!(
Day23.part_2(
"kh-tc\n\
qp-kh\n\
de-cg\n\
ka-co\n\
yn-aq\n\
qp-ub\n\
cg-tb\n\
vc-aq\n\
tb-ka\n\
wh-tc\n\
yn-cg\n\
kh-ub\n\
ta-co\n\
de-co\n\
tc-td\n\
tb-wq\n\
wh-td\n\
ta-ka\n\
td-qp\n\
aq-cg\n\
wq-ub\n\
ub-vc\n\
de-ta\n\
wq-aq\n\
wq-vc\n\
wh-yn\n\
ka-de\n\
kh-ta\n\
co-tc\n\
wh-qp\n\
tb-vc\n\
td-yn"
),
Ok(String::from("co,de,ka,ta"))
);
}
}