Skip to content

Commit 09a10a1

Browse files
committed
Year 2024 Day 23
1 parent 654d256 commit 09a10a1

File tree

7 files changed

+144
-4
lines changed

7 files changed

+144
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
9393
| 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [Source](src/year2024/day19.rs) | 118 |
9494
| 20 | [Race Condition](https://adventofcode.com/2024/day/20) | [Source](src/year2024/day20.rs) | 1354 |
9595
| 22 | [Monkey Market](https://adventofcode.com/2024/day/22) | [Source](src/year2024/day22.rs) | 1350 |
96+
| 23 | [LAN Party](https://adventofcode.com/2024/day/23) | [Source](src/year2024/day23.rs) | 43 |
9697

9798
## 2023
9899

benches/benchmark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ benchmark!(year2023
8888

8989
benchmark!(year2024
9090
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
91-
day14, day15, day16, day17, day18, day19, day20, day22
91+
day14, day15, day16, day17, day18, day19, day20, day22, day23
9292
);

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ library!(year2023 "Restore global snow production."
6868

6969
library!(year2024 "Locate the Chief Historian in time for the big Christmas sleigh launch."
7070
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
71-
day14, day15, day16, day17, day18, day19, day20, day22
71+
day14, day15, day16, day17, day18, day19, day20, day22, day23
7272
);

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,5 @@ run!(year2023
138138

139139
run!(year2024
140140
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
141-
day14, day15, day16, day17, day18, day19, day20, day22
141+
day14, day15, day16, day17, day18, day19, day20, day22, day23
142142
);

src/year2024/day23.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//! # LAN Party
2+
use crate::util::hash::*;
3+
4+
type Input = (FastMap<usize, Vec<usize>>, Vec<[bool; 676]>);
5+
6+
pub fn parse(input: &str) -> Input {
7+
let mut nodes = FastMap::with_capacity(1_000);
8+
let mut edges = vec![[false; 676]; 676];
9+
10+
let to_index = |b: &[u8]| 26 * to_usize(b[0]) + to_usize(b[1]);
11+
let empty = || Vec::with_capacity(16);
12+
13+
for edge in input.as_bytes().chunks(6) {
14+
let from = to_index(&edge[..2]);
15+
let to = to_index(&edge[3..]);
16+
17+
nodes.entry(from).or_insert_with(empty).push(to);
18+
nodes.entry(to).or_insert_with(empty).push(from);
19+
20+
edges[from][to] = true;
21+
edges[to][from] = true;
22+
}
23+
24+
(nodes, edges)
25+
}
26+
27+
pub fn part1(input: &Input) -> usize {
28+
let (nodes, edges) = input;
29+
let mut seen = [false; 676];
30+
let mut triangles = 0;
31+
32+
for n1 in 494..520 {
33+
if let Some(neighbours) = nodes.get(&n1) {
34+
seen[n1] = true;
35+
36+
for (i, &n2) in neighbours.iter().enumerate() {
37+
for &n3 in neighbours.iter().skip(i) {
38+
if !seen[n2] && !seen[n3] && edges[n2][n3] {
39+
triangles += 1;
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
triangles
47+
}
48+
49+
pub fn part2(input: &Input) -> String {
50+
let (nodes, edges) = input;
51+
let mut seen = [false; 676];
52+
let mut clique = Vec::new();
53+
let mut largest = Vec::new();
54+
55+
for (&n1, neighbours) in nodes {
56+
if !seen[n1] {
57+
clique.clear();
58+
clique.push(n1);
59+
60+
for &n2 in neighbours {
61+
if clique.iter().all(|&c| edges[n2][c]) {
62+
seen[n2] = true;
63+
clique.push(n2);
64+
}
65+
}
66+
67+
if clique.len() > largest.len() {
68+
largest.clone_from(&clique);
69+
}
70+
}
71+
}
72+
73+
let mut result = String::new();
74+
largest.sort_unstable();
75+
76+
for n in largest {
77+
result.push(to_char(n / 26));
78+
result.push(to_char(n % 26));
79+
result.push(',');
80+
}
81+
82+
result.pop();
83+
result
84+
}
85+
86+
fn to_usize(b: u8) -> usize {
87+
(b - b'a') as usize
88+
}
89+
90+
fn to_char(u: usize) -> char {
91+
((u as u8) + b'a') as char
92+
}

tests/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ test!(year2023
8181

8282
test!(year2024
8383
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
84-
day14, day15, day16, day17, day18, day19, day20, day22
84+
day14, day15, day16, day17, day18, day19, day20, day22, day23
8585
);

tests/year2024/day23.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use aoc::year2024::day23::*;
2+
3+
const EXAMPLE: &str = "\
4+
kh-tc
5+
qp-kh
6+
de-cg
7+
ka-co
8+
yn-aq
9+
qp-ub
10+
cg-tb
11+
vc-aq
12+
tb-ka
13+
wh-tc
14+
yn-cg
15+
kh-ub
16+
ta-co
17+
de-co
18+
tc-td
19+
tb-wq
20+
wh-td
21+
ta-ka
22+
td-qp
23+
aq-cg
24+
wq-ub
25+
ub-vc
26+
de-ta
27+
wq-aq
28+
wq-vc
29+
wh-yn
30+
ka-de
31+
kh-ta
32+
co-tc
33+
wh-qp
34+
tb-vc
35+
td-yn";
36+
37+
#[test]
38+
fn part1_test() {
39+
let input = parse(EXAMPLE);
40+
assert_eq!(part1(&input), 7);
41+
}
42+
43+
#[test]
44+
fn part2_test() {
45+
let input = parse(EXAMPLE);
46+
assert_eq!(part2(&input), "co,de,ka,ta");
47+
}

0 commit comments

Comments
 (0)