From a09db7c49822168c90c5c48477e434f32d7bf9ba Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 12 Dec 2023 01:05:41 +0000 Subject: Day 11 part 2 --- day11/day11.java | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/day11/day11.java b/day11/day11.java index 6bf7c19..2fd9f8a 100644 --- a/day11/day11.java +++ b/day11/day11.java @@ -68,7 +68,43 @@ public class day11 { } public static String part2(RandomAccessFile input) throws IOException { - return "WIP"; + ArrayList galaxies = new ArrayList<>(); + + String line; + int h = 0; + int w = 0; + while ((line = input.readLine()) != null) { + w = 0; + for (char ch: line.toCharArray()) { + if (ch == '#') { + galaxies.add(new Integer[] {h, w}); + } + w += 1; + } + h += 1; + } + + for (int r = h - 1; r >= 0; r--) { + if (rowEmpty(galaxies, r)) { + moveDown(galaxies, r, 999999); + } + } + + for (int c = w - 1; c >= 0; c--) { + if (colEmpty(galaxies, c)) { + moveRight(galaxies, c, 999999); + } + } + + long sum = 0; + + for (int i = 0; i < galaxies.size() - 1; i++) { + for (int j = i + 1; j < galaxies.size(); j++) { + sum += longDist(galaxies.get(i), galaxies.get(j)); + } + } + + return Long.toString(sum); } static enum Pixel { @@ -88,4 +124,42 @@ public class day11 { private static int dist(Integer[] i, Integer[] j) { return Math.abs(i[0] - j[0]) + Math.abs(i[1] - j[1]); } + + private static long longDist(Integer[] i, Integer[] j) { + return Math.abs(i[0] - j[0]) + Math.abs(i[1] - j[1]); + } + + private static boolean rowEmpty(ArrayList galaxies, int row) { + for (Integer[] galaxy: galaxies) { + if (galaxy[0] == row) { + return false; + } + } + return true; + } + + private static void moveDown(ArrayList galaxies, int row, int dist) { + for (Integer[] galaxy: galaxies) { + if (galaxy[0] > row) { + galaxy[0] += dist; + } + } + } + + private static boolean colEmpty(ArrayList galaxies, int col) { + for (Integer[] galaxy: galaxies) { + if (galaxy[1] == col) { + return false; + } + } + return true; + } + + private static void moveRight(ArrayList galaxies, int col, int dist) { + for (Integer[] galaxy: galaxies) { + if (galaxy[1] > col) { + galaxy[1] += dist; + } + } + } } -- cgit v1.2.1