summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2023-12-16 18:47:07 +0000
committerNat Lasseter <user@4574.co.uk>2023-12-16 18:47:18 +0000
commit8b837e0d7f9daa617db8ae4164ac772fb2d5a56f (patch)
tree14099428228934c285ce08f2cae776416193e7c7
parent1a18a179d4679b519b008132fc79a8ba743edca1 (diff)
Day 16 part 2HEADmain
-rw-r--r--day16/day16.java57
1 files changed, 56 insertions, 1 deletions
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<Beam> beams = new LinkedList<>();
+ beams.add(new Beam(row, col, dir, grid));
+
+ while (beams.size() > 0) {
+ Beam beam = beams.remove();
+ LinkedList<Beam> 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<Cell> row: this.grid) {
+ for (Cell col: row) {
+ col.reset();
+ }
+ }
+ }
+
public void addRow(String line) {
ArrayList<Cell> row = new ArrayList<>();
for (char ch: line.toCharArray()) {