Skip to content

Commit f9a5bc2

Browse files
committed
java/2016/24: add solution for part 2
1 parent a520d40 commit f9a5bc2

File tree

2 files changed

+14
-20
lines changed
  • java/src
    • main/java/com/github/saser/adventofcode/year2016/day24
    • test/java/com/github/saser/adventofcode/year2016/day24

2 files changed

+14
-20
lines changed

java/src/main/java/com/github/saser/adventofcode/year2016/day24/Day24.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private static Result solve(Reader r, int part) {
2929
var keys = findKeys(grid);
3030
var start = findStart(grid, keys);
3131
var distances = findDistances(grid, keys);
32-
var steps = collectKeys(start, keys, distances);
32+
var steps = collectKeys(start, keys, distances, part == 2);
3333
return Result.ok(Integer.toString(steps));
3434
}
3535

@@ -112,7 +112,7 @@ private static Map<Point2D, Map<Point2D, Integer>> findDistances(char[][] grid,
112112
return distances;
113113
}
114114

115-
private static int collectKeys(Point2D start, Set<Point2D> keys, Map<Point2D, Map<Point2D, Integer>> distances) {
115+
private static int collectKeys(Point2D start, Set<Point2D> keys, Map<Point2D, Map<Point2D, Integer>> distances, boolean returnToStart) {
116116
var queue = new PriorityQueue<Tuple3<Point2D, Set<Point2D>, Integer>>(Comparator.comparing(tuple -> tuple.v3));
117117
queue.add(new Tuple3<>(start, Set.of(start), 0));
118118
var visited = new HashSet<Tuple2<Point2D, Set<Point2D>>>();
@@ -127,6 +127,9 @@ private static int collectKeys(Point2D start, Set<Point2D> keys, Map<Point2D, Ma
127127
}
128128
visited.add(state);
129129
if (collected.containsAll(keys)) {
130+
if (returnToStart) {
131+
steps += distances.get(point).get(start);
132+
}
130133
return steps;
131134
}
132135
for (var entry : distances.get(point).entrySet()) {

java/src/test/java/com/github/saser/adventofcode/year2016/day24/Day24Test.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,13 @@ public void part1Actual() throws IOException {
2727
}
2828
}
2929

30-
// @Test
31-
// public void part2Example() {
32-
// var input = new StringReader("");
33-
// var output = "";
34-
// var result = Day24.part2(input);
35-
// Assert.assertEquals("no error", "", result.error);
36-
// Assert.assertEquals("correct output", output, result.answer);
37-
// }
38-
39-
// @Test
40-
// public void part2Actual() throws IOException {
41-
// try (var input = new FileReader("inputs/2016/24")) {
42-
// var output = "";
43-
// var result = Day24.part2(input);
44-
// Assert.assertEquals("no error", "", result.error);
45-
// Assert.assertEquals("correct output", output, result.answer);
46-
// }
47-
// }
30+
@Test
31+
public void part2Actual() throws IOException {
32+
try (var input = new FileReader("inputs/2016/24")) {
33+
var output = "720";
34+
var result = Day24.part2(input);
35+
Assert.assertEquals("no error", "", result.error);
36+
Assert.assertEquals("correct output", output, result.answer);
37+
}
38+
}
4839
}

0 commit comments

Comments
 (0)