Skip to content

Commit

Permalink
[2023] Fix edge cases for day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarnung committed Dec 3, 2023
1 parent dc1d98b commit eaeabd2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public static String getPart1(List<String> lines) {
for (Part symbol : findSymbols(rows, i, j)) {
symbols.add(symbol);
}
} else {
}

if (!Character.isDigit(c) || j == row.size() - 1) {
if (!symbols.isEmpty()) {
total += Integer.parseInt(sb.toString());
}
Expand Down Expand Up @@ -74,7 +76,9 @@ public static String getPart2(List<String> lines) {
symbols.add(symbol);
}
}
} else {
}

if (!Character.isDigit(c) || j == row.size() - 1) {
for (Part symbol : symbols) {
symbolMap.computeIfAbsent(symbol, k -> new ArrayList<>()).add(Integer.parseInt(sb.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,45 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

class AppTest {
private final List<String> testInput = Arrays.asList(
"467..114..",
"...*......",
"..35..633.",
"......#...",
"617*......",
".....+.58.",
"..592.....",
"......755.",
"...$.*....",
".664.598..");

private final List<String> testInput2 = Arrays.asList(
"467..114..",
"...*......",
"..35...633",
"4.....#...",
"617*......",
".....+.58.",
"..592.....",
"......755.",
"...$..*...",
".664...598");

@Test
void testGetPart1() throws IOException {
List<String> lines = Arrays.asList(
"467..114..",
"...*......",
"..35..633.",
"......#...",
"617*......",
".....+.58.",
"..592.....",
"......755.",
"...$.*....",
".664.598..");
assertEquals("4361", App.getPart1(lines));

lines = readLinesFromResources(this, "input.txt");
assertEquals("512794", App.getPart1(lines));
assertEquals("4361", App.getPart1(testInput));
assertEquals("4361", App.getPart1(testInput2));

List<String> realInput = readLinesFromResources(this, "input.txt");
assertEquals("512794", App.getPart1(realInput));
}

@Test
void testGetPart2() throws IOException {
List<String> lines = Arrays.asList(
"467..114..",
"...*......",
"..35..633.",
"......#...",
"617*......",
".....+.58.",
"..592.....",
"......755.",
"...$.*....",
".664.598..");
assertEquals("467835", App.getPart2(lines));

lines = readLinesFromResources(this, "input.txt");
assertEquals("67779080", App.getPart2(lines));
assertEquals("467835", App.getPart2(testInput));
assertEquals("467835", App.getPart2(testInput2));

List<String> realInput = readLinesFromResources(this, "input.txt");
assertEquals("67779080", App.getPart2(realInput));
}
}

0 comments on commit eaeabd2

Please sign in to comment.