Skip to content

Commit 067b67d

Browse files
committed
Optimize 2024 day 23 part 1
1 parent bce98bd commit 067b67d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

crates/year2024/src/day23.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,28 @@ impl Day23 {
5151
#[must_use]
5252
pub fn part1(&self) -> u32 {
5353
let mut count = 0;
54-
for n1 in 0..self.nodes.len() {
54+
55+
// 19 = b't' - b'a'
56+
for n1 in 19 * 26..20 * 26 {
5557
for n2 in self.neighbors(n1) {
56-
if n2 > n1 {
57-
break;
58+
// Ensure the combination is only counted once if the second node also starts with t
59+
if n2 / 26 == 19 && n2 >= n1 {
60+
continue;
5861
}
62+
5963
for n3 in Self::iter(Self::intersect(self.nodes[n1], self.nodes[n2])) {
60-
if n3 > n2 {
64+
if n3 >= n2 {
6165
break;
6266
}
63-
64-
// 19 = b't' - b'a'
65-
if n1 / 26 == 19 || n2 / 26 == 19 || n3 / 26 == 19 {
66-
count += 1;
67+
if n3 / 26 == 19 && n3 >= n1 {
68+
continue;
6769
}
70+
71+
count += 1;
6872
}
6973
}
7074
}
75+
7176
count
7277
}
7378

0 commit comments

Comments
 (0)