@@ -40,7 +40,7 @@ public int compareTo(PathComponent o) {
4040 }
4141 Map <String , PathComponent > pathComponents = new HashMap <>();
4242 allNodes .forEach (s -> dijkstra (connections , s ).forEach ((k , v ) -> {
43- var label = k .compareTo (v .a ()) < 0 ? k + " " + v .a () : v .a () + " " + k ;
43+ var label = k .compareTo (v .node ) < 0 ? k + " " + v .node : v .node + " " + k ;
4444 pathComponents .computeIfAbsent (label , t -> new PathComponent (t , new AtomicInteger ())).counter .incrementAndGet ();
4545 }));
4646 pathComponents .values ().stream ().sorted (reverseOrder ()).limit (3L ).map (PathComponent ::label ).map (s -> s .split (" " )).forEach (s -> {
@@ -51,7 +51,7 @@ public int compareTo(PathComponent o) {
5151 return reducedSize * (totalSize - reducedSize );
5252 }
5353
54- record ND (String node , int distance ) implements Comparable <ND > {
54+ public record ND (String node , int distance ) implements Comparable <ND > {
5555
5656 @ Override
5757 public int compareTo (ND o ) {
@@ -66,20 +66,20 @@ public Object part2() {
6666 return "That's all!" ;
6767 }
6868
69- public Map <String , Pair < T , Integer >> dijkstra (ListMap <T , T > nodes , T node ) {
70- var queue = new PriorityQueue <Pair < T , Integer > >();
71- Set <T > visited = new HashSet <>();
72- Map <T , Pair < T , Integer > > distanceMap = new HashMap <>();
73- queue .add (new Pair <> (node , 0 ));
69+ public Map <String , ND > dijkstra (ListMap <String , String > nodes , String node ) {
70+ var queue = new PriorityQueue <ND >();
71+ Set <String > visited = new HashSet <>();
72+ Map <String , ND > distanceMap = new HashMap <>();
73+ queue .add (new ND (node , 0 ));
7474 while (!queue .isEmpty ()) {
7575 var current = queue .remove ();
76- visited .add (current .a () );
77- nodes .get (current .a () ).stream ()
76+ visited .add (current .node );
77+ nodes .get (current .node ).stream ()
7878 .filter (s -> !visited .contains (s ))
79- .filter (s -> !distanceMap .containsKey (s ) || distanceMap .get (s ).b () > current .b () + 1 )
79+ .filter (s -> !distanceMap .containsKey (s ) || distanceMap .get (s ).distance > current .distance + 1 )
8080 .forEach (s -> {
81- distanceMap .put (s , new Pair <> (current .a () , current .b () + 1 ));
82- queue .add (new Pair <> (s , current .b () + 1 ));
81+ distanceMap .put (s , new ND (current .node , current .distance + 1 ));
82+ queue .add (new ND (s , current .distance + 1 ));
8383 });
8484 }
8585 return distanceMap ;
0 commit comments