Skip to content

Commit

Permalink
Execute part1 and 2 parallel in Day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
zebalu committed Dec 27, 2023
1 parent 05c2777 commit c651b9b
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions aoc2023/src/main/java/io/github/zebalu/aoc2023/days/Day23.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> part1Task = ForkJoinPool.commonPool().submit(()->{
WeightedGraph part1Graph = new WeightedGraph(maze, start, target, (m,c)->c.part1Neighbours(m));
return part1Graph.longestPath(start, target);
});

ForkJoinTask<Integer> 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() {
Expand Down Expand Up @@ -136,7 +147,7 @@ private static class WeightedGraph {
private Map<Integer, BitSet> connections = new LinkedHashMap<Integer, BitSet>();
private Map<Edge, Integer> costs = new LinkedHashMap<>();

public WeightedGraph(List<String> maze, Coord start, Coord target) {
public WeightedGraph(List<String> maze, Coord start, Coord target, BiFunction<List<String>, Coord, List<Coord>> walkableNeighbourExtractor) {
forks.add(start);
int id = 0;
forkIds.put(start, id);
Expand All @@ -146,7 +157,7 @@ public WeightedGraph(List<String> 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);
}
Expand All @@ -161,7 +172,7 @@ public WeightedGraph(List<String> maze, Coord start, Coord target) {
for(Entry<Coord, Integer> iEntry: forkIds.entrySet()) {
for(Entry<Coord, Integer> 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());
Expand Down

0 comments on commit c651b9b

Please sign in to comment.