split out helper method

This commit is contained in:
2025-12-04 22:23:26 -05:00
parent 9f85cc318e
commit 1aeeaa780e

View File

@@ -51,19 +51,11 @@ public class Day04 {
int x = Integer.parseInt(coords[0]); int x = Integer.parseInt(coords[0]);
int y = Integer.parseInt(coords[1]); int y = Integer.parseInt(coords[1]);
neighbors = 0; neighbors = 0;
List.of( generateNeighborKeys(x, y).forEach((coord) -> {
"%s-%s".formatted(x - 1, y), if (floor.getOrDefault(coord, false)) {
"%s-%s".formatted(x + 1, y), neighbors += 1;
"%s-%s".formatted(x, y - 1), }
"%s-%s".formatted(x, y + 1), });
"%s-%s".formatted(x + 1, y - 1),
"%s-%s".formatted(x - 1, y - 1),
"%s-%s".formatted(x + 1, y + 1),
"%s-%s".formatted(x - 1, y + 1)).forEach((coord) -> {
if (floor.getOrDefault(coord, false)) {
neighbors += 1;
}
});
if (neighbors < 4) { if (neighbors < 4) {
score += 1; score += 1;
} }
@@ -103,19 +95,11 @@ public class Day04 {
int x = Integer.parseInt(coords[0]); int x = Integer.parseInt(coords[0]);
int y = Integer.parseInt(coords[1]); int y = Integer.parseInt(coords[1]);
neighbors = 0; neighbors = 0;
List.of( generateNeighborKeys(x, y).forEach((coord) -> {
"%s-%s".formatted(x - 1, y), if (floor.getOrDefault(coord, false)) {
"%s-%s".formatted(x + 1, y), neighbors += 1;
"%s-%s".formatted(x, y - 1), }
"%s-%s".formatted(x, y + 1), });
"%s-%s".formatted(x + 1, y - 1),
"%s-%s".formatted(x - 1, y - 1),
"%s-%s".formatted(x + 1, y + 1),
"%s-%s".formatted(x - 1, y + 1)).forEach((coord) -> {
if (floor.getOrDefault(coord, false)) {
neighbors += 1;
}
});
if (neighbors < 4) { if (neighbors < 4) {
round += 1; round += 1;
newFloor.put(key, false); newFloor.put(key, false);
@@ -135,4 +119,16 @@ public class Day04 {
IO.println(e.getMessage()); IO.println(e.getMessage());
} }
} }
private static List<String> generateNeighborKeys(int x, int y) {
return List.of(
"%s-%s".formatted(x - 1, y),
"%s-%s".formatted(x + 1, y),
"%s-%s".formatted(x, y - 1),
"%s-%s".formatted(x, y + 1),
"%s-%s".formatted(x + 1, y - 1),
"%s-%s".formatted(x - 1, y - 1),
"%s-%s".formatted(x + 1, y + 1),
"%s-%s".formatted(x - 1, y + 1));
}
} }