Skip to content

Commit b018902

Browse files
committed
AOC 2025 day 6
1 parent ac672d4 commit b018902

File tree

11 files changed

+122
-15
lines changed

11 files changed

+122
-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 (10/24)
1661+
# 2025 (12/24)
16621662

16631663
| # | Name | Solution | Test |
16641664
|---------:|-------------------------------------------------|:------------------------------------:|:---------------------------------:|
@@ -1672,8 +1672,8 @@
16721672
| 20250402 | [Day 4: Printing Department - Part 2][20250402] | ✅[💾][20250402solution] | ✅[💾][20250402tests] |
16731673
| 20250501 | [Day 5: Cafeteria ][20250501] | ✅[💾][20250501solution] | ✅[💾][20250501tests] |
16741674
| 20250502 | [Day 5: Cafeteria - Part 2][20250502] | ✅[💾][20250502solution] | ✅[💾][20250502tests] |
1675-
| 20250601 | [Day 6: ][20250601] | [💾][20250601solution] | [💾][20250601tests] |
1676-
| 20250602 | [Day 6: - Part 2][20250602] | [💾][20250602solution] | [💾][20250602tests] |
1675+
| 20250601 | [Day 6: Trash Compactor][20250601] | ✅[💾][20250601solution] | ✅[💾][20250601tests] |
1676+
| 20250602 | [Day 6: Trash Compactor - Part 2][20250602] | ✅[💾][20250602solution] | ✅[💾][20250602tests] |
16771677
| 20250701 | [Day 7: ][20250701] | [💾][20250701solution] | [💾][20250701tests] |
16781678
| 20250702 | [Day 7: - Part 2][20250702] | [💾][20250702solution] | [💾][20250702tests] |
16791679
| 20250801 | [Day 8: ][20250801] | [💾][20250801solution] | [💾][20250801tests] |
Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,129 @@
11
package org.ck.adventofcode.year2025;
22

33
import java.util.*;
4+
import java.util.function.ToLongFunction;
45
import org.ck.adventofcode.util.AOCSolution;
56
import org.ck.codechallengelib.annotation.Solution;
67

78
@Solution(
89
id = 20250601,
9-
name = "Day 6: ",
10+
name = "Day 6: Trash Compactor",
1011
url = "https://adventofcode.com/2025/day/6",
11-
category = "2025",
12-
solved = false)
12+
category = "2025")
1313
@Solution(
1414
id = 20250602,
15-
name = "Day 6: - Part 2",
15+
name = "Day 6: Trash Compactor - Part 2",
1616
url = "https://adventofcode.com/2025/day/6#part2",
17-
category = "2025",
18-
solved = false)
17+
category = "2025")
1918
public class Day06 extends AOCSolution {
2019

2120
@Override
2221
protected void runPartOne(final Scanner in) {
23-
run(in);
22+
run(in, Day06::getPart1Result);
2423
}
2524

2625
@Override
2726
protected void runPartTwo(final Scanner in) {
28-
run(in);
27+
run(in, Day06::getPart2Result);
2928
}
3029

31-
private void run(final Scanner in) {
32-
// not yet implemented
30+
private void run(final Scanner in, final ToLongFunction<List<List<String>>> getResult) {
31+
final List<String> inputs = new ArrayList<>();
32+
33+
while (in.hasNextLine()) {
34+
inputs.add(in.nextLine());
35+
}
36+
37+
final String operations = inputs.getLast();
38+
final List<List<String>> columns = new ArrayList<>();
39+
for (int j = 0; j < inputs.size() - 1; ++j) {
40+
columns.add(new ArrayList<>());
41+
}
42+
43+
int start = 1;
44+
while (true) {
45+
final int nextPlus = operations.indexOf('+', start);
46+
final int nextTimes = operations.indexOf('*', start);
47+
48+
final int nextNumberStart;
49+
if (nextPlus == -1 && nextTimes == -1) {
50+
break;
51+
} else if (nextPlus == -1) {
52+
nextNumberStart = nextTimes;
53+
} else if (nextTimes == -1) {
54+
nextNumberStart = nextPlus;
55+
} else {
56+
nextNumberStart = Math.min(nextPlus, nextTimes);
57+
}
58+
59+
for (int j = 0; j < inputs.size() - 1; ++j) {
60+
columns.get(j).add(inputs.get(j).substring(start - 1, nextNumberStart - 1));
61+
}
62+
63+
start = nextNumberStart + 1;
64+
}
65+
for (int j = 0; j < inputs.size() - 1; ++j) {
66+
columns.get(j).add(inputs.get(j).substring(start - 1));
67+
}
68+
69+
columns.add(new ArrayList<>(Arrays.asList(operations.trim().split("\\s+"))));
70+
71+
print(getResult.applyAsLong(columns));
72+
}
73+
74+
private static Long getPart1Result(List<List<String>> inputs) {
75+
long sum = 0;
76+
for (int i = 0; i < inputs.get(0).size(); ++i) {
77+
final String operation = inputs.get(inputs.size() - 1).get(i);
78+
long result = "*".equals(operation) ? 1 : 0;
79+
80+
for (int j = 0; j < inputs.size() - 1; ++j) {
81+
switch (operation) {
82+
case "+" -> result += Long.parseLong(inputs.get(j).get(i).trim());
83+
case "*" -> result *= Long.parseLong(inputs.get(j).get(i).trim());
84+
}
85+
}
86+
87+
sum += result;
88+
}
89+
90+
return sum;
91+
}
92+
93+
private static Long getPart2Result(List<List<String>> inputs) {
94+
long sum = 0;
95+
for (int i = 0; i < inputs.get(0).size(); ++i) {
96+
int numbersCount = 0;
97+
for (int j = 0; j < inputs.size() - 1; ++j) {
98+
numbersCount = Math.max(numbersCount, inputs.get(j).get(i).length());
99+
}
100+
101+
final List<Long> numbers = new ArrayList<>();
102+
for (int numberIndex = 0; numberIndex < numbersCount; ++numberIndex) {
103+
long number = 0;
104+
for (int j = 0; j < inputs.size() - 1; ++j) {
105+
char current = inputs.get(j).get(i).charAt(numberIndex);
106+
107+
if (current != ' ') {
108+
number = number * 10 + (current - '0');
109+
}
110+
}
111+
112+
numbers.add(number);
113+
}
114+
115+
final String operation = inputs.get(inputs.size() - 1).get(i);
116+
long result = "*".equals(operation) ? 1 : 0;
117+
for (Long number : numbers) {
118+
switch (operation) {
119+
case "+" -> result += number;
120+
case "*" -> result *= number;
121+
}
122+
}
123+
124+
sum += result;
125+
}
126+
127+
return sum;
33128
}
34129
}

adventofcode/src/test/java/org/ck/adventofcode/year2025/Day06Test.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 Day06Test 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+
6343365546996

adventofcode/src/test/resources/org/ck/adventofcode/year2025/day06/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+
4277556
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
123 328 51 64
2+
45 64 387 23
3+
6 98 215 314
4+
* + * +
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11136895955912

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

0 commit comments

Comments
 (0)