Skip to content

Commit 1c1bad1

Browse files
committed
refactor(23/2024): simplify
1 parent ce2c9dc commit 1c1bad1

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/solutions/year2024/day23.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::solutions::Solution;
2-
use std::collections::{HashMap, HashSet};
2+
use itertools::Itertools;
3+
use std::collections::HashMap;
34

45
pub struct Day23;
56

@@ -19,25 +20,22 @@ impl Solution for Day23 {
1920
})
2021
.collect();
2122

22-
let mut sets: HashSet<[&str; 3]> = HashSet::new();
23+
connections
24+
.iter()
25+
.flat_map(|(a, b)| {
26+
let a_neighbours = neighbours.get(a).unwrap();
27+
let b_neighbours = neighbours.get(b).unwrap();
2328

24-
connections.iter().for_each(|(a, b)| {
25-
let a_neighbours = neighbours.get(a).unwrap();
26-
let b_neighbours = neighbours.get(b).unwrap();
27-
28-
let intersection: Vec<_> = a_neighbours
29-
.iter()
30-
.filter(|x| b_neighbours.contains(x))
31-
.collect();
32-
33-
intersection.iter().for_each(|c| {
34-
let mut set = [*a, *b, *c];
35-
set.sort();
36-
sets.insert(set);
37-
});
38-
});
39-
40-
sets.iter()
29+
a_neighbours
30+
.iter()
31+
.filter(|x| b_neighbours.contains(x))
32+
.map(|c| {
33+
let mut set = [*a, *b, *c];
34+
set.sort();
35+
set
36+
})
37+
})
38+
.unique()
4139
.filter(|set| set.iter().any(|c| c.starts_with("t")))
4240
.count()
4341
.to_string()

0 commit comments

Comments
 (0)