From 59c0c0c62fd63312040cb1222fd962fd639ed85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Belellou?= Date: Sat, 18 Jan 2025 15:57:01 +0100 Subject: [PATCH 1/3] Added 2016 Day 17 Moved to JDK 23 to use preview [JEP 480 - Structured Concurrency](https://openjdk.org/jeps/480) --- .github/workflows/maven.yml | 4 +- .idea/compiler.xml | 7 +- .idea/misc.xml | 4 +- pom.xml | 23 +++- .../belellou/kevin/advent/year2016/Day17.java | 125 ++++++++++++++++++ src/main/resources/2016/17/input.txt | 1 + 6 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/belellou/kevin/advent/year2016/Day17.java create mode 100644 src/main/resources/2016/17/input.txt diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 58a8f36..419cd7a 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -16,10 +16,10 @@ jobs: - name: "Checkout repository" uses: actions/checkout@v4 - - name: "Set up JDK 21" + - name: "Set up JDK 23" uses: actions/setup-java@v4 with: - java-version: '21' + java-version: '23' distribution: 'temurin' cache: maven diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 7f1c0ac..14b0122 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -10,4 +10,9 @@ - + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 154de2a..b4d0fd2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1653a6c..7f10348 100644 --- a/pom.xml +++ b/pom.xml @@ -19,8 +19,9 @@ - 21 - 21 + 23 + 23 + 23 UTF-8 @@ -37,6 +38,7 @@ 0.8.12 3.5.2 + 3.13.0 @@ -113,6 +115,23 @@ + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + + ${maven.compiler.source} + ${maven.compiler.target} + ${maven.compiler.release} + ${project.build.sourceEncoding} + + --enable-preview + + + + maven-surefire-plugin diff --git a/src/main/java/com/belellou/kevin/advent/year2016/Day17.java b/src/main/java/com/belellou/kevin/advent/year2016/Day17.java new file mode 100644 index 0000000..82c85de --- /dev/null +++ b/src/main/java/com/belellou/kevin/advent/year2016/Day17.java @@ -0,0 +1,125 @@ +package com.belellou.kevin.advent.year2016; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.StructuredTaskScope; +import java.util.concurrent.StructuredTaskScope.Subtask; +import java.util.function.BinaryOperator; + +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; + +import com.belellou.kevin.advent.generic.AbstractDaySolver; + +@SuppressWarnings({"unused", "preview"}) +public class Day17 extends AbstractDaySolver { + + private static final Position startingPosition = Position.of(0, 0); + private static final Position vaultPosition = Position.of(3, 3); + + public Day17() { + super(Day17.class); + } + + private static String findPath(Position position, String passcode, String path, boolean findShortestPath) + throws InterruptedException, ExecutionException { + if (position.equals(vaultPosition)) { + return path; + } + + try (var scope = new StructuredTaskScope.ShutdownOnFailure()) { + List> subtasks = new ArrayList<>(); + + String hash = DigestUtils.md5Hex(passcode + path).substring(0, 4); + + Arrays.stream(Direction.values()) + .filter(direction -> isDirectionAccessible(direction, position, hash)) + .forEach(direction -> { + Position newPosition = switch (direction) { + case UP -> Position.of(position.getLeft(), position.getRight() - 1); + case DOWN -> Position.of(position.getLeft(), position.getRight() + 1); + case LEFT -> Position.of(position.getLeft() - 1, position.getRight()); + case RIGHT -> Position.of(position.getLeft() + 1, position.getRight()); + }; + + subtasks.add(scope.fork(() -> findPath(newPosition, passcode, path + direction.name().charAt(0), + findShortestPath))); + }); + + scope.join().throwIfFailed(); + + Comparator comparator = Comparator.comparingInt(String::length); + BinaryOperator binaryOperator = + findShortestPath ? BinaryOperator.minBy(comparator) : BinaryOperator.maxBy(comparator); + + return subtasks.stream().map(Subtask::get).filter(Objects::nonNull).reduce(binaryOperator).orElse(null); + } + } + + private static boolean isDirectionAccessible(Direction direction, Position position, String hash) { + boolean doorOpen = isDoorOpen(hash.charAt(direction.ordinal())); + + return switch (direction) { + case UP -> doorOpen && position.getRight() != 0; + case DOWN -> doorOpen && position.getRight() != 3; + case LEFT -> doorOpen && position.getLeft() != 0; + case RIGHT -> doorOpen && position.getLeft() != 3; + }; + } + + private static boolean isDoorOpen(char c) { + return c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'; + } + + @Override + protected String doSolveFirstStar(BufferedReader reader) throws IOException { + try { + return findPath(startingPosition, reader.readLine(), "", true); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } + + @Override + public String getFirstStarSolution() { + return "RRRLDRDUDD"; + } + + @Override + protected Integer doSolveSecondStar(BufferedReader reader) throws IOException { + try { + return findPath(startingPosition, reader.readLine(), "", false).length(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } + + @Override + public Integer getSecondStarSolution() { + return 706; + } + + private enum Direction { + UP, + DOWN, + LEFT, + RIGHT + } + + private static class Position extends ImmutablePair { + + private Position(Integer left, Integer right) { + super(left, right); + } + + public static Position of(Integer left, Integer right) { + return new Position(left, right); + } + } +} diff --git a/src/main/resources/2016/17/input.txt b/src/main/resources/2016/17/input.txt new file mode 100644 index 0000000..286171c --- /dev/null +++ b/src/main/resources/2016/17/input.txt @@ -0,0 +1 @@ +qtetzkpl From 64cf80bc61a25bf74b50b4b27cadb446132bb642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Belellou?= Date: Sat, 18 Jan 2025 16:06:50 +0100 Subject: [PATCH 2/3] Enable preview for unit testing --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index 7f10348..9f5e842 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,7 @@ ${maven.compiler.release} ${project.build.sourceEncoding} + --enable-preview @@ -139,6 +140,9 @@ + + --enable-preview + false From 1a9d6afb83a077eb5669197972f71d79cc964256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Belellou?= Date: Sat, 18 Jan 2025 16:44:50 +0100 Subject: [PATCH 3/3] Fix JaCoCo not outputting .exec file --- pom.xml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 9f5e842..ca71528 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,10 @@ 23 UTF-8 + + + --enable-preview + 3.27.2 1.17.2 @@ -128,7 +132,7 @@ ${project.build.sourceEncoding} - --enable-preview + ${argLine} @@ -140,9 +144,6 @@ - - --enable-preview - false