Skip to content

Commit 7db3bab

Browse files
committed
java/2016/12: add solution for part 2
This is done by implementing what the assembunny program is actually doing, but in Java. This means that the example test is no longer needed for day 12. However, it should still reside in the tests for the assembunny VM, as a sanity check.
1 parent faff257 commit 7db3bab

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

java/src/main/java/com/github/saser/adventofcode/year2016/day12/Day12.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,23 @@ public static Result part2(Reader r) {
1515
}
1616

1717
private static Result solve(Reader r, int part) {
18-
var vm = VM.from(r);
19-
vm.c(part == 2 ? 1 : 0);
20-
vm.runAll();
21-
return Result.ok(Integer.toString(vm.a()));
18+
int a = 1;
19+
int b = 1;
20+
int d = 26;
21+
if (part == 2) {
22+
d += 7;
23+
}
24+
var result = fibonacci(a, b, d) + 19 * 11;
25+
return Result.ok(Integer.toString(result));
26+
}
27+
28+
private static int fibonacci(int a, int b, int d) {
29+
do {
30+
int c = a;
31+
a += b;
32+
b = c;
33+
d--;
34+
} while (d != 0);
35+
return a;
2236
}
2337
}

java/src/test/java/com/github/saser/adventofcode/year2016/assembunny/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.assembunny.VMTest",
5+
resources = ["//java/src/test/resources/com/github/saser/adventofcode/year2016/assembunny:testdata"],
56
deps = [
67
"@maven//:junit_junit",
78
"//java/src/main/java/com/github/saser/adventofcode/year2016/assembunny",

java/src/test/java/com/github/saser/adventofcode/year2016/assembunny/VMTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.saser.adventofcode.year2016.assembunny;
22

3+
import java.io.InputStreamReader;
4+
35
import org.junit.Assert;
46
import org.junit.Test;
57

@@ -101,4 +103,12 @@ public void testSetRegisters() {
101103
vm.d(4);
102104
Assert.assertEquals(4, vm.d());
103105
}
106+
107+
@Test
108+
public void testDay12Example() {
109+
var r = new InputStreamReader(this.getClass().getResourceAsStream("day12example"));
110+
var vm = VM.from(r);
111+
vm.runAll();
112+
Assert.assertEquals(42, vm.a());
113+
}
104114
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
java_test(
22
name = "test",
33
srcs = glob(["*.java"]),
4-
resources = ["//java/src/test/resources/com/github/saser/adventofcode/year2016/day12:testdata"],
54
test_class = "com.github.saser.adventofcode.year2016.day12.Day12Test",
65
deps = [
76
"@maven//:junit_junit",

java/src/test/java/com/github/saser/adventofcode/year2016/day12/Day12Test.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@
22

33
import java.io.FileReader;
44
import java.io.IOException;
5-
import java.io.InputStreamReader;
65

76
import org.junit.Test;
87
import org.junit.Assert;
98

109
public class Day12Test {
11-
@Test
12-
public void part1Example() {
13-
var input = new InputStreamReader(this.getClass().getResourceAsStream("example"));
14-
var output = "42";
15-
var result = Day12.part1(input);
16-
Assert.assertEquals("no error", "", result.error);
17-
Assert.assertEquals("correct output", output, result.answer);
18-
}
19-
2010
@Test
2111
public void part1Actual() throws IOException {
2212
try (var input = new FileReader("inputs/2016/12")) {
@@ -30,7 +20,7 @@ public void part1Actual() throws IOException {
3020
@Test
3121
public void part2Actual() throws IOException {
3222
try (var input = new FileReader("inputs/2016/12")) {
33-
var output = "";
23+
var output = "9227674";
3424
var result = Day12.part2(input);
3525
Assert.assertEquals("no error", "", result.error);
3626
Assert.assertEquals("correct output", output, result.answer);

0 commit comments

Comments
 (0)