Skip to content

Commit 93623e5

Browse files
committed
AoC 2024 Day 15 - java - refactor
1 parent 90b79e5 commit 93623e5

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

src/main/java/AoC2024_15.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ public static AoC2024_15 createDebug() {
4343
@Override
4444
protected Input parseInput(final List<String> inputs) {
4545
final List<List<String>> blocks = StringOps.toBlocks(inputs);
46-
final Grid grid = new Grid(CharGrid.from(blocks.get(0)));
4746
final List<Direction> dirs = Utils.asCharacterStream(
4847
blocks.get(1).stream().collect(joining()))
4948
.map(Direction::fromChar)
5049
.toList();
51-
return new Input(grid, dirs);
50+
return new Input(new GridSupplier(blocks.get(0)), dirs);
5251
}
5352

5453
private int solve(
@@ -192,7 +191,7 @@ private interface GetToMove {
192191
List<Cell> getToMove(CharGrid grid, Cell robot, Direction dir);
193192
}
194193

195-
record Grid(CharGrid gridIn) {
194+
record GridSupplier(List<String> gridIn) {
196195
private static final Map<Character, char[]> SCALE_UP = Map.of(
197196
FLOOR, new char[] { FLOOR, FLOOR },
198197
WALL, new char[] { WALL, WALL },
@@ -201,23 +200,24 @@ record Grid(CharGrid gridIn) {
201200
);
202201

203202
public CharGrid getGrid() {
204-
return this.gridIn.copy();
203+
return CharGrid.from(this.gridIn);
205204
}
206205

207206
public CharGrid getWideGrid() {
208-
final char[][] chars = new char[this.gridIn.getHeight()][];
209-
for (final int r : range(this.gridIn.getHeight())) {
210-
final char[] row = new char[2 * this.gridIn.getWidth()];
211-
for(final int c : range(this.gridIn.getWidth())) {
212-
final char ch = this.gridIn.getValue(Cell.at(r, c));
213-
row[2 * c] = SCALE_UP.get(ch)[0];
214-
row[2 * c + 1] = SCALE_UP.get(ch)[1];
207+
final char[][] chars = new char[this.gridIn.size()][];
208+
for (final int r : range(this.gridIn.size())) {
209+
final String line = this.gridIn.get(r);
210+
final char[] row = new char[2 * line.length()];
211+
for(final int c : range(line.length())) {
212+
final char[] s = SCALE_UP.get(line.charAt(c));
213+
row[2 * c] = s[0];
214+
row[2 * c + 1] = s[1];
215215
}
216216
chars[r] = row;
217217
}
218218
return new CharGrid(chars);
219219
}
220220
}
221221

222-
record Input(Grid grid, List<Direction> dirs) {}
222+
record Input(GridSupplier grid, List<Direction> dirs) {}
223223
}

src/main/java/com/github/pareronia/aoc/CharGrid.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ public CharGrid doClone() {
5454
}
5555
}
5656

57-
public CharGrid copy() {
58-
final char[][] chars = Stream.of(this.cells)
59-
.map(row -> Arrays.copyOf(row, row.length))
60-
.toArray(char[][]::new);
61-
return new CharGrid(chars);
62-
}
63-
6457
public CharGrid addRow(final String string ) {
6558
assertTrue(string.length() == getWidth(), () -> "Invalid row length.");
6659
final List<String> list = new ArrayList<>(getRowsAsStringList());

0 commit comments

Comments
 (0)