-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.rs
174 lines (165 loc) · 4.87 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
164
165
166
167
168
169
170
171
172
173
174
use aoc_runner_derive::aoc;
use itertools::Itertools;
struct Graph {
nodes: rustc_hash::FxHashMap<usize, smallvec::SmallVec<[usize; 16]>>,
connection_map: [[bool; Self::SIZE]; Self::SIZE],
}
impl Graph {
const OFFSET: usize = (b'z' - b'a' + 1) as usize;
const SIZE: usize = Self::OFFSET * Self::OFFSET;
#[inline]
fn parse(input: &str) -> Self {
let mut connection_map = [[false; Self::SIZE]; Self::SIZE];
let nodes: rustc_hash::FxHashMap<_, smallvec::SmallVec<_>> = input
.as_bytes()
.chunks(6)
.fold(rustc_hash::FxHashMap::default(), |mut acc, connection| {
let nodes = unsafe {
[
Self::OFFSET
.unchecked_mul((connection[0].unchecked_sub(b'a')) as usize)
.unchecked_add((connection[1].unchecked_sub(b'a')) as usize),
Self::OFFSET
.unchecked_mul((connection[3].unchecked_sub(b'a')) as usize)
.unchecked_add((connection[4].unchecked_sub(b'a')) as usize),
]
};
connection_map[nodes[0]][nodes[1]] = true;
connection_map[nodes[1]][nodes[0]] = true;
acc.entry(nodes[0]).or_default().push(nodes[1]);
acc.entry(nodes[1]).or_default().push(nodes[0]);
acc
});
Self {
nodes,
connection_map,
}
}
}
#[aoc(day23, part1)]
#[must_use]
pub fn part1(input: &str) -> usize {
const T_RANGE: std::ops::Range<usize> =
(Graph::OFFSET * (b't' - b'a') as usize)..(Graph::OFFSET * (b't' - b'a' + 1) as usize);
let graph = Graph::parse(input);
let mut visited = [false; Graph::SIZE];
T_RANGE
.filter_map(|node_a| {
if let Some(connected_nodes) = graph.nodes.get(&node_a) {
visited[node_a] = true;
Some(
connected_nodes
.iter()
.enumerate()
.map(|(i, &node_b)| {
connected_nodes
.iter()
.skip(i)
.filter(|&&node_c| {
graph.connection_map[node_b][node_c]
&& !visited[node_b]
&& !visited[node_c]
})
.count()
})
.sum::<usize>(),
)
} else {
None
}
})
.sum()
}
#[aoc(day23, part2)]
#[must_use]
pub fn part2(input: &str) -> String {
const EXPECTED_LEN: usize = 14;
let graph = Graph::parse(input);
graph
.nodes
.into_iter()
.fold(
(
smallvec::SmallVec::<[_; EXPECTED_LEN]>::new(),
[false; Graph::SIZE],
),
|(max, mut visited), (node_a, connected_nodes)| {
if visited[node_a] {
return (max, visited);
}
let current = std::iter::once(node_a).chain(connected_nodes).fold(
smallvec::SmallVec::<[_; EXPECTED_LEN]>::new(),
|mut current, node_b| {
if current.iter().all(|&c| graph.connection_map[node_b][c]) {
visited[node_b] = true;
current.push(node_b);
}
current
},
);
if current.len() > max.len() {
(current, visited)
} else {
(max, visited)
}
},
)
.0
.into_iter()
.sorted_unstable()
.map(|node| {
format!(
"{}{}",
char::from(((node / Graph::OFFSET) as u8) + b'a'),
char::from(((node % Graph::OFFSET) as u8) + b'a')
)
})
.join(",")
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
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]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 7);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), "co,de,ka,ta");
}
}