Skip to content

Commit ce2c9dc

Browse files
committed
feat(23/2024): solve first part
1 parent 20d2f6e commit ce2c9dc

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| [Day 20: Race Condition](src/solutions/year2024/day20.rs) | ⭐⭐ | 7.355 | 280.627 |
3434
| [Day 21: Keypad Conundrum](src/solutions/year2024/day21.rs) || 0.454 | - |
3535
| [Day 22: Monkey Market](src/solutions/year2024/day22.rs) | ⭐⭐ | 16.325 | 270.527 |
36+
| [Day 23: LAN Party](src/solutions/year2024/day23.rs) || 6.823 | - |
3637

3738
# 2023
3839

src/solutions/year2024/day23.rs

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
use crate::solutions::Solution;
2+
use std::collections::{HashMap, HashSet};
23

34
pub struct Day23;
45

56
impl Solution for Day23 {
6-
fn part_one(&self, _input: &str) -> String {
7-
String::from("0")
7+
fn part_one(&self, input: &str) -> String {
8+
let mut neighbours: HashMap<&str, Vec<&str>> = HashMap::new();
9+
10+
let connections: Vec<(&str, &str)> = input
11+
.lines()
12+
.map(|line| {
13+
let (a, b) = line.split_once('-').unwrap();
14+
15+
neighbours.entry(a).or_default().push(b);
16+
neighbours.entry(b).or_default().push(a);
17+
18+
(a, b)
19+
})
20+
.collect();
21+
22+
let mut sets: HashSet<[&str; 3]> = HashSet::new();
23+
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()
41+
.filter(|set| set.iter().any(|c| c.starts_with("t")))
42+
.count()
43+
.to_string()
844
}
945

1046
fn part_two(&self, _input: &str) -> String {
@@ -17,10 +53,41 @@ mod tests {
1753
use crate::solutions::year2024::day23::Day23;
1854
use crate::solutions::Solution;
1955

20-
const EXAMPLE: &str = r#""#;
56+
const EXAMPLE: &str = r#"kh-tc
57+
qp-kh
58+
de-cg
59+
ka-co
60+
yn-aq
61+
qp-ub
62+
cg-tb
63+
vc-aq
64+
tb-ka
65+
wh-tc
66+
yn-cg
67+
kh-ub
68+
ta-co
69+
de-co
70+
tc-td
71+
tb-wq
72+
wh-td
73+
ta-ka
74+
td-qp
75+
aq-cg
76+
wq-ub
77+
ub-vc
78+
de-ta
79+
wq-aq
80+
wq-vc
81+
wh-yn
82+
ka-de
83+
kh-ta
84+
co-tc
85+
wh-qp
86+
tb-vc
87+
td-yn"#;
2188

2289
#[test]
2390
fn part_one_example() {
24-
assert_eq!("0", Day23.part_one(EXAMPLE));
91+
assert_eq!("7", Day23.part_one(EXAMPLE));
2592
}
2693
}

0 commit comments

Comments
 (0)