Skip to content

Commit a09ead5

Browse files
committed
java - delombok
1 parent 5a060a9 commit a09ead5

34 files changed

+798
-736
lines changed

src/main/java/AoC2015_22.java

Lines changed: 135 additions & 115 deletions
Large diffs are not rendered by default.

src/main/java/AoC2016_11.java

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,14 @@
1212
import java.util.List;
1313
import java.util.Map;
1414
import java.util.Map.Entry;
15+
import java.util.Objects;
1516
import java.util.PriorityQueue;
1617
import java.util.Set;
1718

1819
import com.github.pareronia.aoc.StringUtils;
1920
import com.github.pareronia.aocd.Aocd;
2021
import com.github.pareronia.aocd.Puzzle;
2122

22-
import lombok.AccessLevel;
23-
import lombok.EqualsAndHashCode;
24-
import lombok.Getter;
25-
import lombok.RequiredArgsConstructor;
26-
import lombok.ToString;
27-
import lombok.Value;
28-
import lombok.With;
29-
3023
public final class AoC2016_11 extends AoCBase {
3124

3225
private final transient State initialState;
@@ -42,7 +35,7 @@ private State parse(final List<String> inputs) {
4235
for (int i = 0; i < inputs.size(); i++) {
4336
String floor = inputs.get(i);
4437
floor = floor.replaceAll(",? and", ",");
45-
floor = floor.replaceAll("\\.", "");
38+
floor = floor.replace(".", "");
4639
final String contains = floor.split(" contains ")[1];
4740
final String[] contained = contains.split(", ");
4841
for (final String containee : contained) {
@@ -135,32 +128,17 @@ public static void main(final String[] args) throws Exception {
135128
"The fourth floor contains nothing relevant."
136129
);
137130

138-
@Value
139131
static final class State {
140132
private static final List<Integer> FLOORS = List.of(1, 2, 3, 4);
141133
private static final int TOP = FLOORS.stream().mapToInt(Integer::intValue).max().getAsInt();
142134
private static final int BOTTOM = FLOORS.stream().mapToInt(Integer::intValue).min().getAsInt();
143135
private static final int MAX_ITEMS_PER_MOVE = 2;
144136

145-
@Getter(AccessLevel.PRIVATE)
146-
@With(AccessLevel.PRIVATE)
147137
private final Integer elevator;
148-
@Getter(AccessLevel.PRIVATE)
149-
@With(AccessLevel.PRIVATE)
150138
private final Map<String, Integer> chips;
151-
@Getter(AccessLevel.PRIVATE)
152-
@With(AccessLevel.PRIVATE)
153139
private final Map<String, Integer> gennys;
154-
@With(AccessLevel.PRIVATE)
155-
@EqualsAndHashCode.Exclude
156140
private final Integer diff;
157-
@Getter(AccessLevel.PRIVATE)
158-
@ToString.Exclude
159-
@EqualsAndHashCode.Exclude
160141
private final Map<Integer, List<String>> chipsPerFloor;
161-
@Getter(AccessLevel.PRIVATE)
162-
@ToString.Exclude
163-
@EqualsAndHashCode.Exclude
164142
private final Map<Integer, List<String>> gennysPerFloor;
165143

166144
private State(
@@ -199,7 +177,18 @@ public State(
199177
this(elevator, chips, gennys, 0);
200178
}
201179

202-
@ToString.Include
180+
public Map<String, Integer> getChips() {
181+
return chips;
182+
}
183+
184+
public Map<String, Integer> getGennys() {
185+
return gennys;
186+
}
187+
188+
public Integer getDiff() {
189+
return diff;
190+
}
191+
203192
boolean isSafe() {
204193
for (final Entry<String, Integer> chip : this.chips.entrySet()) {
205194
final List<String> gennysOnSameFloor
@@ -331,6 +320,22 @@ private List<State> moveChipAndGennyPairs(
331320
return states;
332321
}
333322

323+
private State withElevator(final int elevator) {
324+
return new State(elevator, this.chips, this.gennys, this.diff);
325+
}
326+
327+
private State withDiff(final int diff) {
328+
return new State(this.elevator, this.chips, this.gennys, diff);
329+
}
330+
331+
private State withChips(final Map<String, Integer> chips) {
332+
return new State(this.elevator, chips, this.gennys, this.diff);
333+
}
334+
335+
private State withGennys(final Map<String, Integer> gennys) {
336+
return new State(this.elevator, this.chips, gennys, this.diff);
337+
}
338+
334339
private State moveUpWithChips(final List<String> chips) {
335340
return withChipsTo(chips, this.elevator + 1)
336341
.withElevator(this.elevator + 1)
@@ -378,12 +383,44 @@ private boolean floorsBelowEmpty(final Integer floor) {
378383
}
379384
return true;
380385
}
386+
387+
@Override
388+
public boolean equals(final Object obj) {
389+
if (this == obj) {
390+
return true;
391+
}
392+
if (obj == null) {
393+
return false;
394+
}
395+
if (getClass() != obj.getClass()) {
396+
return false;
397+
}
398+
final State other = (State) obj;
399+
return Objects.equals(elevator, other.elevator)
400+
&& Objects.equals(chips, other.chips)
401+
&& Objects.equals(gennys, other.gennys);
402+
}
403+
404+
@Override
405+
public int hashCode() {
406+
return Objects.hash(chips, elevator, gennys);
407+
}
408+
409+
@Override
410+
public String toString() {
411+
final StringBuilder builder = new StringBuilder();
412+
builder.append("State [elevator=").append(elevator).append(", chips=").append(chips).append(", gennys=")
413+
.append(gennys).append(", diff=").append(diff)
414+
.append(", isSafe=").append(isSafe()).append("]");
415+
return builder.toString();
416+
}
381417
}
382418

383-
@RequiredArgsConstructor(staticName = "of")
384-
private static final class Step {
385-
private final int numberOfSteps;
386-
private final State state;
419+
record Step(int numberOfSteps, State state) {;
420+
421+
public static Step of(final int numberOfSteps, final State state) {
422+
return new Step(numberOfSteps, state);
423+
}
387424

388425
public int score() {
389426
return -state.getDiff() * numberOfSteps;

src/main/java/AoC2016_22.java

Lines changed: 39 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
import com.github.pareronia.aocd.Aocd;
2323
import com.github.pareronia.aocd.Puzzle;
2424

25-
import lombok.AllArgsConstructor;
26-
import lombok.EqualsAndHashCode;
27-
import lombok.Getter;
28-
import lombok.RequiredArgsConstructor;
29-
import lombok.ToString;
30-
3125
public class AoC2016_22 extends AoCBase {
3226

3327
private static final Pattern REGEX = Pattern.compile(
@@ -61,42 +55,42 @@ public static AoC2016_22 createDebug(final List<String> input) {
6155

6256
private Set<Node> getUnusableNodes() {
6357
final Integer maxAvailable = this.nodes.stream()
64-
.max(comparing(Node::getAvailable))
65-
.map(Node::getAvailable).orElseThrow();
58+
.max(comparing(Node::available))
59+
.map(Node::available).orElseThrow();
6660
return this.nodes.stream()
67-
.filter(n -> n.getUsed() > maxAvailable)
61+
.filter(n -> n.used() > maxAvailable)
6862
.collect(toSet());
6963
}
7064

7165
private Node getEmptyNode() {
7266
final List<Node> emptyNodes = nodes.stream()
73-
.filter(n -> n.getUsed() == 0)
67+
.filter(n -> n.used() == 0)
7468
.collect(toList());
7569
assertTrue(emptyNodes.size() == 1, () -> "Expected 1 empty node");
7670
return emptyNodes.get(0);
7771
}
7872

7973
private Integer getMaxX() {
8074
return this.nodes.stream()
81-
.max(comparing(Node::getX))
82-
.map(Node::getX).orElseThrow();
75+
.max(comparing(Node::x))
76+
.map(Node::x).orElseThrow();
8377
}
8478

8579
private Integer getMaxY() {
8680
return this.nodes.stream()
87-
.max(comparing(Node::getY))
88-
.map(Node::getY).orElseThrow();
81+
.max(comparing(Node::y))
82+
.map(Node::y).orElseThrow();
8983
}
9084

9185
private Node getGoalNode() {
9286
return nodes.stream()
93-
.filter(n -> n.getX() == getMaxX() && n.getY() == 0)
87+
.filter(n -> n.x() == getMaxX() && n.y() == 0)
9488
.findFirst().orElseThrow();
9589
}
9690

9791
private Node getAccessibleNode() {
9892
return nodes.stream()
99-
.filter(n -> n.getX() == 0 && n.getY() == 0)
93+
.filter(n -> n.x() == 0 && n.y() == 0)
10094
.findFirst().orElseThrow();
10195
}
10296

@@ -106,20 +100,20 @@ public Integer solvePart1() {
106100
.filter(Node::isNotEmpty)
107101
.flatMap(a -> this.nodes.stream()
108102
.filter(b -> !a.equals(b))
109-
.filter(b -> a.getUsed() <= b.getAvailable()))
103+
.filter(b -> a.used() <= b.available()))
110104
.count();
111105
}
112106

113107
private void visualize() {
114108
final Integer maxX = getMaxX();
115109
final Integer maxY = getMaxY();
116110
final List<Node> sorted = this.nodes.stream()
117-
.sorted(comparing(n -> n.getX() * maxY + n.getY()))
111+
.sorted(comparing(n -> n.x() * maxY + n.y()))
118112
.collect(toList());
119113
final List<List<Node>> grid = Stream.iterate(0, i -> i <= maxX, i -> i + 1)
120114
.map(i -> sorted.stream()
121115
.skip(i * (maxY + 1))
122-
.takeWhile(n -> n.getX() == i)
116+
.takeWhile(n -> n.x() == i)
123117
.collect(toList()))
124118
.collect(toList());
125119
final Set<Node> unusableNodes = getUnusableNodes();
@@ -147,7 +141,7 @@ private void visualize() {
147141
}
148142

149143
private Position toPosition(final Node node) {
150-
return Position.of(node.getX(), node.getY());
144+
return Position.of(node.x(), node.y());
151145
}
152146

153147
private Function<Path, Boolean> stopAt(
@@ -191,7 +185,7 @@ private Integer solve2() {
191185
new PathFinder(goalNode, accessibleNode, max, unusableNodes)
192186
.findPaths(stopAt(accessibleNode, paths));
193187
final Integer length = paths.stream()
194-
.map(Path::getLength)
188+
.map(Path::length)
195189
.collect(summingInt(Integer::valueOf));
196190
log(length);
197191
return length + 1;
@@ -202,21 +196,21 @@ private Integer solve2Cheat() {
202196
final Set<Node> unusableNodes = getUnusableNodes();
203197
log(unusableNodes);
204198
final Set<Integer> holeYs = unusableNodes.stream()
205-
.map(Node::getY)
199+
.map(Node::y)
206200
.collect(toSet());
207201
assertTrue(holeYs.size() == 1, () -> "Expected all unusable nodes in 1 row");
208202
final Integer holeY = holeYs.iterator().next();
209203
if (holeY <= 1) {
210204
throw new IllegalStateException("Unsolvable");
211205
}
212206
assertFalse(unusableNodes.stream()
213-
.max(comparing(Node::getX))
214-
.map(Node::getX)
207+
.max(comparing(Node::x))
208+
.map(Node::x)
215209
.orElseThrow() != getMaxX(),
216210
() -> "Expected unusable row to touch side");
217211
final Integer holeX = unusableNodes.stream()
218-
.min(comparing(Node::getX))
219-
.map(Node::getX)
212+
.min(comparing(Node::x))
213+
.map(Node::x)
220214
.orElseThrow();
221215
final Position hole = Position.of(holeX - 1, holeY);
222216
final Position emptyNode = toPosition(getEmptyNode());
@@ -259,13 +253,22 @@ public static void main(final String[] args) throws Exception {
259253
"/dev/grid/node-x2-y2 9T 6T 3T 66%"
260254
);
261255

262-
@RequiredArgsConstructor
263256
private static final class PathFinder {
264257
private final Position start;
265258
private final Position destination;
266259
private final Position max;
267260
private final Set<Position> unusable;
268261

262+
public PathFinder(
263+
final Position start, final Position destination,
264+
final Position max, final Set<Position> unusable
265+
) {
266+
this.start = start;
267+
this.destination = destination;
268+
this.max = max;
269+
this.unusable = unusable;
270+
}
271+
269272
public void findPaths(final Function<Path, Boolean> stop) {
270273
final Deque<Path> paths = new ArrayDeque<>();
271274
Path path = new Path(0, this.start);
@@ -278,19 +281,19 @@ public void findPaths(final Function<Path, Boolean> stop) {
278281
}
279282
for (final Direction direction : Direction.CAPITAL) {
280283
final Path newPath = buildNewPath(path, direction);
281-
if (isInBounds(newPath.getPosition())
282-
&& isUsable(newPath.getPosition())
283-
&& !seen.contains(newPath.getPosition())) {
284+
if (isInBounds(newPath.position())
285+
&& isUsable(newPath.position())
286+
&& !seen.contains(newPath.position())) {
284287
paths.add(newPath);
285-
seen.add(newPath.getPosition());
288+
seen.add(newPath.position());
286289
}
287290
}
288291
}
289292
}
290293

291294
private Path buildNewPath(final Path path, final Direction direction) {
292-
return new Path(path.getLength() + 1,
293-
path.getPosition().translate(direction));
295+
return new Path(path.length() + 1,
296+
path.position().translate(direction));
294297
}
295298

296299
private boolean isInBounds(final Position position) {
@@ -305,37 +308,13 @@ private boolean isUsable(final Point position) {
305308
}
306309
}
307310

308-
@RequiredArgsConstructor
309-
@EqualsAndHashCode
310-
@ToString
311-
private static final class Path {
312-
@Getter
313-
private final Integer length;
314-
@Getter
315-
private final Position position;
316-
311+
record Path(int length, Position position) {
317312
public boolean isAt(final Position position) {
318313
return this.position.equals(position);
319314
}
320315
}
321-
322-
@AllArgsConstructor
323-
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
324-
@ToString(onlyExplicitlyIncluded = true)
325-
private static final class Node {
326-
@Getter
327-
@EqualsAndHashCode.Include
328-
@ToString.Include
329-
private final Integer x;
330-
@Getter
331-
@EqualsAndHashCode.Include
332-
@ToString.Include
333-
private final Integer y;
334-
@Getter
335-
private final Integer used;
336-
@Getter
337-
private final Integer available;
338-
316+
317+
record Node(int x, int y, int used, int available) {
339318
public boolean isNotEmpty() {
340319
return this.used != 0;
341320
}

0 commit comments

Comments
 (0)