Skip to content

Commit 1704765

Browse files
committed
AoC 2024 Day 12 - java - refactor
1 parent 5a14360 commit 1704765

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/main/java/AoC2024_12.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@
77
import java.util.List;
88
import java.util.Map;
99
import java.util.Set;
10-
import java.util.function.ToIntBiFunction;
1110

1211
import com.github.pareronia.aoc.Grid.Cell;
13-
import com.github.pareronia.aoc.Utils;
12+
import com.github.pareronia.aoc.IterTools.IterToolsIterator;
1413
import com.github.pareronia.aoc.geometry.Direction;
1514
import com.github.pareronia.aoc.graph.BFS;
1615
import com.github.pareronia.aoc.solution.Sample;
1716
import com.github.pareronia.aoc.solution.Samples;
1817
import com.github.pareronia.aoc.solution.SolutionBase;
1918

2019
public final class AoC2024_12 extends SolutionBase<List<String>, Integer, Integer> {
21-
22-
private static final List<List<Direction>> CORNER_DIRS = List.of(
23-
List.of(Direction.LEFT_AND_UP, Direction.LEFT, Direction.UP),
24-
List.of(Direction.RIGHT_AND_UP, Direction.RIGHT, Direction.UP),
25-
List.of(Direction.RIGHT_AND_DOWN, Direction.RIGHT, Direction.DOWN),
26-
List.of(Direction.LEFT_AND_DOWN, Direction.LEFT, Direction.DOWN)
27-
);
2820

2921
private AoC2024_12(final boolean debug) {
3022
super(debug);
@@ -43,17 +35,14 @@ protected List<String> parseInput(final List<String> inputs) {
4335
return inputs;
4436
}
4537

46-
private int solve(
47-
final List<String> input,
48-
final ToIntBiFunction<Cell, Set<Cell>> count
49-
) {
38+
private int solve(final List<String> input, final Pricing pricing) {
5039
final Map<Character, Set<Cell>> plotsByPlant = new HashMap<>();
5140
range(input.size()).forEach(r ->
5241
range(input.get(r).length()).forEach(c ->
5342
plotsByPlant
5443
.computeIfAbsent(input.get(r).charAt(c), k -> new HashSet<>())
5544
.add(Cell.at(r, c))));
56-
final Iterator<Set<Cell>> regions = new Iterator<>() {
45+
final IterToolsIterator<Set<Cell>> regions = new IterToolsIterator<>() {
5746
final Iterator<Character> keys = plotsByPlant.keySet().iterator();
5847
char key = keys.next();
5948
Set<Cell> allPlotsWithPlant = plotsByPlant.get(key);
@@ -77,36 +66,51 @@ public boolean hasNext() {
7766
return !allPlotsWithPlant.isEmpty() || keys.hasNext();
7867
}
7968
};
80-
return Utils.stream(regions)
69+
return regions.stream()
8170
.mapToInt(region -> region.stream()
82-
.mapToInt(plot -> count.applyAsInt(plot, region) * region.size())
83-
.sum())
71+
.mapToInt(plot -> pricing.calculate(plot, region))
72+
.sum() * region.size())
8473
.sum();
8574
}
8675

8776
@Override
8877
public Integer solvePart1(final List<String> input) {
89-
final ToIntBiFunction<Cell, Set<Cell>> countEdges
90-
= (plot, region) -> (4 - (int) plot.capitalNeighbours()
91-
.filter(region::contains)
92-
.count());
93-
return solve(input, countEdges);
78+
return solve(input, Pricing.PERIMETER);
9479
}
9580

9681
@Override
9782
public Integer solvePart2(final List<String> input) {
98-
final Set<int[]> matches = Set.of(
83+
return solve(input, Pricing.NUMBER_OF_SIDES);
84+
}
85+
86+
private enum Pricing {
87+
PERIMETER, NUMBER_OF_SIDES;
88+
89+
private static final List<List<Direction>> CORNER_DIRS = List.of(
90+
List.of(Direction.LEFT_AND_UP, Direction.LEFT, Direction.UP),
91+
List.of(Direction.RIGHT_AND_UP, Direction.RIGHT, Direction.UP),
92+
List.of(Direction.RIGHT_AND_DOWN, Direction.RIGHT, Direction.DOWN),
93+
List.of(Direction.LEFT_AND_DOWN, Direction.LEFT, Direction.DOWN)
94+
);
95+
private static final Set<int[]> MATCHES = Set.of(
9996
new int[] {0, 0, 0}, new int[] {1, 0, 0}, new int[] {0, 1, 1});
100-
final ToIntBiFunction<Cell, Set<Cell>> countCorners
101-
= (plot, region) -> ((int) CORNER_DIRS.stream()
97+
98+
public int calculate(final Cell plot, final Set<Cell> region) {
99+
return switch (this) {
100+
case PERIMETER -> 4 - (int) plot.capitalNeighbours()
101+
.filter(region::contains)
102+
.count();
103+
case NUMBER_OF_SIDES -> (int) CORNER_DIRS.stream()
102104
.filter(d -> {
103105
final int[] test = range(3).intStream()
104106
.map(i -> region.contains(plot.at(d.get(i))) ? 1 : 0)
105107
.toArray();
106-
return matches.stream().anyMatch(m -> Arrays.equals(m, test));
108+
return MATCHES.stream()
109+
.anyMatch(m -> Arrays.equals(m, test));
107110
})
108-
.count());
109-
return solve(input, countCorners);
111+
.count();
112+
};
113+
}
110114
}
111115

112116
@Override

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy