-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle14.java
126 lines (116 loc) · 3.45 KB
/
Puzzle14.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
package advent2022;
import static java.lang.Integer.max;
import static java.lang.Integer.min;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;
/**
* @author Éamonn McManus
*/
public class Puzzle14 {
private static final String SAMPLE =
"""
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample", () -> new StringReader(SAMPLE),
"problem",
() -> new InputStreamReader(Puzzle14.class.getResourceAsStream("puzzle14.txt")));
public static void main(String[] args) throws Exception {
for (var entry : INPUT_PRODUCERS.entrySet()) {
String name = entry.getKey();
try (Reader r = entry.getValue().call()) {
List<String> lines = CharStreams.readLines(r);
int countPart1 = solve(lines, false);
System.out.println("For " + name + ", part 1 count is " + countPart1);
int countPart2 = solve(lines, true);
System.out.println("For " + name + ", part 2 count is " + countPart2);
}
}
}
private static int solve(List<String> lines, boolean part2) {
char[][] grid = makeGrid(lines);
if (part2) {
int maxY = 0;
for (int y = 0; y < grid.length; y++) {
if (!allDots(grid[y])) {
maxY = y;
}
}
Arrays.fill(grid[maxY + 2], '#');
}
int count = 0;
while (grid[0][500] == '.') {
boolean changed = false;
int x = 500;
for (int y = 1; y < grid.length; y++) {
if (grid[y][x] != '.') {
if (grid[y][x - 1] == '.') {
--x;
} else if (grid[y][x + 1] == '.') {
++x;
} else {
grid[y - 1][x] = 'o';
changed = true;
break; // I forgot this at first, so I wasted a lot of time debugging.
}
}
}
if (changed) {
count++;
} else {
break;
}
}
return count;
}
private static boolean allDots(char[] line) {
for (char c : line) {
if (c != '.') {
return false;
}
}
return true;
}
private static char[][] makeGrid(List<String> lines) {
char[][] grid = new char[501][1000];
for (char[] line : grid) {
Arrays.fill(line, '.');
}
record Pair(int x, int y) {}
for (String line : lines) {
List<Pair> pairs =
Pattern.compile("(\\d+),(\\d+)")
.matcher(line)
.results()
.map(mr -> new Pair(Integer.parseInt(mr.group(1)), Integer.parseInt(mr.group(2))))
.toList();
for (int i = 1; i < pairs.size(); i++) {
Pair from = pairs.get(i - 1);
Pair to = pairs.get(i);
if (from.x == to.x) {
for (int y = min(from.y, to.y); y <= max(from.y, to.y); y++) {
grid[y][from.x] = '#';
}
} else if (from.y == to.y) {
for (int x = min(from.x, to.x); x <= max(from.x, to.x); x++) {
grid[from.y][x] = '#';
}
} else {
throw new IllegalStateException(
"Diagonal line " + from.x + "," + from.y + " -> " + to.x + "," + to.y);
}
}
}
return grid;
}
}