|
| 1 | +package com.adventofcode.flashk.day15; |
| 2 | + |
| 3 | +import com.adventofcode.flashk.common.Array2DUtil; |
| 4 | +import com.adventofcode.flashk.common.Vector2; |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Iterator; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +public class ScaledWarehouseWoes { |
| 13 | + |
| 14 | + private static final char ROBOT = '@'; |
| 15 | + private static final char EMPTY = '.'; |
| 16 | + private static final char WALL = '#'; |
| 17 | + private static final char BOX = 'O'; |
| 18 | + private static final char BOX_START = '['; |
| 19 | + private static final char BOX_END = ']'; |
| 20 | + |
| 21 | + private static final char UP = '^'; |
| 22 | + private static final char DOWN = 'v'; |
| 23 | + private static final char LEFT = '<'; |
| 24 | + private static final char RIGHT = '>'; |
| 25 | + |
| 26 | + |
| 27 | + private Map<Character, Vector2> directions = new HashMap<>(); |
| 28 | + |
| 29 | + private int rows = 0; |
| 30 | + private int cols = 0; |
| 31 | + private char[][] map; |
| 32 | + private final char[] movements; |
| 33 | + private Vector2 robotPos; |
| 34 | + |
| 35 | + public ScaledWarehouseWoes(List<String> inputs) { |
| 36 | + |
| 37 | + boolean isMap = true; |
| 38 | + int row = 0; |
| 39 | + |
| 40 | + // Map |
| 41 | + Iterator<String> inputIterator = inputs.iterator(); |
| 42 | + |
| 43 | + while(inputIterator.hasNext() && isMap) { |
| 44 | + String input = inputIterator.next(); |
| 45 | + if(input.equals(StringUtils.EMPTY)) { |
| 46 | + isMap = false; |
| 47 | + } else { |
| 48 | + addRow(input, row); |
| 49 | + row++; |
| 50 | + } |
| 51 | + inputIterator.remove(); |
| 52 | + } |
| 53 | + |
| 54 | + // movements |
| 55 | + |
| 56 | + movements = String.join(StringUtils.EMPTY, inputs).toCharArray(); |
| 57 | + |
| 58 | + // Robot directions |
| 59 | + directions.put(UP, Vector2.down()); |
| 60 | + directions.put(DOWN, Vector2.up()); |
| 61 | + directions.put(LEFT, Vector2.left()); |
| 62 | + directions.put(RIGHT, Vector2.right()); |
| 63 | + } |
| 64 | + |
| 65 | + private void addRow(String input, int row) { |
| 66 | + |
| 67 | + if(map == null) { |
| 68 | + rows = input.length(); |
| 69 | + cols = rows * 2; |
| 70 | + map = new char[rows][cols]; |
| 71 | + } |
| 72 | + |
| 73 | + char[] inputRow = input.toCharArray(); |
| 74 | + int col = 0; |
| 75 | + for(char tile : inputRow) { |
| 76 | + switch (tile) { |
| 77 | + case ROBOT -> { |
| 78 | + map[row][col++] = ROBOT; |
| 79 | + map[row][col++] = EMPTY; |
| 80 | + } |
| 81 | + case BOX -> { |
| 82 | + map[row][col++] = BOX_START; |
| 83 | + map[row][col++] = BOX_END; |
| 84 | + } |
| 85 | + default -> { |
| 86 | + map[row][col++] = tile; |
| 87 | + map[row][col++] = tile; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + String scaledInput = new String(map[row]); |
| 94 | + int robotCol = scaledInput.indexOf(ROBOT); |
| 95 | + if(robotCol != -1) { |
| 96 | + robotPos = new Vector2(robotCol, row); |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public long solveB() { |
| 102 | + int movementIndex = 0; |
| 103 | + Array2DUtil.paint(map); |
| 104 | + while(movementIndex < movements.length) { |
| 105 | + System.out.print("Move: "+ String.valueOf(movements[movementIndex])); |
| 106 | + move(movements[movementIndex]); |
| 107 | + Array2DUtil.paint(map); |
| 108 | + movementIndex++; |
| 109 | + } |
| 110 | + |
| 111 | + return sumGPS(); |
| 112 | + } |
| 113 | + |
| 114 | + private void move(char movement) { |
| 115 | + Vector2 direction = directions.get(movement); |
| 116 | + boolean canMove; |
| 117 | + if(movement == LEFT || movement == RIGHT) { |
| 118 | + canMove = moveH(direction, robotPos.getY(), robotPos.getX()); |
| 119 | + } else { |
| 120 | + canMove = moveV(direction, robotPos.getY(), robotPos.getX()); |
| 121 | + if(canMove) { |
| 122 | + commitMoveV(direction, robotPos.getY(), robotPos.getX()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Update old robot position and move it |
| 127 | + |
| 128 | + if(canMove) { |
| 129 | + map[robotPos.getY()][robotPos.getX()] = EMPTY; |
| 130 | + robotPos.transform(direction); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + private boolean moveH(Vector2 direction, int row, int col) { |
| 136 | + |
| 137 | + if(map[row][col] == EMPTY) { |
| 138 | + return true; |
| 139 | + } else if(map[row][col] == WALL) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + int nextRow = direction.getY()+row; |
| 144 | + int nextCol = direction.getX()+col; |
| 145 | + |
| 146 | + boolean canMove = moveH(direction, nextRow, nextCol); |
| 147 | + |
| 148 | + if(canMove) { |
| 149 | + map[nextRow][nextCol] = map[row][col]; |
| 150 | + } |
| 151 | + |
| 152 | + return canMove; |
| 153 | + } |
| 154 | + |
| 155 | + private boolean moveV(Vector2 direction, int row, int col) { |
| 156 | + if(map[row][col] == EMPTY) { |
| 157 | + return true; |
| 158 | + } else if(map[row][col] == WALL) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + int nextRow = direction.getY()+row; |
| 163 | + |
| 164 | + boolean canMove; |
| 165 | + if(map[row][col] == BOX_START) { |
| 166 | + canMove = moveV(direction, nextRow, col) && moveV(direction, nextRow,col+1); |
| 167 | + } else if(map[row][col] == BOX_END){ |
| 168 | + canMove = moveV(direction, nextRow, col) && moveV(direction, nextRow, col-1); |
| 169 | + } else { |
| 170 | + // ROBOT |
| 171 | + canMove = moveV(direction, nextRow, col); |
| 172 | + } |
| 173 | + |
| 174 | + return canMove; |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + private void commitMoveV(Vector2 direction, int row, int col) { |
| 179 | + // TODO Puede que esto no haga falta |
| 180 | + if(map[row][col] == EMPTY) { |
| 181 | + return; |
| 182 | + } else if(map[row][col] == WALL) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + int nextRow = direction.getY()+row; |
| 187 | + |
| 188 | + //boolean canMove; |
| 189 | + if(map[row][col] == BOX_START) { |
| 190 | + commitMoveV(direction, nextRow, col); |
| 191 | + commitMoveV(direction, nextRow,col+1); |
| 192 | + |
| 193 | + map[nextRow][col] = map[row][col]; |
| 194 | + map[nextRow][col+1] = map[row][col+1]; |
| 195 | + map[row][col] = EMPTY; |
| 196 | + map[row][col+1] = EMPTY; |
| 197 | + } else if(map[row][col] == BOX_END){ |
| 198 | + commitMoveV(direction, nextRow, col); |
| 199 | + commitMoveV(direction, nextRow, col-1); |
| 200 | + map[nextRow][col] = map[row][col]; |
| 201 | + map[nextRow][col-1] = map[row][col-1]; |
| 202 | + map[row][col] = EMPTY; |
| 203 | + map[row][col-1] = EMPTY; |
| 204 | + |
| 205 | + } else { |
| 206 | + // ROBOT |
| 207 | + commitMoveV(direction, nextRow, col); |
| 208 | + map[nextRow][col] = map[row][col]; |
| 209 | + map[row][col] = EMPTY; |
| 210 | + } |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + private long sumGPS() { |
| 215 | + long result = 0; |
| 216 | + |
| 217 | + for(int row = 0; row < rows; row++){ |
| 218 | + for(int col = 0; col < cols; col++) { |
| 219 | + if(map[row][col] == BOX_START) { |
| 220 | + result += (100L*row)+col; |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + return result; |
| 225 | + } |
| 226 | +} |
0 commit comments