88import java .nio .file .Path ;
99import java .util .List ;
1010import java .util .Set ;
11- import java .util .TreeSet ;
1211import java .util .stream .Collectors ;
1312import java .util .stream .IntStream ;
1413
@@ -20,19 +19,15 @@ public static void main(String[] args) throws IOException {
2019
2120 public static long part01 (int expansionFactor ) throws IOException {
2221 URL resource = Main .class .getClassLoader ().getResource ("day11-input.txt" );
23- assert resource != null ;
2422 List <String > lines = Files .lines (Path .of (resource .getFile ())).toList ();
25- Set < Pair < Integer , Integer >> stars = new TreeSet <>();
23+
2624 int boundaryX = lines .get (0 ).length ();
2725 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 ());
2830
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- }
3631 Set <Integer > emptyXs = IntStream .range (0 , boundaryX )
3732 .filter (x -> IntStream .range (0 , boundaryY )
3833 .noneMatch (y -> stars .contains (Pair .with (x , y ))))
@@ -44,7 +39,6 @@ public static long part01(int expansionFactor) throws IOException {
4439
4540 return stars .stream ()
4641 .flatMap (star1 -> stars .stream ().map (star2 -> Pair .with (star1 , star2 )))
47- .parallel ()
4842 .filter (starPair -> !starPair .getValue0 ().equals (starPair .getValue1 ()))
4943 .map (starPair -> distanceBetweenStars (starPair .getValue0 (), starPair .getValue1 (), emptyXs , emptyYs , expansionFactor ))
5044 .reduce (Long ::sum )
0 commit comments