Skip to content

Commit eaeabd2

Browse files
committed
[2023] Fix edge cases for day 3
1 parent dc1d98b commit eaeabd2

File tree

2 files changed

+40
-32
lines changed
  • 2023/day03/src
    • main/java/com/github/kfarnung/adventofcode/aoc2023/day03
    • test/java/com/github/kfarnung/adventofcode/aoc2023/day03

2 files changed

+40
-32
lines changed

2023/day03/src/main/java/com/github/kfarnung/adventofcode/aoc2023/day03/App.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public static String getPart1(List<String> lines) {
4141
for (Part symbol : findSymbols(rows, i, j)) {
4242
symbols.add(symbol);
4343
}
44-
} else {
44+
}
45+
46+
if (!Character.isDigit(c) || j == row.size() - 1) {
4547
if (!symbols.isEmpty()) {
4648
total += Integer.parseInt(sb.toString());
4749
}
@@ -74,7 +76,9 @@ public static String getPart2(List<String> lines) {
7476
symbols.add(symbol);
7577
}
7678
}
77-
} else {
79+
}
80+
81+
if (!Character.isDigit(c) || j == row.size() - 1) {
7882
for (Part symbol : symbols) {
7983
symbolMap.computeIfAbsent(symbol, k -> new ArrayList<>()).add(Integer.parseInt(sb.toString()));
8084
}

2023/day03/src/test/java/com/github/kfarnung/adventofcode/aoc2023/day03/AppTest.java

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,45 @@
1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414

1515
class AppTest {
16+
private final List<String> testInput = Arrays.asList(
17+
"467..114..",
18+
"...*......",
19+
"..35..633.",
20+
"......#...",
21+
"617*......",
22+
".....+.58.",
23+
"..592.....",
24+
"......755.",
25+
"...$.*....",
26+
".664.598..");
27+
28+
private final List<String> testInput2 = Arrays.asList(
29+
"467..114..",
30+
"...*......",
31+
"..35...633",
32+
"4.....#...",
33+
"617*......",
34+
".....+.58.",
35+
"..592.....",
36+
"......755.",
37+
"...$..*...",
38+
".664...598");
39+
1640
@Test
1741
void testGetPart1() throws IOException {
18-
List<String> lines = Arrays.asList(
19-
"467..114..",
20-
"...*......",
21-
"..35..633.",
22-
"......#...",
23-
"617*......",
24-
".....+.58.",
25-
"..592.....",
26-
"......755.",
27-
"...$.*....",
28-
".664.598..");
29-
assertEquals("4361", App.getPart1(lines));
30-
31-
lines = readLinesFromResources(this, "input.txt");
32-
assertEquals("512794", App.getPart1(lines));
42+
assertEquals("4361", App.getPart1(testInput));
43+
assertEquals("4361", App.getPart1(testInput2));
44+
45+
List<String> realInput = readLinesFromResources(this, "input.txt");
46+
assertEquals("512794", App.getPart1(realInput));
3347
}
3448

3549
@Test
3650
void testGetPart2() throws IOException {
37-
List<String> lines = Arrays.asList(
38-
"467..114..",
39-
"...*......",
40-
"..35..633.",
41-
"......#...",
42-
"617*......",
43-
".....+.58.",
44-
"..592.....",
45-
"......755.",
46-
"...$.*....",
47-
".664.598..");
48-
assertEquals("467835", App.getPart2(lines));
49-
50-
lines = readLinesFromResources(this, "input.txt");
51-
assertEquals("67779080", App.getPart2(lines));
51+
assertEquals("467835", App.getPart2(testInput));
52+
assertEquals("467835", App.getPart2(testInput2));
53+
54+
List<String> realInput = readLinesFromResources(this, "input.txt");
55+
assertEquals("67779080", App.getPart2(realInput));
5256
}
5357
}

0 commit comments

Comments
 (0)