-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
169 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package net.bqc.aoc.year2024; | ||
|
||
import net.bqc.aoc.Solution; | ||
|
||
import net.bqc.aoc.utils.GraphUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Day21 extends Solution<Long> { | ||
|
||
private final static char[][] NUMPAD = new char[][]{ | ||
{'7', '8', '9'}, | ||
{'4', '5', '6'}, | ||
{'1', '2', '3'}, | ||
{' ', '0', 'A'} | ||
}; | ||
|
||
private final static char[][] DPAD = new char[][]{ | ||
{' ', '^', 'A'}, | ||
{'<', 'v', '>'} | ||
}; | ||
|
||
// Saving <String, String> occupies much more space and might cause heap out of memory | ||
private final static HashMap<String, Long> memorization = new HashMap<>(); | ||
|
||
@Override | ||
public Long solve(Part part, List<String> inputLines) { | ||
super.solve(part, inputLines); | ||
return inputLines.stream() | ||
.map(l -> Long.parseLong(l.substring(0, l.length() - 1)) * getMinCodeLength(NUMPAD, l, isPart2() ? 26 : 3)) | ||
.reduce(0L, Long::sum); | ||
} | ||
|
||
private long getMinCodeLength(char[][] pad, String code, int depth) { | ||
String key = code + "-" + depth; | ||
if (memorization.containsKey(key)) return memorization.get(key); | ||
|
||
long length = 0L; | ||
char startCh = 'A'; | ||
for (char codeCh : code.toCharArray()) { | ||
List<String> paths = shortestPaths(pad, startCh, codeCh); | ||
long minLength = Long.MAX_VALUE; | ||
if (depth == 1) { | ||
minLength = paths.get(0).length(); | ||
} | ||
else { | ||
for (String path : paths) { | ||
minLength = Math.min(minLength, getMinCodeLength(DPAD, path, depth - 1)); | ||
} | ||
} | ||
length += minLength; | ||
startCh = codeCh; | ||
} | ||
memorization.put(key, length); | ||
return length; | ||
} | ||
|
||
private List<String> shortestPaths(char[][] pad, char start, char end) { | ||
return GraphUtils.shortestPaths(pad, start, end, ' ').stream() | ||
.map(p -> p.stream().map(n -> n.direction.symbol).skip(1).collect(Collectors.joining("")) + "A") | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.bqc.aoc.year2024; | ||
|
||
import net.bqc.aoc.AbstractTest; | ||
import net.bqc.aoc.Solution; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Day21Test extends AbstractTest { | ||
|
||
private Day21 solution; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
INPUT_PATH = "year2024/Day21_Input.txt"; | ||
SAMPLE_INPUT_PATH = "year2024/Day21_SampleInput.txt"; | ||
} | ||
|
||
@BeforeEach | ||
void init() { | ||
solution = new Day21(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("inputDataSource") | ||
void testSolver(Solution.Part part, List<String> inputLines, long expectedOutput) { | ||
long computedOutput = solution.solve(part, inputLines); | ||
assertEquals(expectedOutput, computedOutput); | ||
} | ||
|
||
static Stream<Arguments> inputDataSource() { | ||
List<String> sampleInputLines = getSampleInput(); | ||
List<String> inputLines = getInput(); | ||
return Stream.of( | ||
Arguments.of(Solution.Part.ONE, sampleInputLines, 126384), | ||
Arguments.of(Solution.Part.ONE, inputLines, 138764), | ||
|
||
Arguments.of(Solution.Part.TWO, sampleInputLines, 154115708116294L), | ||
Arguments.of(Solution.Part.TWO, inputLines, 169137886514152L) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
140A | ||
180A | ||
176A | ||
805A | ||
638A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
029A | ||
980A | ||
179A | ||
456A | ||
379A |