Skip to content

Commit 176a72d

Browse files
committed
Add Gift Shop solution for Advent of code 2025, Day 2
1 parent daa733b commit 176a72d

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package by.andd3dfx.game;
2+
3+
import lombok.SneakyThrows;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.apache.commons.lang3.math.NumberUtils;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.util.HashSet;
12+
13+
/**
14+
* <pre>
15+
* --- Day 2: Gift Shop ---
16+
*
17+
* You get inside and take the elevator to its only other stop: the gift shop. "Thank you for visiting the North Pole!" gleefully exclaims a nearby sign. You aren't sure who is even allowed to visit the North Pole, but you know you can access the lobby through here, and from there you can access the rest of the North Pole base.
18+
*
19+
* As you make your way through the surprisingly extensive selection, one of the clerks recognizes you and asks for your help.
20+
*
21+
* As it turns out, one of the younger Elves was playing on a gift shop computer and managed to add a whole bunch of invalid product IDs to their gift shop database! Surely, it would be no trouble for you to identify the invalid product IDs for them, right?
22+
*
23+
* They've even checked most of the product ID ranges already; they only have a few product ID ranges (your puzzle input) that you'll need to check. For example:
24+
*
25+
* 11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
26+
* 1698522-1698528,446443-446449,38593856-38593862,565653-565659,
27+
* 824824821-824824827,2121212118-2121212124
28+
*
29+
* (The ID ranges are wrapped here for legibility; in your input, they appear on a single long line.)
30+
*
31+
* The ranges are separated by commas (,); each range gives its first ID and last ID separated by a dash (-).
32+
*
33+
* Since the young Elf was just doing silly patterns, you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs.
34+
*
35+
* None of the numbers have leading zeroes; 0101 isn't an ID at all. (101 is a valid ID that you would ignore.)
36+
*
37+
* Your job is to find all of the invalid IDs that appear in the given ranges. In the above example:
38+
*
39+
* 11-22 has two invalid IDs, 11 and 22.
40+
* 95-115 has one invalid ID, 99.
41+
* 998-1012 has one invalid ID, 1010.
42+
* 1188511880-1188511890 has one invalid ID, 1188511885.
43+
* 222220-222224 has one invalid ID, 222222.
44+
* 1698522-1698528 contains no invalid IDs.
45+
* 446443-446449 has one invalid ID, 446446.
46+
* 38593856-38593862 has one invalid ID, 38593859.
47+
* The rest of the ranges contain no invalid IDs.
48+
*
49+
* Adding up all the invalid IDs in this example produces 1227775554.
50+
*
51+
* What do you get if you add up all of the invalid IDs?
52+
* </pre>
53+
*
54+
* @see <a href="https://youtu.be/qUsIi-084Xg">Video solution</a>
55+
*/
56+
public class GiftShop {
57+
58+
public static Long determine(String inputString) {
59+
var set = new HashSet<Long>();
60+
var ranges = inputString.split(",");
61+
for (var range : ranges) {
62+
var borders = range.split("-");
63+
var left = NumberUtils.toLong(borders[0]);
64+
var right = NumberUtils.toLong(borders[1]);
65+
66+
for (var current = left; current <= right; current++) {
67+
if (isInvalid(current)) {
68+
set.add(current);
69+
}
70+
}
71+
}
72+
var result = 0L;
73+
for (var item : set) {
74+
result += item;
75+
}
76+
return result;
77+
}
78+
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));
86+
}
87+
88+
@SneakyThrows
89+
public static void main(String[] args) {
90+
var inputString = read("/game/gift-shop.txt");
91+
var result = determine(inputString);
92+
System.out.println(result);
93+
// 262060856
94+
}
95+
96+
private static String read(String filePathName) throws IOException {
97+
InputStream inputStream = NotQuiteLisp.class.getResourceAsStream(filePathName);
98+
var resultStringBuilder = new StringBuilder();
99+
try (var br = new BufferedReader(new InputStreamReader(inputStream))) {
100+
String line;
101+
while ((line = br.readLine()) != null) {
102+
resultStringBuilder.append(line).append("\n");
103+
}
104+
}
105+
return resultStringBuilder.toString();
106+
}
107+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4487-9581,755745207-755766099,954895848-955063124,4358832-4497315,15-47,1-12,9198808-9258771,657981-762275,6256098346-6256303872,142-282,13092529-13179528,96201296-96341879,19767340-19916378,2809036-2830862,335850-499986,172437-315144,764434-793133,910543-1082670,2142179-2279203,6649545-6713098,6464587849-6464677024,858399-904491,1328-4021,72798-159206,89777719-90005812,91891792-91938279,314-963,48-130,527903-594370,24240-60212
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package by.andd3dfx.game;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class GiftShopTest {
8+
9+
@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);
16+
}
17+
}

0 commit comments

Comments
 (0)