|
2 | 2 |
|
3 | 3 | importcom.codefork.aoc2024.util.Maps; |
4 | 4 |
|
| 5 | +importjava.util.ArrayList; |
| 6 | +importjava.util.HashSet; |
5 | 7 | importjava.util.List; |
6 | 8 | importjava.util.Map; |
| 9 | +importjava.util.Set; |
7 | 10 | importjava.util.regex.Pattern; |
8 | 11 | importjava.util.stream.Collectors; |
9 | 12 | importjava.util.stream.IntStream; |
|
14 | 17 | publicrecordSwarm(List<Robot>robots,intwidth,intheight) { |
15 | 18 |
|
16 | 19 | publicrecordPosition(intx,inty) { |
17 | | - |
| 20 | +publicbooleanisAdjacent(Positionother) { |
| 21 | +varxDiff =Math.abs(other.x() -x); |
| 22 | +varyDiff =Math.abs(other.y() -y); |
| 23 | +returnxDiff +yDiff <=1; |
| 24 | + } |
18 | 25 | } |
19 | 26 |
|
20 | 27 | publicrecordVelocity(intdx,intdy) { |
@@ -116,58 +123,41 @@ public Map<Integer, Integer> getQuadrantCounts() { |
116 | 123 | )); |
117 | 124 | } |
118 | 125 |
|
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 | 126 | publicintgetSafetyFactor() { |
170 | 127 | returngetQuadrantCounts().entrySet().stream() |
171 | 128 | .reduce(1, (acc,entry) ->acc *entry.getValue(),Integer::sum); |
172 | 129 | } |
| 130 | + |
| 131 | +/** |
| 132 | + * Look for groups of adjacent robots. this returns a list of sets of positions, not |
| 133 | + * the Robots themselves, because we only care about positions. We actually only care about the |
| 134 | + * existence of clusters, not even the positions. |
| 135 | + */ |
| 136 | +publicList<Set<Position>>getAdjacentClusters() { |
| 137 | +varclusters =newArrayList<Set<Position>>(); |
| 138 | +for(varrobot :robots()) { |
| 139 | +varposition =robot.position(); |
| 140 | + |
| 141 | +varnewClusters =newArrayList<Set<Swarm.Position>>(); |
| 142 | + |
| 143 | +varmergedCluster =newHashSet<Position>(); |
| 144 | +for(varcluster :clusters) { |
| 145 | +varbelongs =cluster.stream().anyMatch(p ->p.isAdjacent(position)); |
| 146 | +if(belongs) { |
| 147 | +mergedCluster.addAll(cluster); |
| 148 | +mergedCluster.add(position); |
| 149 | + }else { |
| 150 | +newClusters.add(cluster); |
| 151 | + } |
| 152 | + } |
| 153 | +if(mergedCluster.isEmpty()) { |
| 154 | +mergedCluster.add(position); |
| 155 | + } |
| 156 | +newClusters.add(mergedCluster); |
| 157 | + |
| 158 | +clusters =newClusters; |
| 159 | + } |
| 160 | +returnclusters; |
| 161 | + } |
| 162 | + |
173 | 163 | } |