Skip to content

Commit e9194f9

Browse files
committed
AOC 2025 day 8
1 parent 5fb757b commit e9194f9

File tree

11 files changed

+162
-15
lines changed

11 files changed

+162
-15
lines changed

adventofcode/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@
16581658
[20242402tests]: src/test/java/org/ck/adventofcode/year2024/Day24Test.java
16591659
[20242501tests]: src/test/java/org/ck/adventofcode/year2024/Day25Test.java
16601660

1661-
# 2025 (14/24)
1661+
# 2025 (16/24)
16621662

16631663
| # | Name | Solution | Test |
16641664
|---------:|-------------------------------------------------|:------------------------------------:|:---------------------------------:|
@@ -1676,8 +1676,8 @@
16761676
| 20250602 | [Day 6: Trash Compactor - Part 2][20250602] | ✅[💾][20250602solution] | ✅[💾][20250602tests] |
16771677
| 20250701 | [Day 7: Laboratories ][20250701] | ✅[💾][20250701solution] | ✅[💾][20250701tests] |
16781678
| 20250702 | [Day 7: Laboratories - Part 2][20250702] | ✅[💾][20250702solution] | ✅[💾][20250702tests] |
1679-
| 20250801 | [Day 8: ][20250801] | [💾][20250801solution] | [💾][20250801tests] |
1680-
| 20250802 | [Day 8: - Part 2][20250802] | [💾][20250802solution] | [💾][20250802tests] |
1679+
| 20250801 | [Day 8: Playground][20250801] | ✅[💾][20250801solution] | ✅[💾][20250801tests] |
1680+
| 20250802 | [Day 8: Playground - Part 2][20250802] | ✅[💾][20250802solution] | ✅[💾][20250802tests] |
16811681
| 20250901 | [Day 9: ][20250901] | [💾][20250901solution] | [💾][20250901tests] |
16821682
| 20250902 | [Day 9: - Part 2][20250902] | [💾][20250902solution] | [💾][20250902tests] |
16831683
| 20251001 | [Day 10: ][20251001] | [💾][20251001solution] | [💾][20251001tests] |
Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,136 @@
11
package org.ck.adventofcode.year2025;
22

33
import java.util.*;
4+
import java.util.function.ToLongBiFunction;
5+
import java.util.stream.LongStream;
46
import org.ck.adventofcode.util.AOCSolution;
57
import org.ck.codechallengelib.annotation.Solution;
68

