Compare commits
2 Commits
9f85cc318e
...
b13e847401
| Author | SHA1 | Date | |
|---|---|---|---|
| b13e847401 | |||
| 1aeeaa780e |
@@ -9,10 +9,9 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class Day04 {
|
public class Day04 {
|
||||||
|
|
||||||
private static int neighbors = 0;
|
|
||||||
private static int score = 0;
|
private static int score = 0;
|
||||||
private static int round = 0;
|
private static int round = 0;
|
||||||
private static Map<String, Boolean> floor = new java.util.HashMap<>();
|
private static Map<String, Integer> floor = new java.util.HashMap<>();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
@@ -37,33 +36,20 @@ public class Day04 {
|
|||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
String key = "%s-%s".formatted(i, rowIndex);
|
String key = "%s-%s".formatted(i, rowIndex);
|
||||||
if (row[i].equals("@")) {
|
if (row[i].equals("@")) {
|
||||||
floor.put(key, true);
|
floor.put(key, 1);
|
||||||
} else {
|
|
||||||
floor.put(key, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
rowIndex++;
|
rowIndex++;
|
||||||
}
|
}
|
||||||
floor.forEach((key, value) -> {
|
floor.forEach((key, value) -> {
|
||||||
if (value) {
|
if (value == 1) {
|
||||||
String[] coords = key.split("-");
|
String[] coords = key.split("-");
|
||||||
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;
|
int neighbors = generateNeighborKeys(x, y).stream().reduce(
|
||||||
List.of(
|
0,
|
||||||
"%s-%s".formatted(x - 1, y),
|
(t, coord) -> (t + floor.getOrDefault(coord, 0)),
|
||||||
"%s-%s".formatted(x + 1, y),
|
Integer::sum);
|
||||||
"%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;
|
||||||
}
|
}
|
||||||
@@ -87,38 +73,25 @@ public class Day04 {
|
|||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
String key = "%s-%s".formatted(i, rowIndex);
|
String key = "%s-%s".formatted(i, rowIndex);
|
||||||
if (row[i].equals("@")) {
|
if (row[i].equals("@")) {
|
||||||
floor.put(key, true);
|
floor.put(key, 1);
|
||||||
} else {
|
|
||||||
floor.put(key, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
rowIndex++;
|
rowIndex++;
|
||||||
}
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
Map<String, Boolean> newFloor = new java.util.HashMap<>(floor);
|
Map<String, Integer> newFloor = new java.util.HashMap<>(floor);
|
||||||
floor.forEach((key, value) -> {
|
floor.forEach((key, value) -> {
|
||||||
if (value) {
|
if (value == 1) {
|
||||||
String[] coords = key.split("-");
|
String[] coords = key.split("-");
|
||||||
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;
|
int neighbors = generateNeighborKeys(x, y).stream().reduce(
|
||||||
List.of(
|
0,
|
||||||
"%s-%s".formatted(x - 1, y),
|
(t, coord) -> (t + floor.getOrDefault(coord, 0)),
|
||||||
"%s-%s".formatted(x + 1, y),
|
Integer::sum);
|
||||||
"%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, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -135,4 +108,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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user