diff --git a/src/Day04.java b/src/Day04.java new file mode 100644 index 0000000..af80257 --- /dev/null +++ b/src/Day04.java @@ -0,0 +1,138 @@ +package src; + +import java.io.FileReader; +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +public class Day04 { + + private static int neighbors = 0; + private static int score = 0; + private static int round = 0; + private static Map floor = new java.util.HashMap<>(); + + public static void main(String[] args) { + Instant start = Instant.now(); + part1(); + Instant middle = Instant.now(); + part2(); + Instant end = Instant.now(); + IO.println("part 1: " + Duration.between(start, middle).toMillis() + " ms"); + IO.println("part 2: " + Duration.between(middle, end).toMillis() + " ms"); + } + + private static void part1() { + try (Scanner scanner = new Scanner(new FileReader("resources/inputs/04/input"))) { + floor = new java.util.HashMap<>(); + + int rowIndex = 0; + int size = 0; + score = 0; + while (scanner.hasNextLine()) { + String[] row = scanner.nextLine().split(""); + size = row.length; + for (int i = 0; i < size; i++) { + String key = "%s-%s".formatted(i, rowIndex); + if (row[i].equals("@")) { + floor.put(key, true); + } else { + floor.put(key, false); + } + + } + rowIndex++; + } + floor.forEach((key, value) -> { + if (value) { + String[] coords = key.split("-"); + int x = Integer.parseInt(coords[0]); + int y = Integer.parseInt(coords[1]); + neighbors = 0; + 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)).forEach((coord) -> { + if (floor.getOrDefault(coord, false)) { + neighbors += 1; + } + }); + if (neighbors < 4) { + score += 1; + } + } + }); + IO.println(score); + } catch (Exception e) { + IO.println(e.getMessage()); + } + } + + private static void part2() { + try (Scanner scanner = new Scanner(new FileReader("resources/inputs/04/input"))) { + floor = new java.util.HashMap<>(); + int rowIndex = 0; + int size = 0; + score = 0; + while (scanner.hasNextLine()) { + String[] row = scanner.nextLine().split(""); + size = row.length; + for (int i = 0; i < size; i++) { + String key = "%s-%s".formatted(i, rowIndex); + if (row[i].equals("@")) { + floor.put(key, true); + } else { + floor.put(key, false); + } + + } + rowIndex++; + } + while (true) { + Map newFloor = new java.util.HashMap<>(floor); + floor.forEach((key, value) -> { + if (value) { + String[] coords = key.split("-"); + int x = Integer.parseInt(coords[0]); + int y = Integer.parseInt(coords[1]); + neighbors = 0; + 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)).forEach((coord) -> { + if (floor.getOrDefault(coord, false)) { + neighbors += 1; + } + }); + if (neighbors < 4) { + round += 1; + newFloor.put(key, false); + } + } + }); + floor = newFloor; + if (round == 0) { + break; + } else { + score = score + round; + round = 0; + } + } + IO.println(score); + } catch (Exception e) { + IO.println(e.getMessage()); + } + } +}