Skip to content

Commit f47d728

Browse files
committed
day14 part 2
1 parent 9c1020d commit f47d728

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/main/java/adventofcode/v2022/Day14.java

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package adventofcode.v2022;
22

33
import adventofcode.Utils;
4-
import org.apache.commons.lang3.tuple.Pair;
54

65
import java.util.Arrays;
76
import java.util.Collection;
@@ -16,10 +15,8 @@ public static void main(String[] args) {
1615
}
1716

1817
public static void part1() {
19-
Pair<Integer, boolean[][]> pair = rocks();
20-
print(pair.getRight());
21-
boolean[][] grid =pair.getRight();
22-
int sandSource = 500 - pair.getLeft();
18+
boolean[][] grid = rocks();
19+
int sandSource = 500;
2320
int sandCount = 0;
2421
while (!grid[1][sandSource]) {
2522
try {
@@ -36,11 +33,38 @@ public static void part1() {
3633
sandCount++;
3734
}
3835
System.out.println("Sand count : " + sandCount);
39-
print(grid);
4036
}
4137

4238
public static void part2() {
39+
boolean[][] grid = rocks();
40+
boolean[][] largeGrid = new boolean[grid.length + 2][99999];
4341

42+
for (int i = 0; i < grid.length; i++) {
43+
System.arraycopy(grid[i], 0, largeGrid[i], 0, grid[i].length);
44+
}
45+
for (int i = 0; i < grid[0].length; i++) {
46+
largeGrid[grid.length][i] = false;
47+
}
48+
for (int i = 0; i < 99999; i++) {
49+
largeGrid[grid.length + 1][i] = true;
50+
}
51+
int sandSource = 500;
52+
int sandCount = 0;
53+
while (!largeGrid[0][sandSource]) {
54+
try {
55+
Coordinate sandPosition = new Coordinate(sandSource, 0);
56+
Coordinate nextSandPosition = moveSand(sandPosition, largeGrid);
57+
while (!sandPosition.equals(nextSandPosition)) {
58+
sandPosition = nextSandPosition;
59+
nextSandPosition = moveSand(sandPosition, largeGrid);
60+
}
61+
largeGrid[sandPosition.y()][sandPosition.x()] = true;
62+
} catch (ArrayIndexOutOfBoundsException e) {
63+
break;
64+
}
65+
sandCount++;
66+
}
67+
System.out.println("Sand count : " + sandCount);
4468
}
4569

4670
static Coordinate moveSand(Coordinate sandPosition, boolean[][] grid) {
@@ -69,7 +93,7 @@ static void print(boolean[][] grid) {
6993
}
7094
}
7195

72-
static Pair<Integer, boolean[][]> rocks() {
96+
static boolean[][] rocks() {
7397
List<List<Coordinate>> lines = Utils.readInput("/v2022/d14/input.txt").stream()
7498
.map(line -> Arrays.stream(line.split(" -> "))
7599
.map(item -> {
@@ -80,46 +104,32 @@ static Pair<Integer, boolean[][]> rocks() {
80104
)
81105
.toList();
82106

83-
int minX = lines.stream()
84-
.flatMap(Collection::stream)
85-
.map(Coordinate::x)
86-
.min(Comparator.naturalOrder())
87-
.orElse(0);
88-
89107
int maxX = lines.stream()
90108
.flatMap(Collection::stream)
91109
.map(Coordinate::x)
92110
.max(Comparator.naturalOrder())
93111
.orElse(0);
94112

95-
int minY = lines.stream()
96-
.flatMap(Collection::stream)
97-
.map(Coordinate::y)
98-
.min(Comparator.naturalOrder())
99-
.orElse(0);
100-
101113
int maxY = lines.stream()
102114
.flatMap(Collection::stream)
103115
.map(Coordinate::y)
104116
.max(Comparator.naturalOrder())
105117
.orElse(0);
106118

107-
System.out.println("minX=" + minX + ", maxX=" + maxX + " / minY=" + minY + ", maxY=" + maxY);
108-
109-
boolean[][] rocks = new boolean[maxY + 1][maxX - minX + 1];
119+
boolean[][] rocks = new boolean[maxY + 1][maxX + 1];
110120

111121
lines.forEach(list -> IntStream.range(1, list.size())
112122
.forEach(i -> {
113123
Coordinate start = list.get(i - 1);
114124
Coordinate end = list.get(i);
115125
for (int k = Math.min(start.y, end.y); k <= Math.max(start.y, end.y); k++) {
116126
for (int j = Math.min(start.x, end.x); j <= Math.max(start.x, end.x); j++) {
117-
rocks[k][j - minX] = true;
127+
rocks[k][j] = true;
118128
}
119129
}
120130
})
121131
);
122-
return Pair.of(minX, rocks);
132+
return rocks;
123133
}
124134

125135
record Coordinate(int x, int y) {

0 commit comments

Comments
 (0)