summaryrefslogtreecommitdiff
path: root/day16/day16.java
blob: 3070d4f33956448fa7caffcc1c88294ee79ff0d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.LinkedList;

public class day16 {
  public static void main(String args[]) throws IOException {
    RandomAccessFile input = new RandomAccessFile("input", "r");
    System.out.println("Day 16 Part 1: " + part1(input));
    input.seek(0);
    System.out.println("Day 16 Part 2: " + part2(input));
    input.close();
  }

  public static String part1(RandomAccessFile input) throws IOException {
    Grid grid = new Grid();

    String line;
    while ((line = input.readLine()) != null) {
      grid.addRow(line);
    }

    LinkedList<Beam> beams = new LinkedList<>();
    beams.add(new Beam(0, 0, Dir.Right, grid));

    while (beams.size() > 0) {
      Beam beam = beams.remove();
      LinkedList<Beam> splits = beam.stepThrough();
      beams.addAll(splits);
    }

    return Integer.toString(grid.visited());
  }

  public static String part2(RandomAccessFile input) throws IOException {
    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 {
    Mirror,
    BackMirror,
    VSplitter,
    HSplitter,
    Empty
  }

  private static enum Dir {
    Right,
    Left,
    Up,
    Down
  }

  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;
    }

    public void reset() {
      this.visited = false;
      this.canBlackhole = false;
    }
  }

  private static class Grid {
    ArrayList<ArrayList<Cell>> grid;

    public Grid() {
      this.grid = new ArrayList<>();
    }

    public int rows() {
      return this.grid.size();
    }

    public int cols() {
      return this.grid.get(0).size();
    }

    public Cell cellAt(int row, int col) {
      return this.grid.get(row).get(col);
    }

    public int visited() {
      int v = 0;
      for (ArrayList<Cell> row: this.grid) {
        for (Cell col: row) {
          if (col.visited) {
            v += 1;
          }
        }
      }
      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()) {
        switch (ch) {
          case '/':
            row.add(new Cell(Tile.Mirror));
            break;
          case '\\':
            row.add(new Cell(Tile.BackMirror));
            break;
          case '|':
            row.add(new Cell(Tile.VSplitter));
            break;
          case '-':
            row.add(new Cell(Tile.HSplitter));
            break;
          case '.':
            row.add(new Cell(Tile.Empty));
            break;
        }
      }
      this.grid.add(row);
    }
  }

  private static class Beam {
    int row, col;
    Dir dir;
    Grid grid;

    public Beam(int row, int col, Dir dir, Grid grid) {
      this.row = row;
      this.col = col;
      this.dir = dir;
      this.grid = grid;
    }

    public boolean escaped() {
      return this.row < 0 ||
             this.row >= this.grid.rows() ||
             this.col < 0 ||
             this.col >= this.grid.cols();
    }

    public LinkedList<Beam> stepThrough() {
      LinkedList<Beam> splits = new LinkedList<>();

steppingLoop:
      while (!this.escaped()) {
        Cell cell = this.grid.cellAt(this.row, this.col);
        cell.visit();
        switch (cell.tile) {
          case Mirror:
            switch (this.dir) {
              case Right:
                this.dir = Dir.Up;
                this.row -= 1;
                break;
              case Left:
                this.dir = Dir.Down;
                this.row += 1;
                break;
              case Up:
                this.dir = Dir.Right;
                this.col += 1;
                break;
              case Down:
                this.dir = Dir.Left;
                this.col -= 1;
                break;
            }
            break;
          case BackMirror:
            switch (this.dir) {
              case Right:
                this.dir = Dir.Down;
                this.row += 1;
                break;
              case Left:
                this.dir = Dir.Up;
                this.row -= 1;
                break;
              case Up:
                this.dir = Dir.Left;
                this.col -= 1;
                break;
              case Down:
                this.dir = Dir.Right;
                this.col += 1;
                break;
            }
            break;
          case VSplitter:
            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:
            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:
            switch (this.dir) {
              case Right:
                this.col += 1;
                break;
              case Left:
                this.col -= 1;
                break;
              case Up:
                this.row -= 1;
                break;
              case Down:
                this.row += 1;
                break;
            }
        }
      }

      return splits;
    }
  }
}