2
2
3
3
import dev .linl33 .adventofcode .lib .graph .GraphUtil ;
4
4
import dev .linl33 .adventofcode .lib .grid .RowArrayGrid ;
5
+ import jdk .incubator .vector .ShortVector ;
6
+ import jdk .incubator .vector .VectorOperators ;
7
+ import jdk .incubator .vector .VectorSpecies ;
5
8
import org .jetbrains .annotations .NotNull ;
6
9
7
10
import java .io .BufferedReader ;
10
13
import java .util .function .IntUnaryOperator ;
11
14
12
15
public class Day12 extends AdventSolution2022 <Integer , Integer > {
16
+ private static final VectorSpecies <Short > SPECIES = ShortVector .SPECIES_PREFERRED ;
17
+
13
18
private static final int POTENTIAL_ENDPOINTS_MAX = 100 ;
14
19
15
20
public static void main (String [] args ) {
@@ -32,13 +37,23 @@ private static int calculatePathLength(@NotNull BufferedReader reader, boolean f
32
37
33
38
var start = 0 ;
34
39
var end = 0 ;
40
+ var startFound = false ;
41
+ var endFound = false ;
35
42
for (int i = 0 ; i < gridBackingArray .length ; i ++) {
36
43
if (gridBackingArray [i ] == 'E' ) {
37
44
gridBackingArray [i ] = 'z' ;
38
45
end = i + 1 ;
46
+
47
+ if ((endFound = true ) && startFound ) {
48
+ break ;
49
+ }
39
50
} else if (gridBackingArray [i ] == 'S' ) {
40
51
gridBackingArray [i ] = 'a' ;
41
52
start = i + 1 ;
53
+
54
+ if ((startFound = true ) && endFound ) {
55
+ break ;
56
+ }
42
57
}
43
58
}
44
59
@@ -63,7 +78,7 @@ private static int calculatePathLength(@NotNull BufferedReader reader, boolean f
63
78
return (heuristicCache [p + 1 ] = Math .abs (pathEndX - x ) + Math .abs (pathEndY - y ));
64
79
};
65
80
} else {
66
- var potentialEndpoints = new int [POTENTIAL_ENDPOINTS_MAX * 2 ];
81
+ var potentialEndpoints = new short [POTENTIAL_ENDPOINTS_MAX * 2 ];
67
82
var potentialEndpointsCount = 0 ;
68
83
69
84
gridLoop :
@@ -92,14 +107,21 @@ private static int calculatePathLength(@NotNull BufferedReader reader, boolean f
92
107
// if its lowest elevation is 'c' then it's inaccessible
93
108
// if its highest elevation is 'a' then it has a neighbor with a shorter path
94
109
if (grid .get (x2 , y2 ) == 'b' ) {
95
- potentialEndpoints [potentialEndpointsCount ++] = x ;
96
- potentialEndpoints [potentialEndpointsCount ++] = y ;
110
+ potentialEndpoints [potentialEndpointsCount ] = (short ) x ;
111
+ potentialEndpoints [potentialEndpointsCount + POTENTIAL_ENDPOINTS_MAX ] = (short ) y ;
112
+ potentialEndpointsCount ++;
97
113
continue gridLoop ;
98
114
}
99
115
}
100
116
}
101
117
}
102
118
119
+ var alignedLength = Math .ceilDiv (potentialEndpointsCount , SPECIES .length ()) * SPECIES .length ();
120
+ if (potentialEndpointsCount % SPECIES .length () != 0 ) {
121
+ Arrays .fill (potentialEndpoints , potentialEndpointsCount , alignedLength , (short ) (grid .width () * 2 ));
122
+ Arrays .fill (potentialEndpoints , potentialEndpointsCount + POTENTIAL_ENDPOINTS_MAX , alignedLength + POTENTIAL_ENDPOINTS_MAX , (short ) (grid .height () * 2 ));
123
+ }
124
+
103
125
var finalPotentialEndpointsCount = potentialEndpointsCount ;
104
126
heuristic = p -> {
105
127
if (heuristicCache [p ] != 0 ) {
@@ -110,15 +132,20 @@ private static int calculatePathLength(@NotNull BufferedReader reader, boolean f
110
132
var x = p % grid .width ();
111
133
var y = p / grid .width ();
112
134
113
- var min = Integer .MAX_VALUE ;
114
- for (int i = 0 ; i < finalPotentialEndpointsCount ; i += 2 ) {
115
- var pathEndX = potentialEndpoints [i ];
116
- var pathEndY = potentialEndpoints [i + 1 ];
135
+ var xVector = (ShortVector ) SPECIES .broadcast (x );
136
+ var yVector = (ShortVector ) SPECIES .broadcast (y );
137
+ var minVector = (ShortVector ) SPECIES .broadcast (Short .MAX_VALUE );
138
+
139
+ for (int i = 0 ; i < alignedLength ; i += SPECIES .length ()) {
140
+ var pathX = (ShortVector ) SPECIES .fromArray (potentialEndpoints , i );
141
+ var pathY = (ShortVector ) SPECIES .fromArray (potentialEndpoints , i + finalPotentialEndpointsCount );
117
142
118
- var manhattanDistance = Math .abs (pathEndX - x ) + Math .abs (pathEndY - y );
119
- min = Math .min (min , manhattanDistance );
143
+ pathX = xVector .sub (pathX ).abs ();
144
+ pathY = yVector .sub (pathY ).abs ();
145
+ minVector = minVector .min (pathX .add (pathY ));
120
146
}
121
147
148
+ var min = minVector .reduceLanes (VectorOperators .MIN );
122
149
return (heuristicCache [p + 1 ] = min );
123
150
};
124
151
}
0 commit comments