From c651b9ba8fd490578fbf68278277c6f03c0c8e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Zaicsek?= Date: Wed, 27 Dec 2023 21:13:48 +0100 Subject: [PATCH] Execute part1 and 2 parallel in Day 23 --- .../io/github/zebalu/aoc2023/days/Day23.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day23.java b/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day23.java index 9f53a4b..49184c0 100644 --- a/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day23.java +++ b/aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day23.java @@ -3,18 +3,29 @@ import java.nio.file.*; import java.util.*; import java.util.Map.Entry; +import java.util.concurrent.*; import java.util.function.*; public class Day23 { public static void main(String[] args) { var maze = readInput().lines().toList(); + Coord start = new Coord(1,0); Coord target = new Coord(maze.size()-2, maze.size()-1); - var longest = longestPathLength(maze, start, target, (m,n)->n.part1Neighbours(m), n->true); - System.out.println(longest); - WeightedGraph wg = new WeightedGraph(maze, start, target); - System.out.println(wg.longestPath(start, target)); + + ForkJoinTask part1Task = ForkJoinPool.commonPool().submit(()->{ + WeightedGraph part1Graph = new WeightedGraph(maze, start, target, (m,c)->c.part1Neighbours(m)); + return part1Graph.longestPath(start, target); + }); + + ForkJoinTask part2Task = ForkJoinPool.commonPool().submit(()->{ + WeightedGraph part1Graph = new WeightedGraph(maze, start, target, (m,c)->c.part2Neighbours(m)); + return part1Graph.longestPath(start, target); + }); + + System.out.println(part1Task.join()); + System.out.println(part2Task.join()); } private static String readInput() { @@ -136,7 +147,7 @@ private static class WeightedGraph { private Map connections = new LinkedHashMap(); private Map costs = new LinkedHashMap<>(); - public WeightedGraph(List maze, Coord start, Coord target) { + public WeightedGraph(List maze, Coord start, Coord target, BiFunction, Coord, List> walkableNeighbourExtractor) { forks.add(start); int id = 0; forkIds.put(start, id); @@ -146,7 +157,7 @@ public WeightedGraph(List maze, Coord start, Coord target) { char ch = line.charAt(x); if(ch != '#') { Coord c = new Coord(x,y); - if(c.part2Neighbours(maze).size()>2) { + if(walkableNeighbourExtractor.apply(maze, c).size()>2) { forks.add(c); forkIds.put(c, ++id); } @@ -161,7 +172,7 @@ public WeightedGraph(List maze, Coord start, Coord target) { for(Entry iEntry: forkIds.entrySet()) { for(Entry jEntry: forkIds.entrySet()) { if(iEntry.getValue() < jEntry.getValue()) { - int length = longestPathLength(maze, iEntry.getKey(), jEntry.getKey(), (m,n)->n.part2Neighbours(m), c->!forks.contains(c) || c.equals(jEntry.getKey())); + int length = longestPathLength(maze, iEntry.getKey(), jEntry.getKey(), walkableNeighbourExtractor, c->!forks.contains(c) || c.equals(jEntry.getKey())); if(length > 0) { connections.get(iEntry.getValue()).set(jEntry.getValue()); connections.get(jEntry.getValue()).set(iEntry.getValue());