Skip to content

Commit caf1869

Browse files
committed
aoc 2024 day 23
1 parent 2be1db4 commit caf1869

File tree

11 files changed

+158
-16
lines changed

11 files changed

+158
-16
lines changed

adventofcode/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@
14541454
[20232402tests]: src/test/java/org/ck/adventofcode/year2023/Day24Test.java
14551455
[20232501tests]: src/test/java/org/ck/adventofcode/year2023/Day25Test.java
14561456

1457-
# 2024 (44/49)
1457+
# 2024 (46/49)
14581458

14591459
| # | Name | Solution | Test |
14601460
|---------:|-----------------------------------------------------|:------------------------------------:|:---------------------------------:|
@@ -1502,8 +1502,8 @@
15021502
| 20242102 | [Day 21: Keypad Conundrum - Part 2][20242102] | ✅[💾][20242102solution] | ✅[💾][20242102tests] |
15031503
| 20242201 | [Day 22: Monkey Market][20242201] | ✅[💾][20242201solution] | ✅[💾][20242201tests] |
15041504
| 20242202 | [Day 22: Monkey Market - Part 2][20242202] | ✅[💾][20242202solution] | ✅[💾][20242202tests] |
1505-
| 20242301 | [Day 23: ?][20242301] | [💾][20242301solution] | [💾][20242301tests] |
1506-
| 20242302 | [Day 23: ? - Part 2][20242302] | [💾][20242302solution] | [💾][20242302tests] |
1505+
| 20242301 | [Day 23: LAN Party][20242301] | ✅[💾][20242301solution] | ✅[💾][20242301tests] |
1506+
| 20242302 | [Day 23: LAN Party - Part 2][20242302] | ✅[💾][20242302solution] | ✅[💾][20242302tests] |
15071507
| 20242401 | [Day 24: ?][20242401] | [💾][20242401solution] | [💾][20242401tests] |
15081508
| 20242402 | [Day 24: ? - Part 2][20242402] | [💾][20242402solution] | [💾][20242402tests] |
15091509
| 20242501 | [Day 25: ?][20242501] | [💾][20242501solution] | [💾][20242501tests] |
Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,108 @@
11
package org.ck.adventofcode.year2024;
22

3-
import java.util.Scanner;
3+
import java.util.*;
4+
import java.util.function.Function;
5+
import java.util.stream.Collectors;
46
import org.ck.adventofcode.util.AOCSolution;
57
import org.ck.codechallengelib.annotation.Solution;
68

79
@Solution(
810
id = 20242301,
9-
name = "Day 23: ?",
11+
name = "Day 23: LAN Party",
1012
url = "https://adventofcode.com/2024/day/23",
11-
category = "2024",
12-
solved = false)
13+
category = "2024")
1314
@Solution(
1415
id = 20242302,
15-
name = "Day 23: ? - Part 2",
16+
name = "Day 23: LAN Party - Part 2",
1617
url = "https://adventofcode.com/2024/day/23#part2",
17-
category = "2024",
18-
solved = false)
18+
category = "2024")
1919
public class Day23 extends AOCSolution {
2020

2121
@Override
2222
protected void runPartOne(final Scanner in) {
23-
run(in);
23+
run(in, Day23::getGroupsOfThree);
2424
}
2525

2626
@Override
2727
protected void runPartTwo(final Scanner in) {
28-
run(in);
28+
run(in, Day23::getLargestGroup);
2929
}
3030

31-
private void run(final Scanner in) {
32-
print("Whoopsie");
31+
private void run(final Scanner in, final Function<Map<String, Set<String>>, String> getResult) {
32+
final Map<String, Set<String>> reachable = new HashMap<>();
33+
34+
while (in.hasNextLine()) {
35+
final String[] names = in.nextLine().split("-");
36+
37+
reachable.computeIfAbsent(names[0], k -> new HashSet<>()).add(names[1]);
38+
reachable.computeIfAbsent(names[1], k -> new HashSet<>()).add(names[0]);
39+
}
40+
41+
print(getResult.apply(reachable));
42+
}
43+
44+
private static String getGroupsOfThree(Map<String, Set<String>> reachable) {
45+
final Set<String> tNames =
46+
reachable.keySet().stream()
47+
.filter(name -> name.startsWith("t"))
48+
.collect(Collectors.toSet());
49+
50+
final Set<Set<String>> networks = new HashSet<>();
51+
52+
for (String tName : tNames) {
53+
for (String second : reachable.get(tName)) {
54+
for (String third : reachable.get(tName)) {
55+
if (reachable.get(second).contains(third)) {
56+
networks.add(Set.of(tName, second, third));
57+
}
58+
}
59+
}
60+
}
61+
62+
return String.valueOf(networks.size());
63+
}
64+
65+
private static String getLargestGroup(Map<String, Set<String>> reachable) {
66+
Set<String> largestNetwork = new HashSet<>();
67+
68+
for (String tName : reachable.keySet()) {
69+
Set<String> network = new HashSet<>();
70+
network.add(tName);
71+
72+
for (String second : reachable.get(tName)) {
73+
for (String third : reachable.get(tName)) {
74+
if (second.equals(third)) {
75+
continue;
76+
}
77+
78+
if (reachable.get(second).contains(third)) {
79+
network.add(second);
80+
network.add(third);
81+
}
82+
}
83+
}
84+
85+
final Iterator<String> iterator = network.iterator();
86+
while (iterator.hasNext()) {
87+
final String name = iterator.next();
88+
89+
for (String other : network) {
90+
if (name.equals(other)) {
91+
continue;
92+
}
93+
94+
if (!reachable.get(other).contains(name)) {
95+
iterator.remove();
96+
break;
97+
}
98+
}
99+
}
100+
101+
if (network.size() > largestNetwork.size()) {
102+
largestNetwork = network;
103+
}
104+
}
105+
106+
return largestNetwork.stream().sorted().collect(Collectors.joining(","));
33107
}
34108
}

adventofcode/src/test/java/org/ck/adventofcode/year2024/Day23Test.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.ck.adventofcode.year2024;
22

33
import org.ck.adventofcode.util.BaseAOCTest;
4-
import org.junit.jupiter.api.Disabled;
54
import org.junit.jupiter.params.ParameterizedTest;
65
import org.junit.jupiter.params.provider.ValueSource;
76

8-
@Disabled
97
class Day23Test extends BaseAOCTest {
108
@ParameterizedTest
119
@ValueSource(strings = {"01a"})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1337

adventofcode/src/test/resources/org/ck/adventofcode/year2024/day23/01.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aw,fk,gv,hi,hp,ip,jy,kc,lk,og,pj,re,sr

adventofcode/src/test/resources/org/ck/adventofcode/year2024/day23/02.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
co,de,ka,ta

0 commit comments

Comments
 (0)