Skip to content

Commit 58e292b

Browse files
committed
AOC 2025 day 2
1 parent 46a9575 commit 58e292b

11 files changed

Lines changed: 83 additions & 15 deletions

File tree

adventofcode/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,14 +1658,14 @@
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 (2/24)
1661+
# 2025 (4/24)
16621662

16631663
| # | Name | Solution | Test |
16641664
|---------:|---------------------------------------------|:------------------------------------:|:---------------------------------:|
16651665
| 20250101 | [Day 1: Secret Entrance][20250101] | ✅[💾][20250101solution] | ✅[💾][20250101tests] |
16661666
| 20250102 | [Day 1: Secret Entrance - Part 2][20250102] | ✅[💾][20250102solution] | ✅[💾][20250102tests] |
1667-
| 20250201 | [Day 2: ][20250201] | [💾][20250201solution] | [💾][20250201tests] |
1668-
| 20250202 | [Day 2: - Part 2][20250202] | [💾][20250202solution] | [💾][20250202tests] |
1667+
| 20250201 | [Day 2: Gift Shop][20250201] | ✅[💾][20250201solution] | ✅[💾][20250201tests] |
1668+
| 20250202 | [Day 2: Gift Shop - Part 2][20250202] | ✅[💾][20250202solution] | ✅[💾][20250202tests] |
16691669
| 20250301 | [Day 3: ][20250301] | [💾][20250301solution] | [💾][20250301tests] |
16701670
| 20250302 | [Day 3: - Part 2][20250302] | [💾][20250302solution] | [💾][20250302tests] |
16711671
| 20250401 | [Day 4: ][20250401] | [💾][20250401solution] | [💾][20250401tests] |
Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,96 @@
11
package org.ck.adventofcode.year2025;
22

3+
import java.util.HashSet;
34
import java.util.Scanner;
5+
import java.util.Set;
6+
import java.util.function.ToIntFunction;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
49
import org.ck.adventofcode.util.AOCSolution;
510
import org.ck.codechallengelib.annotation.Solution;
611

