-
Notifications
You must be signed in to change notification settings - Fork 2
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
10 changed files
with
181 additions
and
183 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
130 changes: 127 additions & 3 deletions
130
adventofcode/src/main/java/org/ck/adventofcode/year2016/Day24.java
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 |
---|---|---|
@@ -1,18 +1,142 @@ | ||
package org.ck.adventofcode.year2016; | ||
|
||
import java.util.*; | ||
import java.util.function.BiFunction; | ||
import java.util.stream.Collectors; | ||
import org.ck.adventofcode.util.AOCSolution; | ||
import org.ck.codechallengelib.annotation.Solution; | ||
|
||
@Solution( | ||
id = 20162401, | ||
name = "Day 24: Air Duct Spelunking", | ||
url = "https://adventofcode.com/2016/day/24", | ||
category = "2016") | ||
@Solution( | ||
id = 20162402, | ||
name = "Day 24: Air Duct Spelunking - Part 2", | ||
url = "https://adventofcode.com/2016/day/24", | ||
category = "2016") | ||
public class Day24 extends AOCSolution { | ||
@Override | ||
protected void runPartOne(final Scanner in) { | ||
run(in); | ||
run(in, (lastStep, distances) -> 0); | ||
} | ||
|
||
@Override | ||
protected void runPartTwo(final Scanner in) { | ||
run(in); | ||
run(in, (lastStep, distances) -> distances.get('0').get(lastStep)); | ||
} | ||
|
||
private void run(final Scanner in) {} | ||
private void run( | ||
final Scanner in, | ||
final BiFunction<Character, Map<Character, Map<Character, Integer>>, Integer> | ||
lastStepLength) { | ||
final List<char[]> grid = new ArrayList<>(); | ||
|
||
while (in.hasNextLine()) { | ||
grid.add(in.nextLine().toCharArray()); | ||
} | ||
|
||
final List<Target> wanted = new ArrayList<>(); | ||
for (int y = 0; y < grid.size(); ++y) { | ||
for (int x = 0; x < grid.get(y).length; ++x) { | ||
final char field = grid.get(y)[x]; | ||
|
||
if (field != '.' && field != '#') { | ||
wanted.add(new Target(field, new Coordinate(x, y))); | ||
} | ||
} | ||
} | ||
|
||
final Map<Character, Map<Character, Integer>> distances = new HashMap<>(); | ||
for (int i = 0; i < wanted.size(); ++i) { | ||
for (int j = i + 1; j < wanted.size(); ++j) { | ||
final int steps = | ||
findPathLength(grid, wanted.get(i).coordinate(), wanted.get(j).coordinate()); | ||
|
||
distances | ||
.computeIfAbsent(wanted.get(i).name(), k -> new HashMap<>()) | ||
.computeIfAbsent(wanted.get(j).name(), k -> steps); | ||
distances | ||
.computeIfAbsent(wanted.get(j).name(), k -> new HashMap<>()) | ||
.computeIfAbsent(wanted.get(i).name(), k -> steps); | ||
} | ||
} | ||
|
||
print( | ||
getOverallDistance( | ||
wanted.stream() | ||
.map(Target::name) | ||
.filter(target -> target != '0') | ||
.collect(Collectors.toSet()), | ||
distances, | ||
'0', | ||
lastStepLength)); | ||
} | ||
|
||
private int getOverallDistance( | ||
final Set<Character> remaining, | ||
final Map<Character, Map<Character, Integer>> distances, | ||
final char current, | ||
final BiFunction<Character, Map<Character, Map<Character, Integer>>, Integer> | ||
lastStepLength) { | ||
if (remaining.isEmpty()) { | ||
return lastStepLength.apply(current, distances); | ||
} | ||
|
||
int distance = Integer.MAX_VALUE; | ||
|
||
for (final char next : remaining) { | ||
distance = | ||
Math.min( | ||
distance, | ||
getOverallDistance( | ||
remaining.stream() | ||
.filter(target -> target != next) | ||
.collect(Collectors.toSet()), | ||
distances, | ||
next, | ||
lastStepLength) | ||
+ distances.get(current).get(next)); | ||
} | ||
|
||
return distance; | ||
} | ||
|
||
private static int findPathLength( | ||
final List<char[]> grid, final Coordinate start, final Coordinate end) { | ||
final Queue<State> queue = new PriorityQueue<>(Comparator.comparingInt(State::steps)); | ||
queue.add(new State(start, 0)); | ||
|
||
final Set<Coordinate> lookAheadCache = new HashSet<>(); | ||
|
||
while (!queue.isEmpty()) { | ||
final State state = queue.poll(); | ||
final Coordinate coordinate = state.coordinate(); | ||
|
||
if (coordinate.equals(end)) { | ||
return state.steps(); | ||
} | ||
|
||
for (final Coordinate next : | ||
List.of( | ||
new Coordinate(coordinate.x(), coordinate.y() - 1), | ||
new Coordinate(coordinate.x(), coordinate.y() + 1), | ||
new Coordinate(coordinate.x() - 1, coordinate.y()), | ||
new Coordinate(coordinate.x() + 1, coordinate.y()))) { | ||
if (grid.get(next.y())[next.x()] != '#' && !lookAheadCache.contains(next)) { | ||
lookAheadCache.add(next); | ||
queue.add(new State(next, state.steps() + 1)); | ||
} | ||
} | ||
} | ||
|
||
return Integer.MAX_VALUE; | ||
} | ||
|
||
private record Target(char name, Coordinate coordinate) {} | ||
|
||
private record Coordinate(int x, int y) {} | ||
|
||
private record State(Coordinate coordinate, int steps) {} | ||
} |
122 changes: 0 additions & 122 deletions
122
adventofcode/src/main/java/org/ck/adventofcode/year2016/day24/Part1.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
adventofcode/src/main/java/org/ck/adventofcode/year2016/day24/Part2.java
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
adventofcode/src/test/java/org/ck/adventofcode/year2016/Day24Test.java
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
15 changes: 0 additions & 15 deletions
15
adventofcode/src/test/java/org/ck/adventofcode/year2016/day24/Part1Test.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
adventofcode/src/test/java/org/ck/adventofcode/year2016/day24/Part2Test.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
adventofcode/src/test/resources/org/ck/adventofcode/year2016/day24/01.result.txt
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 |
---|---|---|
@@ -1 +1 @@ | ||
0 | ||
412 |
2 changes: 1 addition & 1 deletion
2
adventofcode/src/test/resources/org/ck/adventofcode/year2016/day24/02.result.txt
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 |
---|---|---|
@@ -1 +1 @@ | ||
0 | ||
664 |
Oops, something went wrong.