From 514b764e194eb29d2d35f319fb427efad89f2979 Mon Sep 17 00:00:00 2001 From: TheCK Date: Fri, 15 Dec 2023 10:51:27 +0100 Subject: [PATCH] AOC 2023 days 14 & 15 refactoring and cleanup --- .../org/ck/adventofcode/year2023/Day14.java | 66 +++++++++---------- .../org/ck/adventofcode/year2023/Day15.java | 21 ------ 2 files changed, 30 insertions(+), 57 deletions(-) diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day14.java b/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day14.java index c93e3bd4..84b130f4 100644 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day14.java +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day14.java @@ -93,33 +93,29 @@ private long calcLoad(final List dish) { private void tilt( final List dish, - final LoopDefinition outerDef, - final LoopDefinition innerDef, - final Mapper rowMapper, - final Mapper columnMapper, + final LoopDefinition rowDef, + final LoopDefinition columnDef, + final IntUnaryOperator getRowPeek, + final IntUnaryOperator getColumnPeek, final BiPredicate condition) { - for (int outer = outerDef.start(); - outerDef.continueLoop().test(outer); - outer = outerDef.step().applyAsInt(outer)) { - for (int inner = innerDef.start(); - innerDef.continueLoop().test(inner); - inner = innerDef.step().applyAsInt(inner)) { - final int row = rowMapper.get().applyAsInt(outer, inner); - final int column = columnMapper.get().applyAsInt(outer, inner); - + for (int row = rowDef.start(); + rowDef.continueLoop().test(row); + row = rowDef.step().applyAsInt(row)) { + for (int column = columnDef.start(); + columnDef.continueLoop().test(column); + column = columnDef.step().applyAsInt(column)) { if (dish.get(row)[column] == 'O') { int otherRow = row; int otherColumn = column; while (condition.test(otherRow, otherColumn) - && dish.get(rowMapper.getPeek().applyAsInt(otherRow))[ - columnMapper.getPeek().applyAsInt(otherColumn)] + && dish.get(getRowPeek.applyAsInt(otherRow))[getColumnPeek.applyAsInt(otherColumn)] == '.') { - otherRow = rowMapper.getPeek().applyAsInt(otherRow); - otherColumn = columnMapper.getPeek().applyAsInt(otherColumn); + otherRow = getRowPeek.applyAsInt(otherRow); + otherColumn = getColumnPeek.applyAsInt(otherColumn); } - if (row != otherRow || column != otherColumn) { + if (row + column != otherRow + otherColumn) { dish.get(otherRow)[otherColumn] = 'O'; dish.get(row)[column] = '.'; } @@ -131,44 +127,42 @@ private void tilt( private void tiltNorth(final List dish) { tilt( dish, - new LoopDefinition(0, outer -> outer < dish.size(), step -> step + 1), - new LoopDefinition(0, inner -> inner < dish.getFirst().length, step -> step + 1), - new Mapper((outer, inner) -> outer, row -> row - 1), - new Mapper((outer, inner) -> inner, column -> column), + new LoopDefinition(0, row -> row < dish.size(), step -> step + 1), + new LoopDefinition(0, column -> column < dish.getFirst().length, step -> step + 1), + row -> row - 1, + column -> column, (row, column) -> row > 0); } private void tiltSouth(final List dish) { tilt( dish, - new LoopDefinition(dish.size() - 1, outer -> outer >= 0, step -> step - 1), - new LoopDefinition(0, inner -> inner < dish.getFirst().length, step -> step + 1), - new Mapper((outer, inner) -> outer, row -> row + 1), - new Mapper((outer, inner) -> inner, column -> column), + new LoopDefinition(dish.size() - 1, row -> row >= 0, step -> step - 1), + new LoopDefinition(0, column -> column < dish.getFirst().length, step -> step + 1), + row -> row + 1, + column -> column, (row, column) -> row < dish.size() - 1); } private void tiltWest(final List dish) { tilt( dish, - new LoopDefinition(0, outer -> outer < dish.getFirst().length, step -> step + 1), - new LoopDefinition(0, inner -> inner < dish.size(), step -> step + 1), - new Mapper((outer, inner) -> inner, row -> row), - new Mapper((outer, inner) -> outer, column -> column - 1), + new LoopDefinition(0, row -> row < dish.size(), step -> step + 1), + new LoopDefinition(0, column -> column < dish.getFirst().length, step -> step + 1), + row -> row, + column -> column - 1, (row, column) -> column > 0); } private void tiltEast(final List dish) { tilt( dish, - new LoopDefinition(dish.getFirst().length - 1, outer -> outer >= 0, step -> step - 1), - new LoopDefinition(0, inner -> inner < dish.size(), step -> step + 1), - new Mapper((outer, inner) -> inner, row -> row), - new Mapper((outer, inner) -> outer, column -> column + 1), + new LoopDefinition(0, row -> row < dish.size(), step -> step + 1), + new LoopDefinition(dish.getFirst().length - 1, column -> column >= 0, step -> step - 1), + row -> row, + column -> column + 1, (row, column) -> column < dish.getFirst().length - 1); } private record LoopDefinition(int start, IntPredicate continueLoop, IntUnaryOperator step) {} - - private record Mapper(IntBinaryOperator get, IntUnaryOperator getPeek) {} } diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day15.java b/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day15.java index 607554b2..29b0879a 100644 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day15.java +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2023/Day15.java @@ -87,25 +87,4 @@ private static int calculateHash(final String step) { } return hash; } - - private record Lens(String name, int focalLength) { - @Override - public boolean equals(final Object o) { - if (this == o) { - return true; - } - - if (o == null || getClass() != o.getClass()) { - return false; - } - - final Lens lens = (Lens) o; - return Objects.equals(name, lens.name); - } - - @Override - public int hashCode() { - return Objects.hash(name); - } - } }