From 1a18a179d4679b519b008132fc79a8ba743edca1 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Sat, 16 Dec 2023 18:35:50 +0000 Subject: Day 16 part 1 but not stupid this time. --- day16/day16.java | 73 ++++++++++++++++++++++++++++++++++---------------------- 1 file 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 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 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 stepThrough() { LinkedList 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: -- cgit v1.2.1