Skip to content

Commit 10a2174

Browse files
committed
Day 23
1 parent 9ca42ee commit 10a2174

File tree

5 files changed

+166
-2
lines changed

5 files changed

+166
-2
lines changed

Cargo.lock

Lines changed: 40 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mygrid = { version = "0.0.1", path = "mygrid" }
3333
nalgebra = "0.33.2"
3434
num = "0.4.3"
3535
object-pool = "0.6.0"
36+
petgraph = "0.6.5"
3637
phf = { version = "0.11.2", features = ["macros"] }
3738
pico-args = "0.5.0"
3839
rayon = "1.10.0"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
3333
| [Day 20](./src/bin/20.rs) | `308.3µs` | `1.8ms` |
3434
| [Day 21](./src/bin/21.rs) | `10.9µs` | `112.6µs` |
3535
| [Day 22](./src/bin/22.rs) | `610.4µs` | `9.0s` |
36+
| [Day 23](./src/bin/23.rs) | `157.2µs` | `620.5µs` |
3637

37-
**Total: 9017.30ms**
38+
**Total: 9018.08ms**
3839
<!--- benchmarking table --->
3940

4041
---

data/examples/23.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
kh-tc
2+
qp-kh
3+
de-cg
4+
ka-co
5+
yn-aq
6+
qp-ub
7+
cg-tb
8+
vc-aq
9+
tb-ka
10+
wh-tc
11+
yn-cg
12+
kh-ub
13+
ta-co
14+
de-co
15+
tc-td
16+
tb-wq
17+
wh-td
18+
ta-ka
19+
td-qp
20+
aq-cg
21+
wq-ub
22+
ub-vc
23+
de-ta
24+
wq-aq
25+
wq-vc
26+
wh-yn
27+
ka-de
28+
kh-ta
29+
co-tc
30+
wh-qp
31+
tb-vc
32+
td-yn

src/bin/23.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use itertools::Itertools;
2+
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
3+
use rustc_hash::{FxHashMap, FxHashSet};
4+
5+
advent_of_code::solution!(23);
6+
7+
type Node = (u8, u8);
8+
type Connections = FxHashMap<Node, FxHashSet<Node>>;
9+
10+
fn parse_input(input: &str) -> Connections {
11+
let mut connections = FxHashMap::default();
12+
13+
// nice technique from https://github.com/ndunnett/aoc/blob/main/rust/2024/src/bin/day23.rs
14+
for (a0, a1, _, b0, b1, _) in input.bytes().tuples() {
15+
connections
16+
.entry((a0, a1))
17+
.or_insert(FxHashSet::default())
18+
.insert((b0, b1));
19+
20+
connections
21+
.entry((b0, b1))
22+
.or_insert(FxHashSet::default())
23+
.insert((a0, a1));
24+
}
25+
26+
connections
27+
}
28+
29+
pub fn part_one(input: &str) -> Option<u32> {
30+
let graph = parse_input(input);
31+
32+
type Island = (Node, Node, Node);
33+
34+
let all_islands = graph
35+
.iter()
36+
.filter(|(l1, _)| l1.0 == b't')
37+
.flat_map(|(a, adj_a)| {
38+
adj_a.iter().flat_map(|b| {
39+
adj_a.intersection(&graph[b]).map(|c| {
40+
let mut island = [*a, *b, *c];
41+
island.sort();
42+
island.into_iter().next_tuple::<Island>().unwrap()
43+
})
44+
})
45+
});
46+
47+
let islands = FxHashSet::from_iter(all_islands);
48+
Some(islands.len() as u32)
49+
}
50+
51+
pub fn part_two(input: &str) -> Option<String> {
52+
let graph = parse_input(input);
53+
let mut cliques = graph
54+
.keys()
55+
.map(|&a| FxHashSet::from_iter([a]))
56+
.collect::<Vec<_>>();
57+
58+
cliques.par_iter_mut().for_each(|clique| {
59+
graph.keys().for_each(|a| {
60+
if clique.iter().all(|b| graph[a].contains(b)) {
61+
clique.insert(*a);
62+
}
63+
});
64+
});
65+
66+
let max_clique = cliques.iter().max_by(|a, b| a.len().cmp(&b.len())).unwrap();
67+
let password = max_clique
68+
.iter()
69+
.sorted()
70+
.map(|node| format!("{}{}", node.0 as char, node.1 as char))
71+
.join(",");
72+
73+
Some(password)
74+
}
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use super::*;
79+
80+
#[test]
81+
fn test_part_one() {
82+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
83+
assert_eq!(result, Some(7));
84+
}
85+
86+
#[test]
87+
fn test_part_two() {
88+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
89+
assert_eq!(result, Some("co,de,ka,ta".to_string()));
90+
}
91+
}

0 commit comments

Comments
 (0)