|
| 1 | +packagecom.codefork.aoc2024.day15; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.HashSet; |
| 5 | +importjava.util.List; |
| 6 | +importjava.util.Set; |
| 7 | +importjava.util.stream.IntStream; |
| 8 | +importjava.util.stream.Stream; |
| 9 | + |
| 10 | +publicrecordWarehouse(Set<Position>walls,Set<Box>boxes,Positionrobot) { |
| 11 | + |
| 12 | +publicWarehousewithWalls(Set<Position>newWalls) { |
| 13 | +returnnewWarehouse(newWalls,boxes,robot); |
| 14 | + } |
| 15 | + |
| 16 | +publicWarehousewithBoxes(Set<Box>newBoxes) { |
| 17 | +returnnewWarehouse(walls,newBoxes,robot); |
| 18 | + } |
| 19 | + |
| 20 | +publicWarehousewithRobot(PositionnewRobot) { |
| 21 | +returnnewWarehouse(walls,boxes,newRobot); |
| 22 | + } |
| 23 | + |
| 24 | +/** |
| 25 | + * return true if any of the positions are walls |
| 26 | + */ |
| 27 | +publicbooleanareWalls(List<Position>positions) { |
| 28 | +returnpositions.stream().anyMatch(walls::contains); |
| 29 | + } |
| 30 | + |
| 31 | +publicWarehousemoveVertical(intdY) { |
| 32 | +varboxWidth =boxes.stream().findFirst().orElseThrow().width(); |
| 33 | +varfoundEmpty =false; |
| 34 | +// order matters for boxesToMove, so it's a List |
| 35 | +varboxesToMove =newArrayList<Box>(); |
| 36 | +varboxesToCheck =IntStream.range(0,boxWidth).boxed().map(i -> |
| 37 | +newPosition(robot.x() -i,robot.y() +dY) |
| 38 | + ).toList(); |
| 39 | +varwallsToCheck =List.of(newPosition(robot.x(),robot.y() +dY)); |
| 40 | +while (!areWalls(wallsToCheck)) { |
| 41 | +// check if boxesToCheck are boxes |
| 42 | +finalvar_toCheck =boxesToCheck; |
| 43 | +varfoundBoxes =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 | +returnStream.of(box.pos().withChangedY(dY)); |
| 56 | + }elseif(boxWidth ==2) { |
| 57 | +returnStream.of( |
| 58 | +box.pos().withChangedY(dY).withChangedX(-1), |
| 59 | +box.pos().withChangedY(dY), |
| 60 | +box.pos().withChangedY(dY).withChangedX(1) |
| 61 | + ); |
| 62 | + } |
| 63 | +thrownewRuntimeException("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 | +returnStream.of(box.pos().withChangedY(dY)); |
| 70 | + }elseif(boxWidth ==2) { |
| 71 | +returnStream.of(box.pos().withChangedY(dY),box.pos().withChangedY(dY).withChangedX(1)); |
| 72 | + } |
| 73 | +thrownewRuntimeException("can't handle boxWidth=" +boxWidth); |
| 74 | + }).toList(); |
| 75 | + } |
| 76 | +if (foundEmpty) { |
| 77 | +varnewBoxes =newHashSet<>(boxes); |
| 78 | +for (varbox :boxesToMove.reversed()) { |
| 79 | +newBoxes.remove(box); |
| 80 | +newBoxes.add(newBox(newPosition(box.pos().x(),box.pos().y() +dY),boxWidth)); |
| 81 | + } |
| 82 | +returnnewWarehouse(walls,newBoxes,newPosition(robot.x(),robot.y() +dY)); |
| 83 | + }else { |
| 84 | +returnthis; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +publicWarehousemoveHorizontal(intdX) { |
| 89 | +varboxWidth =boxes.stream().findFirst().orElseThrow().width(); |
| 90 | +varfoundEmpty =false; |
| 91 | +// order matters for boxesToMove, so it's a List |
| 92 | +varboxesToMove =newArrayList<Box>(); |
| 93 | +varboxToCheck =dX <0 ? |
| 94 | +newBox(newPosition(robot.x() + (dX *boxWidth),robot.y()),boxWidth) : |
| 95 | +newBox(newPosition(robot.x() +dX,robot.y()),boxWidth); |
| 96 | +varwallsToCheck =List.of(newPosition(robot.x() +dX,robot.y())); |
| 97 | +while (!areWalls(wallsToCheck)) { |
| 98 | +varisBox =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 =newBox(newPosition(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 | + }elseif (boxWidth ==2) { |
| 112 | +wallsToCheck =dX <0 ? |
| 113 | +List.of(boxToCheck.pos().withChangedX(1)) : |
| 114 | +List.of(boxToCheck.pos()); |
| 115 | + } |
| 116 | + } |
| 117 | +if (foundEmpty) { |
| 118 | +varnewBoxes =newHashSet<>(boxes); |
| 119 | +for (varbox :boxesToMove.reversed()) { |
| 120 | +newBoxes.remove(box); |
| 121 | +varmoved =newBox(newPosition(box.pos().x() +dX,box.pos().y()),boxWidth); |
| 122 | +newBoxes.add(moved); |
| 123 | + } |
| 124 | +returnnewWarehouse(walls,newBoxes,newPosition(robot.x() +dX,robot.y())); |
| 125 | + }else { |
| 126 | +returnthis; |
| 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 | +publicWarehousemove(Directiondirection) { |
| 136 | +varnewWarehouse =switch (direction) { |
| 137 | +caseDirection.Up ->moveVertical(-1); |
| 138 | +caseDirection.Down ->moveVertical(1); |
| 139 | +caseDirection.Right ->moveHorizontal(1); |
| 140 | +caseDirection.Left ->moveHorizontal(-1); |
| 141 | + }; |
| 142 | +//System.out.println("moved " + direction + ":"); |
| 143 | +//newWarehouse.print(); |
| 144 | +returnnewWarehouse; |
| 145 | + } |
| 146 | + |
| 147 | +publicintgetSumBoxCoordinates() { |
| 148 | +returnboxes.stream() |
| 149 | + .map(box ->100 *box.pos().y() +box.pos().x()) |
| 150 | + .mapToInt(i ->i) |
| 151 | + .sum(); |
| 152 | + } |
| 153 | + |
| 154 | +publicvoidprint() { |
| 155 | +intheight =walls.stream().map(Position::y).max(Integer::compareTo).orElseThrow() +1; |
| 156 | +intwidth =walls.stream().map(Position::x).max(Integer::compareTo).orElseThrow() +1; |
| 157 | + |
| 158 | +for (vary =0;y <height;y++) { |
| 159 | +for (varx =0;x <width;x++) { |
| 160 | +varpos =newPosition(x,y); |
| 161 | +if (walls.contains(pos)) { |
| 162 | +System.out.print("#"); |
| 163 | + }elseif (boxes.contains(newBox(pos,1))) { |
| 164 | +System.out.print("O"); |
| 165 | + }elseif (boxes.contains(newBox(pos,2))) { |
| 166 | +System.out.print("[]"); |
| 167 | +x +=1; |
| 168 | + }elseif (pos.equals(robot)) { |
| 169 | +System.out.print("@"); |
| 170 | + }else { |
| 171 | +System.out.print("."); |
| 172 | + } |
| 173 | + } |
| 174 | +System.out.println(); |
| 175 | + } |
| 176 | + } |
| 177 | +} |