|
| 1 | +packagecom.codefork.aoc2024.day14; |
| 2 | + |
| 3 | +importcom.codefork.aoc2024.util.Maps; |
| 4 | + |
| 5 | +importjava.util.List; |
| 6 | +importjava.util.Map; |
| 7 | +importjava.util.regex.Pattern; |
| 8 | +importjava.util.stream.Collectors; |
| 9 | +importjava.util.stream.IntStream; |
| 10 | +importjava.util.stream.Stream; |
| 11 | + |
| 12 | +importstaticcom.codefork.aoc2024.util.FoldLeft.foldLeft; |
| 13 | + |
| 14 | +publicrecordSwarm(List<Robot>robots,intwidth,intheight) { |
| 15 | + |
| 16 | +publicrecordPosition(intx,inty) { |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | +publicrecordVelocity(intdx,intdy) { |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | +publicrecordRobot(Positionposition,Velocityv) { |
| 25 | + } |
| 26 | + |
| 27 | +privatefinalstaticPatternpat =Pattern.compile("p=([0-9]+),([0-9]+) v=([0-9\\-]+),([0-9\\-]+)"); |
| 28 | + |
| 29 | +publicstaticSwarmcreate(Stream<String>data,intwidth,intheight) { |
| 30 | +varrobots =data |
| 31 | + .map(line -> { |
| 32 | +varmatcher =pat.matcher(line); |
| 33 | +if (!matcher.find()) { |
| 34 | +thrownewRuntimeException("couldn't parse line"); |
| 35 | + } |
| 36 | +varparts =IntStream.range(1,5) |
| 37 | + .mapToObj(matcher::group) |
| 38 | + .map(Integer::valueOf) |
| 39 | + .toList(); |
| 40 | +returnnewRobot(newPosition(parts.get(0),parts.get(1)),newVelocity(parts.get(2),parts.get(3))); |
| 41 | + }) |
| 42 | + .toList(); |
| 43 | +returnnewSwarm(robots,width,height); |
| 44 | + } |
| 45 | + |
| 46 | +publicSwarmdoMoves(intn) { |
| 47 | +varfinalRobots =IntStream.range(0,n) |
| 48 | + .boxed() |
| 49 | + .collect(foldLeft( |
| 50 | + () ->robots, |
| 51 | + (acc,i) -> { |
| 52 | +returnacc.stream().map(this::move).toList(); |
| 53 | + } |
| 54 | + )); |
| 55 | +returnnewSwarm(finalRobots,width,height); |
| 56 | + } |
| 57 | + |
| 58 | +publicRobotmove(Robotrobot) { |
| 59 | +varchangedX =robot.position().x() +robot.v().dx(); |
| 60 | +varnewX =changedX >=width ?changedX -width : (changedX <0 ?changedX +width :changedX); |
| 61 | +varchangedY = (robot.position().y() +robot.v().dy()) %height; |
| 62 | +varnewY =changedY >=height ?changedY -height : (changedY <0 ?changedY +height :changedY); |
| 63 | +returnnewRobot(newPosition(newX,newY),robot.v()); |
| 64 | + } |
| 65 | + |
| 66 | +publicvoidprint() { |
| 67 | +varbyPos =robots().stream().collect(Collectors.toMap( |
| 68 | +Robot::position, |
| 69 | +List::of, |
| 70 | +Maps::listConcat)); |
| 71 | +for (vary =0;y <height;y++) { |
| 72 | +for (varx =0;x <width;x++) { |
| 73 | +varlist =byPos.getOrDefault(newPosition(x,y),List.of()); |
| 74 | +varcount =list.size(); |
| 75 | +varch =count >0 ?String.valueOf(count) :"."; |
| 76 | +System.out.print(ch); |
| 77 | + } |
| 78 | +System.out.println(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +privatestaticintDISCARD = -1; |
| 83 | + |
| 84 | +/** |
| 85 | + * quadrants are numbered in order from left to right, then top to bottom, like reading English |
| 86 | + * @return |
| 87 | + */ |
| 88 | +publicMap<Integer,Integer>getQuadrantCounts() { |
| 89 | +varbyQuadrant =robots.stream() |
| 90 | + .collect(Collectors.groupingBy( |
| 91 | + (robot) -> { |
| 92 | +if (robot.position().y() <height /2) { |
| 93 | +if (robot.position().x() <width /2) { |
| 94 | +return0; |
| 95 | + }elseif (robot.position().x() >width /2) { |
| 96 | +return1; |
| 97 | + } |
| 98 | + }elseif (robot.position().y() >height /2) { |
| 99 | +if (robot.position().x() <width /2) { |
| 100 | +return2; |
| 101 | + }elseif (robot.position().x() >width /2) { |
| 102 | +return3; |
| 103 | + } |
| 104 | + } |
| 105 | +// put the robots on the center lines in a separate group |
| 106 | +// that we'll discard |
| 107 | +returnDISCARD; |
| 108 | + }) |
| 109 | + ); |
| 110 | +returnbyQuadrant.entrySet().stream() |
| 111 | + .filter(entry ->entry.getKey() !=DISCARD) |
| 112 | + .collect(Collectors.toMap( |
| 113 | +Map.Entry::getKey, |
| 114 | +entry ->entry.getValue().size(), |
| 115 | +Integer::sum |
| 116 | + )); |
| 117 | + } |
| 118 | + |
| 119 | +/** |
| 120 | + * testing for part 2: try smaller sections |
| 121 | + */ |
| 122 | +publicMap<Integer,Integer>getCustomSectionCounts() { |
| 123 | +varbyQuadrant =robots.stream() |
| 124 | + .collect(Collectors.groupingBy( |
| 125 | + (robot) -> { |
| 126 | +if (robot.position().y() <height /3) { |
| 127 | +if (robot.position().x() <width /2) { |
| 128 | +return0; |
| 129 | + }elseif (robot.position().x() >width /2) { |
| 130 | +return1; |
| 131 | + } |
| 132 | + }elseif (robot.position().y() >height /3 &&robot.position().y() < (height /3) *2) { |
| 133 | +if (robot.position().x() <width /2) { |
| 134 | +return2; |
| 135 | + }elseif (robot.position().x() >width /2) { |
| 136 | +return3; |
| 137 | + } |
| 138 | + }elseif (robot.position().y() > (height /3) *2) { |
| 139 | +if (robot.position().x() <width /2) { |
| 140 | +return4; |
| 141 | + }elseif (robot.position().x() >width /2) { |
| 142 | +return5; |
| 143 | + } |
| 144 | + } |
| 145 | +// put the robots on the center lines in a separate group |
| 146 | +// that we'll discard |
| 147 | +returnDISCARD; |
| 148 | + }) |
| 149 | + ); |
| 150 | +returnbyQuadrant.entrySet().stream() |
| 151 | + .filter(entry ->entry.getKey() !=DISCARD) |
| 152 | + .collect(Collectors.toMap( |
| 153 | +Map.Entry::getKey, |
| 154 | +entry -> { |
| 155 | +// count occupied positions rather than robots, since they can be stacked, and we only |
| 156 | +// care about what they look like from above |
| 157 | +recordPos(intx,inty) { |
| 158 | + } |
| 159 | +varlist =entry.getValue(); |
| 160 | +varoccupiedPositions =list.stream() |
| 161 | + .map(robot ->newPos(robot.position().x(),robot.position().y())) |
| 162 | + .collect(Collectors.toSet()); |
| 163 | +returnoccupiedPositions.size(); |
| 164 | + }, |
| 165 | +Integer::sum |
| 166 | + )); |
| 167 | + } |
| 168 | + |
| 169 | +publicintgetSafetyFactor() { |
| 170 | +returngetQuadrantCounts().entrySet().stream() |
| 171 | + .reduce(1, (acc,entry) ->acc *entry.getValue(),Integer::sum); |
| 172 | + } |
| 173 | +} |