Skip to content

Commit

Permalink
AoC 2024 Day 15 - java - refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 15, 2024
1 parent 90b79e5 commit 93623e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/main/java/AoC2024_15.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ public static AoC2024_15 createDebug() {
@Override
protected Input parseInput(final List<String> inputs) {
final List<List<String>> blocks = StringOps.toBlocks(inputs);
final Grid grid = new Grid(CharGrid.from(blocks.get(0)));
final List<Direction> dirs = Utils.asCharacterStream(
blocks.get(1).stream().collect(joining()))
.map(Direction::fromChar)
.toList();
return new Input(grid, dirs);
return new Input(new GridSupplier(blocks.get(0)), dirs);
}

private int solve(
Expand Down Expand Up @@ -192,7 +191,7 @@ private interface GetToMove {
List<Cell> getToMove(CharGrid grid, Cell robot, Direction dir);
}

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

public CharGrid getGrid() {
return this.gridIn.copy();
return CharGrid.from(this.gridIn);
}

public CharGrid getWideGrid() {
final char[][] chars = new char[this.gridIn.getHeight()][];
for (final int r : range(this.gridIn.getHeight())) {
final char[] row = new char[2 * this.gridIn.getWidth()];
for(final int c : range(this.gridIn.getWidth())) {
final char ch = this.gridIn.getValue(Cell.at(r, c));
row[2 * c] = SCALE_UP.get(ch)[0];
row[2 * c + 1] = SCALE_UP.get(ch)[1];
final char[][] chars = new char[this.gridIn.size()][];
for (final int r : range(this.gridIn.size())) {
final String line = this.gridIn.get(r);
final char[] row = new char[2 * line.length()];
for(final int c : range(line.length())) {
final char[] s = SCALE_UP.get(line.charAt(c));
row[2 * c] = s[0];
row[2 * c + 1] = s[1];
}
chars[r] = row;
}
return new CharGrid(chars);
}
}

record Input(Grid grid, List<Direction> dirs) {}
record Input(GridSupplier grid, List<Direction> dirs) {}
}
7 changes: 0 additions & 7 deletions src/main/java/com/github/pareronia/aoc/CharGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ public CharGrid doClone() {
}
}

public CharGrid copy() {
final char[][] chars = Stream.of(this.cells)
.map(row -> Arrays.copyOf(row, row.length))
.toArray(char[][]::new);
return new CharGrid(chars);
}

public CharGrid addRow(final String string ) {
assertTrue(string.length() == getWidth(), () -> "Invalid row length.");
final List<String> list = new ArrayList<>(getRowsAsStringList());
Expand Down

0 comments on commit 93623e5

Please sign in to comment.