diff options
author | Nat Lasseter <user@4574.co.uk> | 2023-12-16 18:35:50 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2023-12-16 18:35:50 +0000 |
commit | 1a18a179d4679b519b008132fc79a8ba743edca1 (patch) | |
tree | 88e19b2a0653bec19555ef134ffd610e448099d7 | |
parent | 392f5ed69934ea1985fb3f703cc3f9464df1cebc (diff) |
Day 16 part 1 but not stupid this time.
-rw-r--r-- | day16/day16.java | 73 |
1 files changed, 44 insertions, 29 deletions
diff --git a/day16/day16.java b/day16/day16.java index ce07fda..f03dcd6 100644 --- a/day16/day16.java +++ b/day16/day16.java @@ -23,12 +23,10 @@ public class day16 { LinkedList<Beam> beams = new LinkedList<>(); beams.add(new Beam(0, 0, Dir.Right, grid)); - int count = 500000; - while (beams.size() > 0 && count > 0) { + while (beams.size() > 0) { Beam beam = beams.remove(); LinkedList<Beam> splits = beam.stepThrough(); beams.addAll(splits); - count -= 1; } return Integer.toString(grid.visited()); @@ -56,15 +54,21 @@ public class day16 { private static class Cell { Tile tile; boolean visited; + boolean canBlackhole; public Cell(Tile tile) { this.tile = tile; this.visited = false; + this.canBlackhole = false; } public void visit() { this.visited = true; } + + public void doSplit() { + this.canBlackhole = true; + } } private static class Grid { @@ -145,6 +149,7 @@ public class day16 { public LinkedList<Beam> stepThrough() { LinkedList<Beam> splits = new LinkedList<>(); +steppingLoop: while (!this.escaped()) { Cell cell = this.grid.cellAt(this.row, this.col); cell.visit(); @@ -190,35 +195,45 @@ public class day16 { } break; case VSplitter: - switch (this.dir) { - case Right: - case Left: - splits.add(new Beam(this.row + 1, this.col, Dir.Down, this.grid)); - this.dir = Dir.Up; - this.row -= 1; - break; - case Up: - this.row -= 1; - break; - case Down: - this.row += 1; - break; + if (!cell.canBlackhole) { + switch (this.dir) { + case Right: + case Left: + cell.doSplit(); + splits.add(new Beam(this.row + 1, this.col, Dir.Down, this.grid)); + this.dir = Dir.Up; + this.row -= 1; + break; + case Up: + this.row -= 1; + break; + case Down: + this.row += 1; + break; + } + } else { + break steppingLoop; } break; case HSplitter: - switch (this.dir) { - case Right: - this.col += 1; - break; - case Left: - this.col -= 1; - break; - case Up: - case Down: - splits.add(new Beam(this.row, this.col + 1, Dir.Right, this.grid)); - this.dir = Dir.Left; - this.col -= 1; - break; + if (!cell.canBlackhole) { + switch (this.dir) { + case Right: + this.col += 1; + break; + case Left: + this.col -= 1; + break; + case Up: + case Down: + cell.doSplit(); + splits.add(new Beam(this.row, this.col + 1, Dir.Right, this.grid)); + this.dir = Dir.Left; + this.col -= 1; + break; + } + } else { + break steppingLoop; } break; default: |