From 8b837e0d7f9daa617db8ae4164ac772fb2d5a56f Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Sat, 16 Dec 2023 18:47:07 +0000 Subject: Day 16 part 2 --- day16/day16.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'day16') diff --git a/day16/day16.java b/day16/day16.java index f03dcd6..3070d4f 100644 --- a/day16/day16.java +++ b/day16/day16.java @@ -33,7 +33,49 @@ public class day16 { } public static String part2(RandomAccessFile input) throws IOException { - return "WIP"; + Grid grid = new Grid(); + + String line; + while ((line = input.readLine()) != null) { + grid.addRow(line); + } + + int maxV = 0, v; + for (int i = 0; i < grid.rows(); i++) { + v = testEntryPoint(i, 0, Dir.Right, grid); + if (v > maxV) { + maxV = v; + } + v = testEntryPoint(i, grid.cols() - 1, Dir.Left, grid); + if (v > maxV) { + maxV = v; + } + v = testEntryPoint(0, i, Dir.Down, grid); + if (v > maxV) { + maxV = v; + } + v = testEntryPoint(grid.rows() - 1, i, Dir.Up, grid); + if (v > maxV) { + maxV = v; + } + } + + return Integer.toString(maxV); + } + + private static int testEntryPoint(int row, int col, Dir dir, Grid grid) { + grid.reset(); + + LinkedList beams = new LinkedList<>(); + beams.add(new Beam(row, col, dir, grid)); + + while (beams.size() > 0) { + Beam beam = beams.remove(); + LinkedList splits = beam.stepThrough(); + beams.addAll(splits); + } + + return grid.visited(); } private static enum Tile { @@ -69,6 +111,11 @@ public class day16 { public void doSplit() { this.canBlackhole = true; } + + public void reset() { + this.visited = false; + this.canBlackhole = false; + } } private static class Grid { @@ -102,6 +149,14 @@ public class day16 { return v; } + public void reset() { + for (ArrayList row: this.grid) { + for (Cell col: row) { + col.reset(); + } + } + } + public void addRow(String line) { ArrayList row = new ArrayList<>(); for (char ch: line.toCharArray()) { -- cgit v1.2.1