Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 2016 Day 17 #20

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
</developers>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<maven.compiler.release>23</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Enable JEPs in preview -->
<!-- Must be declared here and not in surefire configuration to avoid erasing JaCoCo configuration -->
<argLine>--enable-preview</argLine>

<!-- Dependency versions -->
<assertj.version>3.27.2</assertj.version>
<commons-codec.version>1.17.2</commons-codec.version>
Expand All @@ -37,6 +42,7 @@
<!-- Plugin versions -->
<jacoco-maven-plugin.version>0.8.12</jacoco-maven-plugin.version>
<maven-surefire-plugin.version>3.5.2</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -113,6 +119,24 @@

<build>
<plugins>
<plugin>
<!-- Compiler configuration -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>

<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<release>${maven.compiler.release}</release>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgs>
<!-- Enable JEPs in preview -->
<arg>${argLine}</arg>
</compilerArgs>
</configuration>
</plugin>

<plugin>
<!-- Unit test execution -->
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
125 changes: 125 additions & 0 deletions src/main/java/com/belellou/kevin/advent/year2016/Day17.java
Original file line number Diff line number Diff line change
@@ -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<String, Integer> {

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<Subtask<String>> 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<String> comparator = Comparator.comparingInt(String::length);
BinaryOperator<String> 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<Integer, Integer> {

private Position(Integer left, Integer right) {
super(left, right);
}

public static Position of(Integer left, Integer right) {
return new Position(left, right);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/2016/17/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qtetzkpl
Loading