-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2022_14.java
170 lines (152 loc) Β· 5.87 KB
/
AoC2022_14.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import static com.github.pareronia.aoc.Utils.toAString;
import static java.util.stream.Collectors.toList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.IntStream;
import com.github.pareronia.aoc.geometry.Position;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public class AoC2022_14
extends SolutionBase<AoC2022_14.Cave, Integer, Integer> {
private static final Position SOURCE = Position.of(500, 0);
private static final char START = '+';
private static final char EMPTY = ' ';
private static final char ROCK = 'β';
private static final char SAND = 'o';
private AoC2022_14(final boolean debug) {
super(debug);
}
public static final AoC2022_14 create() {
return new AoC2022_14(false);
}
public static final AoC2022_14 createDebug() {
return new AoC2022_14(true);
}
Optional<Position> drop(final boolean[][] occupied, final int maxY) {
Position curr = SOURCE;
while (true) {
final int y = curr.getY() + 1;
final Position p = curr;
for (final int x : List.of(0, -1 , 1)) {
try {
if (!occupied[y][curr.getX() + x]) {
curr = Position.of(curr.getX() + x, y);
break;
}
} catch (final ArrayIndexOutOfBoundsException e) {
}
}
if (curr.equals(p)) {
return Optional.of(curr);
}
if (curr.getY() > maxY) {
return Optional.empty();
}
}
}
private int solve(final Cave cave, final int maxY) {
int cnt = 0;
while (true) {
final Optional<Position> p = drop(cave.occupied, maxY);
if (p.isPresent()) {
cave.occupied[p.get().getY()][p.get().getX()] = true;
cnt++;
if (p.get().equals(SOURCE)) {
break;
}
} else {
break;
}
}
return cnt;
}
void draw(final Cave cave) {
if (!this.debug) {
return;
}
final var statsX = IntStream.range(0, cave.occupied.length)
.flatMap(y -> IntStream.range(0, cave.occupied[y].length)
.filter(x -> cave.occupied[y][x]))
.summaryStatistics();
IntStream.rangeClosed(SOURCE.getY(), cave.maxY).forEach(y -> {
final var line = IntStream.rangeClosed(statsX.getMin(), statsX.getMax())
.mapToObj(x -> {
final Position pos = Position.of(x, y);
if (cave.rocks.contains(pos)) {
return ROCK;
} else if (cave.occupied[pos.getY()][pos.getX()]) {
return SAND;
} else if (SOURCE.equals(pos)) {
return START;
}
return EMPTY;
})
.collect(toAString());
log(line);
});
}
@Override
protected Cave parseInput(final List<String> inputs) {
return Cave.fromInput(inputs);
}
@Override
public Integer solvePart1(final Cave cave) {
final int ans = solve(cave, cave.maxY);
draw(cave);
return ans;
}
@Override
public Integer solvePart2(final Cave cave) {
final Cave newCave = Cave.withRocks(cave.rocks);
final int ans = solve(newCave, cave.maxY + 2);
draw(newCave);
return ans;
}
@Samples({
@Sample(method = "part1", input = TEST, expected = "24"),
@Sample(method = "part2", input = TEST, expected = "93"),
})
public static void main(final String[] args) throws Exception {
AoC2022_14.create().run();
}
private static final String TEST = """
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9
""";
record Cave(Set<Position> rocks, boolean[][] occupied, int maxY) {
public static Cave withRocks(final Set<Position> rocks) {
final int maxX = rocks.stream().mapToInt(Position::getX).max().getAsInt();
final int maxY = rocks.stream().mapToInt(Position::getY).max().getAsInt();
final var occupied = new boolean[maxY + 2][maxX + 150];
rocks.stream().forEach(p -> occupied[p.getY()][p.getX()] = true);
return new Cave(rocks, occupied, maxY);
}
public static Cave fromInput(final List<String> input) {
final Set<Position> rocks = new HashSet<>();
for (final String line : input) {
final var splits = line.split(" -> ");
IntStream.range(1, splits.length).forEach(i -> {
final var splits1 = splits[i - 1].split(",");
final var splits2 = splits[i].split(",");
final var xs = List.of(splits1, splits2).stream()
.map(a -> a[0])
.map(Integer::parseInt)
.sorted()
.collect(toList());
final var ys = List.of(splits1, splits2).stream()
.map(a -> a[1])
.map(Integer::parseInt)
.sorted()
.collect(toList());
IntStream.rangeClosed(xs.get(0), xs.get(1)).forEach(x ->
IntStream.rangeClosed(ys.get(0), ys.get(1)).forEach(y ->
rocks.add(Position.of(x, y))));
});
}
return Cave.withRocks(rocks);
}
}
}