Skip to content

Commit 421c70f

Browse files
committed
Add solution for Advent of Code 2025, Day 2, Part 2
1 parent 7a118ee commit 421c70f

File tree

2 files changed

+97
-28
lines changed

2 files changed

+97
-28
lines changed

src/main/java/by/andd3dfx/game/GiftShop.java

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.InputStream;
1010
import java.io.InputStreamReader;
1111
import java.util.HashSet;
12+
import java.util.function.Function;
1213

1314
/**
1415
* <pre>
@@ -49,13 +50,64 @@
4950
* Adding up all the invalid IDs in this example produces 1227775554.
5051
*
5152
* What do you get if you add up all of the invalid IDs?
53+
*
54+
* Your puzzle answer was 23534117921.
55+
*
56+
* --- Part Two ---
57+
*
58+
* The clerk quickly discovers that there are still invalid IDs in the ranges in your list. Maybe the young Elf was doing other silly patterns as well?
59+
*
60+
* Now, an ID is invalid if it is made only of some sequence of digits repeated at least twice. So, 12341234 (1234 two times), 123123123 (123 three times), 1212121212 (12 five times), and 1111111 (1 seven times) are all invalid IDs.
61+
*
62+
* From the same example as before:
63+
*
64+
* 11-22 still has two invalid IDs, 11 and 22.
65+
* 95-115 now has two invalid IDs, 99 and 111.
66+
* 998-1012 now has two invalid IDs, 999 and 1010.
67+
* 1188511880-1188511890 still has one invalid ID, 1188511885.
68+
* 222220-222224 still has one invalid ID, 222222.
69+
* 1698522-1698528 still contains no invalid IDs.
70+
* 446443-446449 still has one invalid ID, 446446.
71+
* 38593856-38593862 still has one invalid ID, 38593859.
72+
* 565653-565659 now has one invalid ID, 565656.
73+
* 824824821-824824827 now has one invalid ID, 824824824.
74+
* 2121212118-2121212124 now has one invalid ID, 2121212121.
75+
*
76+
* Adding up all the invalid IDs in this example produces 4174379265.
77+
*
78+
* What do you get if you add up all of the invalid IDs using these new rules?
79+
*
80+
* Your puzzle answer was 31755323497.
5281
* </pre>
5382
*
54-
* @see <a href="https://youtu.be/qUsIi-084Xg">Video solution</a>
83+
* @see <a href="https://youtu.be/qUsIi-084Xg">Video solution, Part 1</a>
84+
* @see <a href="https://youtu.be/m45uHY63ZqI">Video solution, Part 2</a>
5585
*/
5686
public class GiftShop {
5787

58-
public static Long determine(String inputString) {
88+
public static Long determinePart1(String inputString) {
89+
return determinePartInner(inputString, GiftShop::isInvalid);
90+
}
91+
92+
public static Long determinePart2(String inputString) {
93+
return determinePartInner(inputString, GiftShop::isInvalid2);
94+
}
95+
96+
private static boolean isInvalid(Long id) {
97+
var str = String.valueOf(id);
98+
var len = str.length();
99+
if (len % 2 == 1) {
100+
return false;
101+
}
102+
return StringUtils.equals(str.substring(0, len / 2), str.substring(len / 2));
103+
}
104+
105+
static boolean isInvalid2(long id) {
106+
var str = String.valueOf(id);
107+
return str.matches("^(\\d+)\\1+$");
108+
}
109+
110+
public static Long determinePartInner(String inputString, Function<Long, Boolean> isInvalidFunc) {
59111
var set = new HashSet<Long>();
60112
var ranges = inputString.split(",");
61113
for (var range : ranges) {
@@ -64,7 +116,7 @@ public static Long determine(String inputString) {
64116
var right = NumberUtils.toLong(borders[1]);
65117

66118
for (var current = left; current <= right; current++) {
67-
if (isInvalid(current)) {
119+
if (isInvalidFunc.apply(current)) {
68120
set.add(current);
69121
}
70122
}
@@ -73,35 +125,22 @@ public static Long determine(String inputString) {
73125
for (var item : set) {
74126
result += item;
75127
}
76-
return result;
77-
}
78128

79-
private static boolean isInvalid(Long id) {
80-
var str = String.valueOf(id);
81-
var len = str.length();
82-
if (len % 2 == 1) {
83-
return false;
84-
}
85-
return StringUtils.equals(str.substring(0, len / 2), str.substring(len / 2));
129+
return result;
86130
}
87131

88132
@SneakyThrows
89133
public static void main(String[] args) {
90134
var inputString = read("/game/gift-shop.txt");
91-
var result = determine(inputString);
92-
System.out.println(result);
93-
// 262060856
135+
var resultPart1 = determinePart1(inputString);
136+
var resultPart2 = determinePart2(inputString);
137+
System.out.println(resultPart1 + ", " + resultPart2);
94138
}
95139

96140
private static String read(String filePathName) throws IOException {
97-
InputStream inputStream = NotQuiteLisp.class.getResourceAsStream(filePathName);
98-
var resultStringBuilder = new StringBuilder();
141+
InputStream inputStream = GiftShop.class.getResourceAsStream(filePathName);
99142
try (var br = new BufferedReader(new InputStreamReader(inputStream))) {
100-
String line;
101-
while ((line = br.readLine()) != null) {
102-
resultStringBuilder.append(line).append("\n");
103-
}
143+
return br.readLine();
104144
}
105-
return resultStringBuilder.toString();
106145
}
107146
}

src/test/java/by/andd3dfx/game/GiftShopTest.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,41 @@
77
public class GiftShopTest {
88

99
@Test
10-
public void determine() {
11-
assertThat(GiftShop.determine("11-22")).isEqualTo(11 + 22);
12-
assertThat(GiftShop.determine("95-115")).isEqualTo(99);
13-
assertThat(GiftShop.determine("1188511880-1188511890")).isEqualTo(1188511885);
14-
assertThat(GiftShop.determine("11-22,95-115,998-1012,1188511880-1188511890,222220-222224," +
15-
"1698522-1698528,446443-446449,38593856-38593862")).isEqualTo(1227775554);
10+
public void determinePart1() {
11+
assertThat(GiftShop.determinePart1("11-22")).isEqualTo(11 + 22);
12+
assertThat(GiftShop.determinePart1("95-115")).isEqualTo(99);
13+
assertThat(GiftShop.determinePart1("1188511880-1188511890")).isEqualTo(1188511885);
14+
assertThat(GiftShop.determinePart1("11-22,95-115,998-1012,1188511880-1188511890,222220-222224," +
15+
"1698522-1698528,446443-446449,38593856-38593862")).isEqualTo(1227775554L);
16+
}
17+
18+
@Test
19+
public void determinePart2() {
20+
assertThat(GiftShop.determinePart2("11-22")).isEqualTo(11 + 22);
21+
assertThat(GiftShop.determinePart2("95-115")).isEqualTo(99 + 111);
22+
assertThat(GiftShop.determinePart2("998-1012")).isEqualTo(999 + 1010);
23+
assertThat(GiftShop.determinePart2("1188511880-1188511890")).isEqualTo(1188511885);
24+
assertThat(GiftShop.determinePart2("222220-222224")).isEqualTo(222222);
25+
assertThat(GiftShop.determinePart2("1698522-1698528")).isEqualTo(0);
26+
assertThat(GiftShop.determinePart2("446443-446449")).isEqualTo(446446);
27+
assertThat(GiftShop.determinePart2("38593856-38593862")).isEqualTo(38593859);
28+
assertThat(GiftShop.determinePart2("565653-565659")).isEqualTo(565656);
29+
assertThat(GiftShop.determinePart2("2121212118-2121212124")).isEqualTo(2121212121);
30+
assertThat(GiftShop.determinePart2("11-22,95-115,998-1012,1188511880-1188511890,222220-222224," +
31+
"1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124")).isEqualTo(4174379265L);
32+
}
33+
34+
@Test
35+
public void isInvalid2() {
36+
assertThat(GiftShop.isInvalid2(1L)).isEqualTo(false);
37+
assertThat(GiftShop.isInvalid2(11L)).isEqualTo(true);
38+
assertThat(GiftShop.isInvalid2(111L)).isEqualTo(true);
39+
assertThat(GiftShop.isInvalid2(1111111L)).isEqualTo(true);
40+
assertThat(GiftShop.isInvalid2(11111117L)).isEqualTo(false);
41+
assertThat(GiftShop.isInvalid2(123L)).isEqualTo(false);
42+
assertThat(GiftShop.isInvalid2(123123L)).isEqualTo(true);
43+
assertThat(GiftShop.isInvalid2(123123123L)).isEqualTo(true);
44+
assertThat(GiftShop.isInvalid2(1231231234L)).isEqualTo(false);
45+
assertThat(GiftShop.isInvalid2(1212121212L)).isEqualTo(true);
1646
}
1747
}

0 commit comments

Comments
 (0)