Skip to content

Commit 7e437f1

Browse files
committed
java/2016/22: add solution for part 2
1 parent f850919 commit 7e437f1

File tree

6 files changed

+103
-19
lines changed

6 files changed

+103
-19
lines changed

java/src/main/java/com/github/saser/adventofcode/year2016/day22/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ java_library(
55
srcs = glob(["*.java"]),
66
deps = [
77
"//java/src/main/java/com/github/saser/adventofcode/geo:geo",
8+
"//java/src/main/java/com/github/saser/adventofcode/tuple:tuple",
89
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
910
],
1011
)

java/src/main/java/com/github/saser/adventofcode/year2016/day22/Day22.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import java.io.BufferedReader;
44
import java.io.Reader;
55
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
68
import java.util.Map;
79
import java.util.Set;
810
import java.util.regex.Pattern;
911
import java.util.stream.Collectors;
1012

1113
import com.github.saser.adventofcode.Result;
1214
import com.github.saser.adventofcode.geo.Point2D;
15+
import com.github.saser.adventofcode.tuple.Tuple2;
16+
import com.github.saser.adventofcode.tuple.Tuple3;
1317

1418
public final class Day22 {
1519
public static Result part1(Reader r) {
@@ -22,8 +26,20 @@ public static Result part2(Reader r) {
2226

2327
private static Result solve(Reader r, int part) {
2428
var grid = Grid.parse(r);
25-
var count = grid.findViable().size();
26-
return Result.ok(Integer.toString(count));
29+
if (part == 1) {
30+
var count = grid.findViable().size();
31+
return Result.ok(Integer.toString(count));
32+
}
33+
var maxX = grid.nodes
34+
.keySet()
35+
.stream()
36+
.mapToInt(point -> point.x)
37+
.max()
38+
.getAsInt();
39+
var from = new Point2D(maxX, 0);
40+
var to = new Point2D(0, 0);
41+
var steps = grid.moveData(from, to);
42+
return Result.ok(Integer.toString(steps));
2743
}
2844

2945
private static class Node {
@@ -97,5 +113,53 @@ public Set<Point2D> findViable() {
97113
.map(Map.Entry::getKey)
98114
.collect(Collectors.toSet());
99115
}
116+
117+
public int moveData(Point2D from, Point2D to) {
118+
var queue = new LinkedList<Tuple3<Point2D, Point2D, Integer>>();
119+
var empty = this.findEmpty();
120+
queue.add(new Tuple3<>(empty, from.clone(), 0));
121+
var visited = new HashSet<Tuple2<Point2D, Point2D>>();
122+
var maxX = 0;
123+
var maxY = 0;
124+
for (var point : this.nodes.keySet()) {
125+
maxX = Math.max(maxX, point.x);
126+
maxY = Math.max(maxY, point.y);
127+
}
128+
var viable = this.findViable();
129+
viable.add(empty);
130+
while (!queue.isEmpty()) {
131+
var tuple = queue.remove();
132+
var emptyPosition = tuple.v1;
133+
var dataPosition = tuple.v2;
134+
var state = new Tuple2<>(emptyPosition, dataPosition);
135+
if (visited.contains(state)) {
136+
continue;
137+
}
138+
visited.add(state);
139+
var steps = tuple.v3;
140+
if (dataPosition.equals(to)) {
141+
return steps;
142+
}
143+
var neighbors = new Point2D[] {
144+
emptyPosition.plus(new Point2D(1, 0)),
145+
emptyPosition.plus(new Point2D(-1, 0)),
146+
emptyPosition.plus(new Point2D(0, 1)),
147+
emptyPosition.plus(new Point2D(0, -1)),
148+
};
149+
for (var neighbor : neighbors) {
150+
var nx = neighbor.x;
151+
var ny = neighbor.y;
152+
if (nx < 0 || nx > maxX || ny < 0 || ny > maxY) {
153+
continue;
154+
}
155+
if (!viable.contains(neighbor)) {
156+
continue;
157+
}
158+
var newDataPosition = (neighbor.equals(dataPosition) ? emptyPosition : dataPosition).clone();
159+
queue.add(new Tuple3<>(neighbor, newDataPosition, steps + 1));
160+
}
161+
}
162+
return -1;
163+
}
100164
}
101165
}

java/src/test/java/com/github/saser/adventofcode/year2016/day22/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ java_test(
22
name = "test",
33
srcs = glob(["*.java"]),
44
test_class = "com.github.saser.adventofcode.year2016.day22.Day22Test",
5+
resources = ["//java/src/test/resources/com/github/saser/adventofcode/year2016/day22:testdata"],
56
deps = [
67
"@maven//:junit_junit",
78
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",

java/src/test/java/com/github/saser/adventofcode/year2016/day22/Day22Test.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.FileReader;
44
import java.io.IOException;
5+
import java.io.InputStreamReader;
56

67
import org.junit.Test;
78
import org.junit.Assert;
@@ -17,22 +18,22 @@ public void part1Actual() throws IOException {
1718
}
1819
}
1920

20-
// @Test
21-
// public void part2Example() {
22-
// var input = new StringReader("");
23-
// var output = "";
24-
// var result = Day22.part2(input);
25-
// Assert.assertEquals("no error", "", result.error);
26-
// Assert.assertEquals("correct output", output, result.answer);
27-
// }
21+
@Test
22+
public void part2Example() {
23+
var input = new InputStreamReader(this.getClass().getResourceAsStream("example"));
24+
var output = "7";
25+
var result = Day22.part2(input);
26+
Assert.assertEquals("no error", "", result.error);
27+
Assert.assertEquals("correct output", output, result.answer);
28+
}
2829

29-
// @Test
30-
// public void part2Actual() throws IOException {
31-
// try (var input = new FileReader("inputs/2016/22")) {
32-
// var output = "";
33-
// var result = Day22.part2(input);
34-
// Assert.assertEquals("no error", "", result.error);
35-
// Assert.assertEquals("correct output", output, result.answer);
36-
// }
37-
// }
30+
@Test
31+
public void part2Actual() throws IOException {
32+
try (var input = new FileReader("inputs/2016/22")) {
33+
var output = "236";
34+
var result = Day22.part2(input);
35+
Assert.assertEquals("no error", "", result.error);
36+
Assert.assertEquals("correct output", output, result.answer);
37+
}
38+
}
3839
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
filegroup(
4+
name = "testdata",
5+
testonly = 1,
6+
srcs = glob(["*"]),
7+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Filesystem Size Used Avail Use%
2+
/dev/grid/node-x0-y0 10T 8T 2T 80%
3+
/dev/grid/node-x0-y1 11T 6T 5T 54%
4+
/dev/grid/node-x0-y2 32T 28T 4T 87%
5+
/dev/grid/node-x1-y0 9T 7T 2T 77%
6+
/dev/grid/node-x1-y1 8T 0T 8T 0%
7+
/dev/grid/node-x1-y2 11T 7T 4T 63%
8+
/dev/grid/node-x2-y0 10T 6T 4T 60%
9+
/dev/grid/node-x2-y1 9T 8T 1T 88%
10+
/dev/grid/node-x2-y2 9T 6T 3T 66%

0 commit comments

Comments
 (0)