From 93623e5a3c975b095448f33e1c76eea8584db564 Mon Sep 17 00:00:00 2001 From: pareronia <49491686+pareronia@users.noreply.github.com> Date: Sun, 15 Dec 2024 22:45:11 +0100 Subject: [PATCH] AoC 2024 Day 15 - java - refactor --- src/main/java/AoC2024_15.java | 24 +++++++++---------- .../com/github/pareronia/aoc/CharGrid.java | 7 ------ 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/main/java/AoC2024_15.java b/src/main/java/AoC2024_15.java index e1a5de7..b8749a7 100644 --- a/src/main/java/AoC2024_15.java +++ b/src/main/java/AoC2024_15.java @@ -43,12 +43,11 @@ public static AoC2024_15 createDebug() { @Override protected Input parseInput(final List inputs) { final List> blocks = StringOps.toBlocks(inputs); - final Grid grid = new Grid(CharGrid.from(blocks.get(0))); final List 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( @@ -192,7 +191,7 @@ private interface GetToMove { List getToMove(CharGrid grid, Cell robot, Direction dir); } - record Grid(CharGrid gridIn) { + record GridSupplier(List gridIn) { private static final Map SCALE_UP = Map.of( FLOOR, new char[] { FLOOR, FLOOR }, WALL, new char[] { WALL, WALL }, @@ -201,17 +200,18 @@ 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; } @@ -219,5 +219,5 @@ public CharGrid getWideGrid() { } } - record Input(Grid grid, List dirs) {} + record Input(GridSupplier grid, List dirs) {} } \ No newline at end of file diff --git a/src/main/java/com/github/pareronia/aoc/CharGrid.java b/src/main/java/com/github/pareronia/aoc/CharGrid.java index d5777cc..48ab625 100644 --- a/src/main/java/com/github/pareronia/aoc/CharGrid.java +++ b/src/main/java/com/github/pareronia/aoc/CharGrid.java @@ -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 list = new ArrayList<>(getRowsAsStringList());