Skip to content

Commit bdedc91

Browse files
committed
AoC 2025 Day 2 - java
1 parent 01d2d00 commit bdedc91

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
1111
| python3 | [](src/main/python/AoC2025_01.py) | [](src/main/python/AoC2025_02.py) | | | | | | | | | | |
12-
| java | [](src/main/java/AoC2025_01.java) | | | | | | | | | | | |
12+
| java | [](src/main/java/AoC2025_01.java) | [](src/main/java/AoC2025_02.java) | | | | | | | | | | |
1313
| bash | [](src/main/bash/AoC2025_01.sh) | | | | | | | | | | | |
1414
<!-- @END:ImplementationsTable:2025@ -->
1515

src/main/java/AoC2025_02.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import com.github.pareronia.aoc.StringOps;
2+
import com.github.pareronia.aoc.StringOps.StringSplit;
3+
import com.github.pareronia.aoc.solution.Sample;
4+
import com.github.pareronia.aoc.solution.Samples;
5+
import com.github.pareronia.aoc.solution.SolutionBase;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.regex.Pattern;
10+
import java.util.stream.LongStream;
11+
12+
@SuppressWarnings({"PMD.ClassNamingConventions", "PMD.NoPackage"})
13+
public final class AoC2025_02 extends SolutionBase<List<AoC2025_02.Range>, Long, Long> {
14+
15+
private AoC2025_02(final boolean debug) {
16+
super(debug);
17+
}
18+
19+
public static AoC2025_02 create() {
20+
return new AoC2025_02(false);
21+
}
22+
23+
public static AoC2025_02 createDebug() {
24+
return new AoC2025_02(true);
25+
}
26+
27+
@Override
28+
protected List<Range> parseInput(final List<String> inputs) {
29+
return Arrays.stream(inputs.getFirst().split(","))
30+
.map(
31+
s -> {
32+
final StringSplit sp = StringOps.splitOnce(s, "-");
33+
return new Range(Long.parseLong(sp.left()), Long.parseLong(sp.right()));
34+
})
35+
.toList();
36+
}
37+
38+
private long solve(final List<Range> ranges, final String pattern) {
39+
final Pattern regex = Pattern.compile(pattern);
40+
return ranges.stream()
41+
.flatMapToLong(
42+
range ->
43+
LongStream.rangeClosed(range.from, range.to)
44+
.filter(n -> regex.matcher(String.valueOf(n)).matches()))
45+
.sum();
46+
}
47+
48+
@Override
49+
public Long solvePart1(final List<Range> ranges) {
50+
return this.solve(ranges, "^(\\d+)\\1$");
51+
}
52+
53+
@Override
54+
public Long solvePart2(final List<Range> ranges) {
55+
return this.solve(ranges, "^(\\d+)\\1+$");
56+
}
57+
58+
@Samples({
59+
@Sample(method = "part1", input = TEST, expected = "1227775554"),
60+
@Sample(method = "part2", input = TEST, expected = "4174379265"),
61+
})
62+
public static void main(final String[] args) throws Exception {
63+
create().run();
64+
}
65+
66+
private static final String TEST =
67+
"""
68+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,\
69+
446443-446449,38593856-38593862,565653-565659,824824821-824824827,\
70+
2121212118-2121212124
71+
""";
72+
73+
record Range(long from, long to) {}
74+
}

0 commit comments

Comments
 (0)