79
@Solution(
810
id = 20250801,
9-
name = "Day 8: ",
11+
name = "Day 8: Playground",
1012
url = "https://adventofcode.com/2025/day/8",
11-
category = "2025",
12-
solved = false)
13+
category = "2025")
1314
@Solution(
1415
id = 20250802,
15-
name = "Day 8: - Part 2",
16+
name = "Day 8: Playground - Part 2",
1617
url = "https://adventofcode.com/2025/day/8#part2",
17-
category = "2025",
18-
solved = false)
18+
category = "2025")
1919
public class Day08 extends AOCSolution {
2020

2121
@Override
2222
protected void runPartOne(final Scanner in) {
23-
run(in);
23+
final int connections = Integer.parseInt(in.nextLine());
24+
25+
run(
26+
in,
27+
connections,
28+
(networks, _) -> {
29+
final Queue<Network> sortedNetworks =
30+
new PriorityQueue<>(Comparator.comparing(Network::size).reversed());
31+
sortedNetworks.addAll(networks);
32+
33+
return (long) sortedNetworks.poll().boxes().size()
34+
* sortedNetworks.poll().boxes().size()
35+
* sortedNetworks.poll().boxes().size();
36+
});
2437
}
2538

2639
@Override
2740
protected void runPartTwo(final Scanner in) {
28-
run(in);
41+
run(in, -1, (_, xCoordinates) -> xCoordinates.reduce(1, (a, b) -> a * b));
42+
}
43+
44+
private void run(
45+
final Scanner in,
46+
int stopAfter,
47+
final ToLongBiFunction<Set<Network>, LongStream> resultGetter) {
48+
final Set<Box> boxes = new HashSet<>();
49+
final Queue<Distance> distances = new PriorityQueue<>(Comparator.comparing(Distance::distance));
50+
51+
parseInput(in, boxes, distances);
52+
53+
final Set<Network> networks = new HashSet<>();
54+
LongStream xCoordinates = null;
55+
boolean continueLoop = true;
56+
while (continueLoop) {
57+
final Distance distance = distances.poll();
58+
if (distance == null) {
59+
throw new IllegalStateException("Distance is null");
60+
}
61+
62+
final Set<Network> effectedNetworks = getEffectedNetworks(networks, distance);
63+
64+
if (effectedNetworks.isEmpty()) {
65+
networks.add(new Network(distance.boxes()));
66+
} else {
67+
final HashSet<Box> merged = new HashSet<>(distance.boxes());
68+
69+
for (final Network effectedNetwork : effectedNetworks) {
70+
merged.addAll(effectedNetwork.boxes());
71+
networks.remove(effectedNetwork);
72+
}
73+
74+
final Network newNetwork = new Network(merged);
75+
networks.add(newNetwork);
76+
77+
if (newNetwork.size() == boxes.size()) {
78+
xCoordinates = distance.boxes().stream().mapToLong(Box::x);
79+
continueLoop = false;
80+
}
81+
}
82+
83+
if (--stopAfter == 0) {
84+
continueLoop = false;
85+
}
86+
}
87+
88+
print(resultGetter.applyAsLong(networks, xCoordinates));
2989
}
3090

31-
private void run(final Scanner in) {
32-
// not yet implemented
91+
private static Set<Network> getEffectedNetworks(
92+
final Set<Network> networks, final Distance distance) {
93+
final Set<Network> effectedNetworks = new HashSet<>();
94+
95+
for (final Network network : networks) {
96+
for (final Box box : distance.boxes()) {
97+
if (network.boxes().contains(box)) {
98+
effectedNetworks.add(network);
99+
}
100+
}
101+
}
102+
return effectedNetworks;
103+
}
104+
105+
private void parseInput(final Scanner in, final Set<Box> boxes, final Queue<Distance> distances) {
106+
while (in.hasNextLine()) {
107+
final String[] line = in.nextLine().split(",");
108+
final Box box =
109+
new Box(Integer.parseInt(line[0]), Integer.parseInt(line[1]), Integer.parseInt(line[2]));
110+
111+
for (final Box other : boxes) {
112+
distances.add(new Distance(Set.of(box, other), getDistance(box, other)));
113+
}
114+
115+
boxes.add(box);
116+
}
117+
}
118+
119+
private double getDistance(final Box box, final Box other) {
120+
final long dx = box.x - other.x;
121+
final long dy = box.y - other.y;
122+
final long dz = box.z - other.z;
123+
124+
return Math.sqrt((double) dx * dx + dy * dy + dz * dz);
125+
}
126+
127+
private record Box(long x, long y, long z) {}
128+
129+
private record Distance(Set<Box> boxes, double distance) {}
130+
131+
private record Network(Set<Box> boxes) {
132+
public int size() {
133+
return boxes.size();
134+
}
33135
}
34136
}

adventofcode/src/test/java/org/ck/adventofcode/year2025/Day08Test.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.ck.adventofcode.year2025;
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 Day08Test 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+
123234

adventofcode/src/test/resources/org/ck/adventofcode/year2025/day08/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+
40
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
10
2+
162,817,812
3+
57,618,57
4+
906,360,560
5+
592,479,940
6+
352,342,300
7+
466,668,158
8+
542,29,236
9+
431,825,988
10+
739,650,466
11+
52,470,668
12+
216,146,977
13+
819,987,18
14+
117,168,530
15+
805,96,715
16+
346,949,466
17+
970,615,88
18+
941,993,340
19+
862,61,35
20+
984,92,344
21+
425,690,689
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9259958565

adventofcode/src/test/resources/org/ck/adventofcode/year2025/day08/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+
25272

0 commit comments

Comments
 (0)