712
@Solution(
813
id = 20250201,
9-
name = "Day 2: ",
14+
name = "Day 2: Gift Shop",
1015
url = "https://adventofcode.com/2025/day/2",
11-
category = "2025",
12-
solved = false)
16+
category = "2025")
1317
@Solution(
1418
id = 20250202,
15-
name = "Day 2: - Part 2",
19+
name = "Day 2: Gift Shop - Part 2",
1620
url = "https://adventofcode.com/2025/day/2#part2",
17-
category = "2025",
18-
solved = false)
21+
category = "2025")
1922
public class Day02 extends AOCSolution {
23+
static final Pattern rangePattern = Pattern.compile("(?<start>\\d+)-(?<end>\\d+)");
2024

2125
@Override
2226
protected void runPartOne(final Scanner in) {
23-
run(in);
27+
run(in, _ -> 2);
2428
}
2529

2630
@Override
2731
protected void runPartTwo(final Scanner in) {
28-
run(in);
32+
run(in, startLength -> startLength);
2933
}
3034

31-
private void run(final Scanner in) {
32-
// not yet implemented
35+
private void run(final Scanner in, final ToIntFunction<Integer> getMaxRepetitions) {
36+
final String[] ranges = in.nextLine().split(",");
37+
38+
final Set<Long> invalidIds = new HashSet<>();
39+
for (final String range : ranges) {
40+
final Matcher matcher = rangePattern.matcher(range);
41+
42+
if (matcher.find()) {
43+
final String startString = matcher.group("start");
44+
final String endString = matcher.group("end");
45+
46+
final long end = Long.parseLong(endString);
47+
48+
for (int repetitions = 2;
49+
repetitions <= getMaxRepetitions.applyAsInt(endString.length());
50+
++repetitions) {
51+
invalidIds.addAll(generateInvalidIds(repetitions, startString, end));
52+
}
53+
}
54+
}
55+
56+
print(invalidIds.stream().mapToLong(Long::longValue).sum());
57+
}
58+
59+
private static Set<Long> generateInvalidIds(
60+
final int repetitions, final String startString, final long end) {
61+
final long start = Long.parseLong(startString);
62+
final int startLength = startString.length();
63+
64+
final int halfLength = startLength / repetitions;
65+
final long startHalf =
66+
halfLength > 0 ? Long.parseLong(startString.substring(0, halfLength)) : 1;
67+
68+
final Set<Long> invalidIds = new HashSet<>();
69+
for (long numberHalf = startHalf; ; ++numberHalf) {
70+
final long number = getInvalidId(numberHalf, repetitions);
71+
72+
if (number > end) {
73+
break;
74+
}
75+
76+
if (number >= start) {
77+
invalidIds.add(number);
78+
}
79+
}
80+
81+
return invalidIds;
82+
}
83+
84+
private static long getInvalidId(final long numberHalf, final int repetitions) {
85+
final int currentLength = (int) Math.log10(numberHalf) + 1;
86+
final long shiftAmount = (long) Math.pow(10, currentLength);
87+
88+
long number = numberHalf;
89+
for (int i = 1; i < repetitions; ++i) {
90+
number *= shiftAmount;
91+
number += numberHalf;
92+
}
93+
94+
return number;
3395
}
3496
}

adventofcode/src/test/java/org/ck/adventofcode/year2025/Day02Test.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 Day02Test 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+
9188031749
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7CFSkaJB5BuU4L0UpjPkcQ==@2ZSj7+LhetXVMQHTyRuyzBPwa4WV+Y7e2wNv4QksyK5eRHVYnpVXnrxN6tpwREGOXgmfewHPFjp/t+p3yEctsHa2edQ3VUG0WiBC+LZi2NQAgOOTmbqXi8eJlJg5xOqEwp1nS5rJVfz4g9+/OYtrA1MNP6bJfa7y4QGsqbhgmbg1dLYgxkCyg6S5uy9HfBbfhcDn565pjXU4UlKggWuxqqcEMBDjNe8x9+iMokiau5/KYAutpMOPUDyR4mrTgpHes9PaI0BqVW6LOusG9rRDhsxk1Jk0WTDR+Bh9a4smFfGBYynlX5iVqxv/D1pQch04v26q2N8S/YnSC3NxiIda3fNIzvtYuNIRAoloOOFRqsNVjfeRqMbJy5tyGxxYFg7hVzDAf0MMlc0zMXbaNG0GT9oVlBWG6f6nOamYxAefjDbze1a585u+KgM1VG1sInGaxbdJ1Cv8Eb9NltMTQZUJKROTPCtFvy4hZMJP8zrT19geOLrp4Cri0xlILaYq6RXsc9YIRpCRiWwTm3Cr7mnZYEQ8JNwIAzRJQCXiF5TcUI9ncJVJI/9sfKpnv5AhaRyElcVJEbrH322VGtFILZTQemmQCCxmEC+f3eVFahSbCg2rK89EnA==
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1227775554
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11323661261
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7CFSkaJB5BuU4L0UpjPkcQ==@2ZSj7+LhetXVMQHTyRuyzBPwa4WV+Y7e2wNv4QksyK5eRHVYnpVXnrxN6tpwREGOXgmfewHPFjp/t+p3yEctsHa2edQ3VUG0WiBC+LZi2NQAgOOTmbqXi8eJlJg5xOqEwp1nS5rJVfz4g9+/OYtrA1MNP6bJfa7y4QGsqbhgmbg1dLYgxkCyg6S5uy9HfBbfhcDn565pjXU4UlKggWuxqqcEMBDjNe8x9+iMokiau5/KYAutpMOPUDyR4mrTgpHes9PaI0BqVW6LOusG9rRDhsxk1Jk0WTDR+Bh9a4smFfGBYynlX5iVqxv/D1pQch04v26q2N8S/YnSC3NxiIda3fNIzvtYuNIRAoloOOFRqsNVjfeRqMbJy5tyGxxYFg7hVzDAf0MMlc0zMXbaNG0GT9oVlBWG6f6nOamYxAefjDbze1a585u+KgM1VG1sInGaxbdJ1Cv8Eb9NltMTQZUJKROTPCtFvy4hZMJP8zrT19geOLrp4Cri0xlILaYq6RXsc9YIRpCRiWwTm3Cr7mnZYEQ8JNwIAzRJQCXiF5TcUI9ncJVJI/9sfKpnv5AhaRyElcVJEbrH322VGtFILZTQemmQCCxmEC+f3eVFahSbCg2rK89EnA==
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4174379265

0 commit comments

Comments
 (0)