8
8
import java .nio .file .Path ;
9
9
import java .util .List ;
10
10
import java .util .Set ;
11
- import java .util .TreeSet ;
12
11
import java .util .stream .Collectors ;
13
12
import java .util .stream .IntStream ;
14
13
@@ -20,19 +19,15 @@ public static void main(String[] args) throws IOException {
20
19
21
20
public static long part01 (int expansionFactor ) throws IOException {
22
21
URL resource = Main .class .getClassLoader ().getResource ("day11-input.txt" );
23
- assert resource != null ;
24
22
List <String > lines = Files .lines (Path .of (resource .getFile ())).toList ();
25
- Set < Pair < Integer , Integer >> stars = new TreeSet <>();
23
+
26
24
int boundaryX = lines .get (0 ).length ();
27
25
int boundaryY = lines .size ();
26
+ Set <Pair <Integer , Integer >> stars = IntStream .range (0 , boundaryY ).boxed ()
27
+ .flatMap (y -> IntStream .range (0 , boundaryX ).mapToObj (x -> Pair .with (x , y )))
28
+ .filter (pair -> lines .get (pair .getValue1 ()).charAt (pair .getValue0 ()) == '#' )
29
+ .collect (Collectors .toSet ());
28
30
29
- for (int y = 0 ; y < boundaryY ; y ++) {
30
- for (int x = 0 ; x < boundaryX ; x ++) {
31
- if (lines .get (y ).charAt (x ) == '#' ) {
32
- stars .add (Pair .with (x , y ));
33
- }
34
- }
35
- }
36
31
Set <Integer > emptyXs = IntStream .range (0 , boundaryX )
37
32
.filter (x -> IntStream .range (0 , boundaryY )
38
33
.noneMatch (y -> stars .contains (Pair .with (x , y ))))
@@ -44,7 +39,6 @@ public static long part01(int expansionFactor) throws IOException {
44
39
45
40
return stars .stream ()
46
41
.flatMap (star1 -> stars .stream ().map (star2 -> Pair .with (star1 , star2 )))
47
- .parallel ()
48
42
.filter (starPair -> !starPair .getValue0 ().equals (starPair .getValue1 ()))
49
43
.map (starPair -> distanceBetweenStars (starPair .getValue0 (), starPair .getValue1 (), emptyXs , emptyYs , expansionFactor ))
50
44
.reduce (Long ::sum )
0 commit comments