1
1
package adventofcode .v2022 ;
2
2
3
3
import adventofcode .Utils ;
4
- import org .apache .commons .lang3 .tuple .Pair ;
5
4
6
5
import java .util .Arrays ;
7
6
import java .util .Collection ;
@@ -16,10 +15,8 @@ public static void main(String[] args) {
16
15
}
17
16
18
17
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 ;
23
20
int sandCount = 0 ;
24
21
while (!grid [1 ][sandSource ]) {
25
22
try {
@@ -36,11 +33,38 @@ public static void part1() {
36
33
sandCount ++;
37
34
}
38
35
System .out .println ("Sand count : " + sandCount );
39
- print (grid );
40
36
}
41
37
42
38
public static void part2 () {
39
+ boolean [][] grid = rocks ();
40
+ boolean [][] largeGrid = new boolean [grid .length + 2 ][99999 ];
43
41
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 );
44
68
}
45
69
46
70
static Coordinate moveSand (Coordinate sandPosition , boolean [][] grid ) {
@@ -69,7 +93,7 @@ static void print(boolean[][] grid) {
69
93
}
70
94
}
71
95
72
- static Pair < Integer , boolean [][]> rocks () {
96
+ static boolean [][] rocks () {
73
97
List <List <Coordinate >> lines = Utils .readInput ("/v2022/d14/input.txt" ).stream ()
74
98
.map (line -> Arrays .stream (line .split (" -> " ))
75
99
.map (item -> {
@@ -80,46 +104,32 @@ static Pair<Integer, boolean[][]> rocks() {
80
104
)
81
105
.toList ();
82
106
83
- int minX = lines .stream ()
84
- .flatMap (Collection ::stream )
85
- .map (Coordinate ::x )
86
- .min (Comparator .naturalOrder ())
87
- .orElse (0 );
88
-
89
107
int maxX = lines .stream ()
90
108
.flatMap (Collection ::stream )
91
109
.map (Coordinate ::x )
92
110
.max (Comparator .naturalOrder ())
93
111
.orElse (0 );
94
112
95
- int minY = lines .stream ()
96
- .flatMap (Collection ::stream )
97
- .map (Coordinate ::y )
98
- .min (Comparator .naturalOrder ())
99
- .orElse (0 );
100
-
101
113
int maxY = lines .stream ()
102
114
.flatMap (Collection ::stream )
103
115
.map (Coordinate ::y )
104
116
.max (Comparator .naturalOrder ())
105
117
.orElse (0 );
106
118
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 ];
110
120
111
121
lines .forEach (list -> IntStream .range (1 , list .size ())
112
122
.forEach (i -> {
113
123
Coordinate start = list .get (i - 1 );
114
124
Coordinate end = list .get (i );
115
125
for (int k = Math .min (start .y , end .y ); k <= Math .max (start .y , end .y ); k ++) {
116
126
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 ;
118
128
}
119
129
}
120
130
})
121
131
);
122
- return Pair . of ( minX , rocks ) ;
132
+ return rocks ;
123
133
}
124
134
125
135
record Coordinate (int x , int y ) {
0 commit comments