Skip to content

Commit ef10498

Browse files
committed
java - delombok
1 parent a6968ed commit ef10498

12 files changed

+166
-209
lines changed

src/main/java/AoC2015_18.java

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
import com.github.pareronia.aoc.Grid.Cell;
1414
import com.github.pareronia.aoc.solution.SolutionBase;
1515

16-
import lombok.AccessLevel;
17-
import lombok.RequiredArgsConstructor;
18-
19-
public class AoC2015_18 extends SolutionBase<GameOfLife, Integer, Integer> {
16+
public class AoC2015_18 extends SolutionBase<AoC2015_18.GameOfLife, Integer, Integer> {
2017

2118
private AoC2015_18(final boolean debug) {
2219
super(debug);
@@ -123,39 +120,44 @@ public static void main(final String[] args) throws Exception {
123120
"#.#..#\r\n" +
124121
"####.."
125122
);
126-
}
127123

128-
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
129-
final class GameOfLife implements Cloneable {
130-
private static final char ON = '#';
131-
132-
final Set<Cell> grid;
133-
final int height;
134-
final int width;
135-
136-
public static GameOfLife fromInput(final List<String> inputs) {
137-
final int height = inputs.size();
138-
final int width = inputs.get(0).length();
139-
final Set<Cell> grid = IntStream.range(0, height).boxed()
140-
.flatMap(r -> IntStream.range(0, width).mapToObj(c -> Cell.at(r, c)))
141-
.filter(c -> inputs.get(c.getRow()).charAt(c.getCol()) == ON)
142-
.collect(toSet());
143-
return new GameOfLife(Collections.unmodifiableSet(grid), height, width);
144-
}
145-
146-
public static GameOfLife clone(final GameOfLife gameOfLife) {
147-
try {
148-
return gameOfLife.clone();
149-
} catch (final CloneNotSupportedException e) {
150-
throw new RuntimeException(e);
124+
static final class GameOfLife implements Cloneable {
125+
private static final char ON = '#';
126+
127+
final Set<Cell> grid;
128+
final int height;
129+
final int width;
130+
131+
protected GameOfLife(final Set<Cell> grid, final int height, final int width) {
132+
this.grid = grid;
133+
this.height = height;
134+
this.width = width;
151135
}
152-
}
153136

154-
@Override
155-
protected GameOfLife clone() throws CloneNotSupportedException {
156-
return new GameOfLife(
157-
grid.stream().collect(toSet()),
158-
height,
159-
width);
137+
public static GameOfLife fromInput(final List<String> inputs) {
138+
final int height = inputs.size();
139+
final int width = inputs.get(0).length();
140+
final Set<Cell> grid = IntStream.range(0, height).boxed()
141+
.flatMap(r -> IntStream.range(0, width).mapToObj(c -> Cell.at(r, c)))
142+
.filter(c -> inputs.get(c.getRow()).charAt(c.getCol()) == ON)
143+
.collect(toSet());
144+
return new GameOfLife(Collections.unmodifiableSet(grid), height, width);
145+
}
146+
147+
public static GameOfLife clone(final GameOfLife gameOfLife) {
148+
try {
149+
return gameOfLife.clone();
150+
} catch (final CloneNotSupportedException e) {
151+
throw new RuntimeException(e);
152+
}
153+
}
154+
155+
@Override
156+
protected GameOfLife clone() throws CloneNotSupportedException {
157+
return new GameOfLife(
158+
grid.stream().collect(toSet()),
159+
height,
160+
width);
161+
}
160162
}
161163
}

src/main/java/AoC2015_19.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import com.github.pareronia.aoc.solution.Samples;
1414
import com.github.pareronia.aoc.solution.SolutionBase;
1515

16-
import lombok.RequiredArgsConstructor;
17-
18-
public class AoC2015_19 extends SolutionBase<ReplacementsAndMolecule, Integer, Integer> {
16+
public class AoC2015_19
17+
extends SolutionBase<AoC2015_19.ReplacementsAndMolecule, Integer, Integer> {
1918

2019
private AoC2015_19(final boolean debug) {
2120
super(debug);
@@ -133,23 +132,26 @@ public static void main(final String[] args) throws Exception {
133132
}
134133

135134
private static final String TEST1 =
136-
"H => HO\r\n"
137-
+ "H => OH\r\n"
138-
+ "O => HH\r\n"
139-
+ "\r\n"
140-
+ "HOH";
135+
"""
136+
H => HO\r
137+
H => OH\r
138+
O => HH\r
139+
\r
140+
HOH""";
141141
private static final String TEST2 =
142-
"H => HO\r\n"
143-
+ "H => OH\r\n"
144-
+ "O => HH\r\n"
145-
+ "\r\n"
146-
+ "HOHOHO";
142+
"""
143+
H => HO\r
144+
H => OH\r
145+
O => HH\r
146+
\r
147+
HOHOHO""";
147148
private static final String TEST3 =
148-
"H => HO\r\n"
149-
+ "H => OH\r\n"
150-
+ "Oo => HH\r\n"
151-
+ "\r\n"
152-
+ "HOHOHO";
149+
"""
150+
H => HO\r
151+
H => OH\r
152+
Oo => HH\r
153+
\r
154+
HOHOHO""";
153155
private static final List<String> TEST4 = splitLines(
154156
"e => H\r\n"
155157
+ "e => O\r\n"
@@ -168,21 +170,19 @@ public static void main(final String[] args) throws Exception {
168170
+ "\r\n"
169171
+ "HOHOHO"
170172
);
171-
}
172173

173-
@RequiredArgsConstructor
174-
final class ReplacementsAndMolecule {
175-
176-
final Map<String, List<String>> replacements;
177-
final String molecule;
178-
179-
public static ReplacementsAndMolecule fromInput(final List<String> inputs) {
180-
final List<List<String>> blocks = StringOps.toBlocks(inputs);
181-
final Map<String, List<String>> replacements = blocks.get(0).stream()
182-
.map(s -> s.split(" => "))
183-
.collect(groupingBy(sp -> sp[0], mapping(s -> s[1], toList())));
184-
assert blocks.get(1).size() == 1;
185-
final String molecule = blocks.get(1).get(0);
186-
return new ReplacementsAndMolecule(replacements, molecule);
174+
record ReplacementsAndMolecule(
175+
Map<String, List<String>> replacements,
176+
String molecule
177+
) {
178+
public static ReplacementsAndMolecule fromInput(final List<String> inputs) {
179+
final List<List<String>> blocks = StringOps.toBlocks(inputs);
180+
final Map<String, List<String>> replacements = blocks.get(0).stream()
181+
.map(s -> s.split(" => "))
182+
.collect(groupingBy(sp -> sp[0], mapping(s -> s[1], toList())));
183+
assert blocks.get(1).size() == 1;
184+
final String molecule = blocks.get(1).get(0);
185+
return new ReplacementsAndMolecule(replacements, molecule);
186+
}
187187
}
188188
}

src/main/java/AoC2015_21.java

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99
import java.util.Comparator;
1010
import java.util.HashSet;
1111
import java.util.List;
12+
import java.util.Objects;
1213
import java.util.Set;
1314
import java.util.function.Predicate;
1415

1516
import com.github.pareronia.aoc.SetUtils;
1617
import com.github.pareronia.aoc.solution.SolutionBase;
1718

18-
import lombok.EqualsAndHashCode;
19-
import lombok.Getter;
20-
import lombok.RequiredArgsConstructor;
21-
import lombok.ToString;
22-
2319
public class AoC2015_21 extends SolutionBase<AoC2015_21.Game, Integer, Integer> {
2420

2521
protected AoC2015_21(final boolean debug) {
@@ -47,22 +43,22 @@ private int solve(
4743
.sorted(comparator)
4844
.filter(filter)
4945
.findFirst()
50-
.map(Game.PlayerConfig::getTotalCost)
46+
.map(Game.PlayerConfig::totalCost)
5147
.orElseThrow(() -> new IllegalStateException("Unsolvable"));
5248
}
5349

5450
@Override
5551
protected Integer solvePart1(final Game game) {
5652
return solve(
57-
game.getPlayerConfigs(),
53+
game.playerConfigs(),
5854
game.lowestCost(),
5955
game.winsFromBoss());
6056
}
6157

6258
@Override
6359
protected Integer solvePart2(final Game game) {
6460
return solve(
65-
game.getPlayerConfigs(),
61+
game.playerConfigs(),
6662
game.lowestCost().reversed(),
6763
game.winsFromBoss().negate());
6864
}
@@ -71,11 +67,7 @@ public static void main(final String[] args) throws Exception {
7167
AoC2015_21.create().run();
7268
}
7369

74-
@RequiredArgsConstructor
75-
static final class Game {
76-
private final Boss boss;
77-
@Getter
78-
private final List<PlayerConfig> playerConfigs;
70+
record Game(Boss boss, List<PlayerConfig> playerConfigs) {
7971

8072
public static Game fromInput(final List<String> inputs) {
8173
return new Game(parse(inputs), setUpPlayerConfigs(setUpShop()));
@@ -130,21 +122,10 @@ private static List<PlayerConfig> setUpPlayerConfigs(final Shop shop) {
130122
return configs;
131123
}
132124

133-
@RequiredArgsConstructor
134-
@Getter
135-
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
136-
@ToString
137-
private static final class ShopItem {
125+
record ShopItem(Type type, String name, int cost, int damage, int armor) {
126+
138127
private enum Type { WEAPON, ARMOR, RING, NONE }
139128

140-
@EqualsAndHashCode.Include
141-
private final Type type;
142-
@EqualsAndHashCode.Include
143-
private final String name;
144-
private final Integer cost;
145-
private final Integer damage;
146-
private final Integer armor;
147-
148129
public static ShopItem weapon(
149130
final String name, final Integer cost, final Integer damage) {
150131
return new ShopItem(Type.WEAPON, name, cost, damage, 0);
@@ -173,12 +154,29 @@ public boolean isArmor() {
173154
public boolean isRing() {
174155
return this.type == Type.RING;
175156
}
157+
158+
@Override
159+
public int hashCode() {
160+
return Objects.hash(name, type);
161+
}
162+
163+
@Override
164+
public boolean equals(final Object obj) {
165+
if (this == obj) {
166+
return true;
167+
}
168+
if (obj == null) {
169+
return false;
170+
}
171+
if (getClass() != obj.getClass()) {
172+
return false;
173+
}
174+
final ShopItem other = (ShopItem) obj;
175+
return Objects.equals(name, other.name) && type == other.type;
176+
}
176177
}
177178

178-
@RequiredArgsConstructor
179-
@ToString
180-
private static final class Shop {
181-
private final Set<ShopItem> items;
179+
record Shop(Set<ShopItem> items) {
182180

183181
public Set<ShopItem> getWeapons() {
184182
return this.items.stream()
@@ -214,44 +212,34 @@ public Set<Set<ShopItem>> getRings() {
214212
}
215213
}
216214

217-
@ToString
218-
@Getter
219-
static final class PlayerConfig {
220-
private final int hitPoints;
221-
private final int totalCost;
222-
private final int totalDamage;
223-
private final int totalArmor;
215+
record PlayerConfig(
216+
int hitPoints, int totalCost, int totalDamage, int totalArmor) {
224217

225218
public PlayerConfig(final Set<ShopItem> items) {
226-
this.hitPoints = 100;
227-
this.totalCost = items.stream().mapToInt(ShopItem::getCost).sum();
228-
this.totalDamage = items.stream().mapToInt(ShopItem::getDamage).sum();
229-
this.totalArmor = items.stream().mapToInt(ShopItem::getArmor).sum();
219+
this(
220+
100,
221+
items.stream().mapToInt(ShopItem::cost).sum(),
222+
items.stream().mapToInt(ShopItem::damage).sum(),
223+
items.stream().mapToInt(ShopItem::armor).sum());
230224
}
231225
}
232226

233-
@RequiredArgsConstructor
234-
@Getter
235-
private static final class Boss {
236-
private final int hitPoints;
237-
private final int damage;
238-
private final int armor;
239-
}
227+
record Boss(int hitPoints, int damage, int armor) { }
240228

241229
public Comparator<Game.PlayerConfig> lowestCost() {
242-
return comparing(Game.PlayerConfig::getTotalCost);
230+
return comparing(Game.PlayerConfig::totalCost);
243231
}
244232

245233
public Predicate<Game.PlayerConfig> winsFromBoss() {
246234
return playerConfig -> {
247-
int playerHP = playerConfig.getHitPoints();
248-
int bossHP = this.boss.getHitPoints();
235+
int playerHP = playerConfig.hitPoints();
236+
int bossHP = this.boss.hitPoints();
249237
while (true) {
250-
bossHP -= Math.max(playerConfig.getTotalDamage() - this.boss.getArmor(), 1);
238+
bossHP -= Math.max(playerConfig.totalDamage() - this.boss.armor(), 1);
251239
if (bossHP <= 0) {
252240
return true;
253241
}
254-
playerHP -= Math.max(this.boss.getDamage() - playerConfig.getTotalArmor(), 1);
242+
playerHP -= Math.max(this.boss.damage() - playerConfig.totalArmor(), 1);
255243
if (playerHP <= 0) {
256244
return false;
257245
}

src/main/java/AoC2016_02.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import com.github.pareronia.aocd.Aocd;
1515
import com.github.pareronia.aocd.Puzzle;
1616

17-
import lombok.RequiredArgsConstructor;
18-
1917
public class AoC2016_02 extends AoCBase {
2018

2119
private static final Map<Position, Character> LAYOUT1 = Map.of(
@@ -93,11 +91,18 @@ public static void main(final String[] args) throws Exception {
9391
"UUUUD"
9492
);
9593

96-
@RequiredArgsConstructor(staticName = "fromLayout")
9794
private static final class Keypad {
9895
private final Map<Position, Character> positions;
9996
private Position current = Position.ORIGIN;
10097

98+
private Keypad(final Map<Position, Character> positions) {
99+
this.positions = positions;
100+
}
101+
102+
public static Keypad fromLayout(final Map<Position, Character> positions) {
103+
return new Keypad(positions);
104+
}
105+
101106
public String executeInstructions(final List<List<Direction>> directions) {
102107
return directions.stream()
103108
.map(this::executeInstruction)

0 commit comments

Comments
 (0)