|
| 1 | +package com.codefork.aoc2024.day15; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.stream.IntStream; |
| 8 | +import java.util.stream.Stream; |
| 9 | + |
| 10 | +public record Warehouse(Set<Position> walls, Set<Box> boxes, Position robot) { |
| 11 | + |
| 12 | + public Warehouse withWalls(Set<Position> newWalls) { |
| 13 | + return new Warehouse(newWalls, boxes, robot); |
| 14 | + } |
| 15 | + |
| 16 | + public Warehouse withBoxes(Set<Box> newBoxes) { |
| 17 | + return new Warehouse(walls, newBoxes, robot); |
| 18 | + } |
| 19 | + |
| 20 | + public Warehouse withRobot(Position newRobot) { |
| 21 | + return new Warehouse(walls, boxes, newRobot); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * return true if any of the positions are walls |
| 26 | + */ |
| 27 | + public boolean areWalls(List<Position> positions) { |
| 28 | + return positions.stream().anyMatch(walls::contains); |
| 29 | + } |
| 30 | + |
| 31 | + public Warehouse moveVertical(int dY) { |
| 32 | + var boxWidth = boxes.stream().findFirst().orElseThrow().width(); |
| 33 | + var foundEmpty = false; |
| 34 | + // order matters for boxesToMove, so it's a List |
| 35 | + var boxesToMove = new ArrayList<Box>(); |
| 36 | + var boxesToCheck = IntStream.range(0, boxWidth).boxed().map(i -> |
| 37 | + new Position(robot.x() - i, robot.y() + dY) |
| 38 | + ).toList(); |
| 39 | + var wallsToCheck = List.of(new Position(robot.x(), robot.y() + dY)); |
| 40 | + while (!areWalls(wallsToCheck)) { |
| 41 | + // check if boxesToCheck are boxes |
| 42 | + final var _toCheck = boxesToCheck; |
| 43 | + var foundBoxes = boxes.stream().filter(box -> _toCheck.contains(box.pos())).toList(); |
| 44 | + // no boxes found? we're done, we can move! |
| 45 | + if(foundBoxes.isEmpty()) { |
| 46 | + foundEmpty = true; |
| 47 | + break; |
| 48 | + } |
| 49 | + boxesToMove.addAll(foundBoxes); |
| 50 | + // set boxesToCheck for the boxes we just found |
| 51 | + boxesToCheck = foundBoxes.stream().flatMap(box -> { |
| 52 | + // TODO: I hard coded these possibilities because I'm tired, |
| 53 | + // but they should really be calculated based on boxWidth |
| 54 | + if(boxWidth == 1) { |
| 55 | + return Stream.of(box.pos().withChangedY(dY)); |
| 56 | + } else if(boxWidth == 2) { |
| 57 | + return Stream.of( |
| 58 | + box.pos().withChangedY(dY).withChangedX(-1), |
| 59 | + box.pos().withChangedY(dY), |
| 60 | + box.pos().withChangedY(dY).withChangedX(1) |
| 61 | + ); |
| 62 | + } |
| 63 | + throw new RuntimeException("can't handle boxWidth=" + boxWidth); |
| 64 | + }).toList(); |
| 65 | + wallsToCheck = foundBoxes.stream().flatMap(box -> { |
| 66 | + // TODO: I hard coded these possibilities because I'm tired, |
| 67 | + // but they should really be calculated based on boxWidth |
| 68 | + if(boxWidth == 1) { |
| 69 | + return Stream.of(box.pos().withChangedY(dY)); |
| 70 | + } else if(boxWidth == 2) { |
| 71 | + return Stream.of(box.pos().withChangedY(dY), box.pos().withChangedY(dY).withChangedX(1)); |
| 72 | + } |
| 73 | + throw new RuntimeException("can't handle boxWidth=" + boxWidth); |
| 74 | + }).toList(); |
| 75 | + } |
| 76 | + if (foundEmpty) { |
| 77 | + var newBoxes = new HashSet<>(boxes); |
| 78 | + for (var box : boxesToMove.reversed()) { |
| 79 | + newBoxes.remove(box); |
| 80 | + newBoxes.add(new Box(new Position(box.pos().x(), box.pos().y() + dY), boxWidth)); |
| 81 | + } |
| 82 | + return new Warehouse(walls, newBoxes, new Position(robot.x(), robot.y() + dY)); |
| 83 | + } else { |
| 84 | + return this; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public Warehouse moveHorizontal(int dX) { |
| 89 | + var boxWidth = boxes.stream().findFirst().orElseThrow().width(); |
| 90 | + var foundEmpty = false; |
| 91 | + // order matters for boxesToMove, so it's a List |
| 92 | + var boxesToMove = new ArrayList<Box>(); |
| 93 | + var boxToCheck = dX < 0 ? |
| 94 | + new Box(new Position(robot.x() + (dX * boxWidth), robot.y()), boxWidth) : |
| 95 | + new Box(new Position(robot.x() + dX, robot.y()), boxWidth); |
| 96 | + var wallsToCheck = List.of(new Position(robot.x() + dX, robot.y())); |
| 97 | + while (!areWalls(wallsToCheck)) { |
| 98 | + var isBox = boxes.contains(boxToCheck); |
| 99 | + // not a box? we found empty space, so we can move |
| 100 | + if(!isBox) { |
| 101 | + foundEmpty = true; |
| 102 | + break; |
| 103 | + } |
| 104 | + boxesToMove.add(boxToCheck); |
| 105 | + |
| 106 | + boxToCheck = new Box(new Position(boxToCheck.pos().x() + (dX * boxWidth), boxToCheck.pos().y()), boxWidth); |
| 107 | + // TODO: I hard coded these possibilities because I'm tired, |
| 108 | + // but they should really be calculated based on boxWidth |
| 109 | + if(boxWidth == 1) { |
| 110 | + wallsToCheck = List.of(boxToCheck.pos()); |
| 111 | + } else if (boxWidth == 2) { |
| 112 | + wallsToCheck = dX < 0 ? |
| 113 | + List.of(boxToCheck.pos().withChangedX(1)) : |
| 114 | + List.of(boxToCheck.pos()); |
| 115 | + } |
| 116 | + } |
| 117 | + if (foundEmpty) { |
| 118 | + var newBoxes = new HashSet<>(boxes); |
| 119 | + for (var box : boxesToMove.reversed()) { |
| 120 | + newBoxes.remove(box); |
| 121 | + var moved = new Box(new Position(box.pos().x() + dX, box.pos().y()), boxWidth); |
| 122 | + newBoxes.add(moved); |
| 123 | + } |
| 124 | + return new Warehouse(walls, newBoxes, new Position(robot.x() + dX, robot.y())); |
| 125 | + } else { |
| 126 | + return this; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * move the robot in the specified direction and return the new state of the warehouse |
| 132 | + * @param direction |
| 133 | + * @return |
| 134 | + */ |
| 135 | + public Warehouse move(Direction direction) { |
| 136 | + var newWarehouse = switch (direction) { |
| 137 | + case Direction.Up -> moveVertical(-1); |
| 138 | + case Direction.Down -> moveVertical(1); |
| 139 | + case Direction.Right -> moveHorizontal(1); |
| 140 | + case Direction.Left -> moveHorizontal(-1); |
| 141 | + }; |
| 142 | + //System.out.println("moved " + direction + ":"); |
| 143 | + //newWarehouse.print(); |
| 144 | + return newWarehouse; |
| 145 | + } |
| 146 | + |
| 147 | + public int getSumBoxCoordinates() { |
| 148 | + return boxes.stream() |
| 149 | + .map(box -> 100 * box.pos().y() + box.pos().x()) |
| 150 | + .mapToInt(i -> i) |
| 151 | + .sum(); |
| 152 | + } |
| 153 | + |
| 154 | + public void print() { |
| 155 | + int height = walls.stream().map(Position::y).max(Integer::compareTo).orElseThrow() + 1; |
| 156 | + int width = walls.stream().map(Position::x).max(Integer::compareTo).orElseThrow() + 1; |
| 157 | + |
| 158 | + for (var y = 0; y < height; y++) { |
| 159 | + for (var x = 0; x < width; x++) { |
| 160 | + var pos = new Position(x, y); |
| 161 | + if (walls.contains(pos)) { |
| 162 | + System.out.print("#"); |
| 163 | + } else if (boxes.contains(new Box(pos, 1))) { |
| 164 | + System.out.print("O"); |
| 165 | + } else if (boxes.contains(new Box(pos, 2))) { |
| 166 | + System.out.print("[]"); |
| 167 | + x += 1; |
| 168 | + } else if (pos.equals(robot)) { |
| 169 | + System.out.print("@"); |
| 170 | + } else { |
| 171 | + System.out.print("."); |
| 172 | + } |
| 173 | + } |
| 174 | + System.out.println(); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments