Skip to content

Commit e9c68a0

Browse files
committed
AoC 2024 Day 23 - rust
1 parent e827073 commit e9c68a0

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
1111
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | [](src/main/python/AoC2024_04.py) | [](src/main/python/AoC2024_05.py) | [](src/main/python/AoC2024_06.py) | [](src/main/python/AoC2024_07.py) | [](src/main/python/AoC2024_08.py) | [](src/main/python/AoC2024_09.py) | [](src/main/python/AoC2024_10.py) | [](src/main/python/AoC2024_11.py) | [](src/main/python/AoC2024_12.py) | [](src/main/python/AoC2024_13.py) | [](src/main/python/AoC2024_14.py) | [](src/main/python/AoC2024_15.py) | [](src/main/python/AoC2024_16.py) | [](src/main/python/AoC2024_17.py) | [](src/main/python/AoC2024_18.py) | [](src/main/python/AoC2024_19.py) | [](src/main/python/AoC2024_20.py) | [](src/main/python/AoC2024_21.py) | [](src/main/python/AoC2024_22.py) | [](src/main/python/AoC2024_23.py) | | |
1212
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | [](src/main/java/AoC2024_06.java) | [](src/main/java/AoC2024_07.java) | [](src/main/java/AoC2024_08.java) | | [](src/main/java/AoC2024_10.java) | [](src/main/java/AoC2024_11.java) | [](src/main/java/AoC2024_12.java) | | [](src/main/java/AoC2024_14.java) | [](src/main/java/AoC2024_15.java) | | | | | | | | | | |
13-
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | [](src/main/rust/AoC2024_10/src/main.rs) | [](src/main/rust/AoC2024_11/src/main.rs) | [](src/main/rust/AoC2024_12/src/main.rs) | [](src/main/rust/AoC2024_13/src/main.rs) | [](src/main/rust/AoC2024_14/src/main.rs) | [](src/main/rust/AoC2024_15/src/main.rs) | [](src/main/rust/AoC2024_16/src/main.rs) | [](src/main/rust/AoC2024_17/src/main.rs) | [](src/main/rust/AoC2024_18/src/main.rs) | [](src/main/rust/AoC2024_19/src/main.rs) | [](src/main/rust/AoC2024_20/src/main.rs) | | [](src/main/rust/AoC2024_22/src/main.rs) | | | |
13+
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | [](src/main/rust/AoC2024_10/src/main.rs) | [](src/main/rust/AoC2024_11/src/main.rs) | [](src/main/rust/AoC2024_12/src/main.rs) | [](src/main/rust/AoC2024_13/src/main.rs) | [](src/main/rust/AoC2024_14/src/main.rs) | [](src/main/rust/AoC2024_15/src/main.rs) | [](src/main/rust/AoC2024_16/src/main.rs) | [](src/main/rust/AoC2024_17/src/main.rs) | [](src/main/rust/AoC2024_18/src/main.rs) | [](src/main/rust/AoC2024_19/src/main.rs) | [](src/main/rust/AoC2024_20/src/main.rs) | | [](src/main/rust/AoC2024_22/src/main.rs) | [](src/main/rust/AoC2024_23/src/main.rs) | | |
1414
<!-- @END:ImplementationsTable:2024@ -->
1515

1616
## 2023

src/main/rust/AoC2024_23/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "AoC2024_23"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../aoc" }

src/main/rust/AoC2024_23/src/main.rs

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#![allow(non_snake_case)]
2+
3+
use aoc::Puzzle;
4+
use std::collections::{HashMap, HashSet};
5+
6+
struct AoC2024_23;
7+
8+
impl AoC2024_23 {}
9+
10+
impl aoc::Puzzle for AoC2024_23 {
11+
type Input = HashMap<String, HashSet<String>>;
12+
type Output1 = usize;
13+
type Output2 = String;
14+
15+
aoc::puzzle_year_day!(2024, 23);
16+
17+
fn parse_input(&self, lines: Vec<String>) -> Self::Input {
18+
let mut edges: HashMap<String, HashSet<String>> = HashMap::new();
19+
lines.iter().for_each(|line| {
20+
let (node_1, node_2) = line.split_once("-").unwrap();
21+
edges
22+
.entry(String::from(node_1))
23+
.or_default()
24+
.insert(String::from(node_2));
25+
edges
26+
.entry(String::from(node_2))
27+
.or_default()
28+
.insert(String::from(node_1));
29+
});
30+
edges
31+
}
32+
33+
fn part_1(&self, edges: &Self::Input) -> Self::Output1 {
34+
let mut triangles: HashSet<(&str, &str, &str)> = HashSet::new();
35+
for t in edges.keys().filter(|node| node.starts_with("t")) {
36+
for neighbour_1 in edges.get(t).unwrap() {
37+
for neighbour_2 in edges.get(neighbour_1).unwrap() {
38+
if edges.get(neighbour_2).unwrap().contains(t) {
39+
let mut triangle = [t, neighbour_1, neighbour_2];
40+
triangle.sort_unstable();
41+
triangles.insert((
42+
triangle[0],
43+
triangle[1],
44+
triangle[2],
45+
));
46+
}
47+
}
48+
}
49+
}
50+
triangles.len()
51+
}
52+
53+
fn part_2(&self, edges: &Self::Input) -> Self::Output2 {
54+
let mut clique = vec![];
55+
let mut largest = vec![];
56+
let mut seen: HashSet<&String> = HashSet::new();
57+
for (n1, neighbours) in edges {
58+
if seen.contains(n1) {
59+
continue;
60+
}
61+
clique.clear();
62+
clique.push(n1);
63+
for n2 in neighbours {
64+
let nn = edges.get(n2).unwrap();
65+
if clique.iter().all(|&c| nn.contains(c)) {
66+
seen.insert(n2);
67+
clique.push(n2);
68+
}
69+
}
70+
if clique.len() > largest.len() {
71+
largest.clone_from(&clique);
72+
}
73+
}
74+
largest.sort_unstable();
75+
largest
76+
.iter()
77+
.copied()
78+
.map(String::as_str)
79+
.collect::<Vec<_>>()
80+
.join(",")
81+
}
82+
83+
fn samples(&self) {
84+
aoc::puzzle_samples! {
85+
self, part_1, TEST, 7,
86+
self, part_2, TEST, "co,de,ka,ta"
87+
};
88+
}
89+
}
90+
91+
fn main() {
92+
AoC2024_23 {}.run(std::env::args());
93+
}
94+
95+
const TEST: &str = "\
96+
kh-tc
97+
qp-kh
98+
de-cg
99+
ka-co
100+
yn-aq
101+
qp-ub
102+
cg-tb
103+
vc-aq
104+
tb-ka
105+
wh-tc
106+
yn-cg
107+
kh-ub
108+
ta-co
109+
de-co
110+
tc-td
111+
tb-wq
112+
wh-td
113+
ta-ka
114+
td-qp
115+
aq-cg
116+
wq-ub
117+
ub-vc
118+
de-ta
119+
wq-aq
120+
wq-vc
121+
wh-yn
122+
ka-de
123+
kh-ta
124+
co-tc
125+
wh-qp
126+
tb-vc
127+
td-yn
128+
";
129+
130+
#[cfg(test)]
131+
mod tests {
132+
use super::*;
133+
134+
#[test]
135+
pub fn samples() {
136+
AoC2024_23 {}.samples();
137+
}
138+
}

src/main/rust/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)