Skip to content

java: add solutions for year 2016, day 12 #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
java_library(
name = "benchmark_library",
srcs = glob(["*.java"]),
deps = [
"@maven//:org_openjdk_jmh_jmh_core",
"//java/src/main/java/com/github/saser/adventofcode/year2016/day12:day12",
],
plugins = ["//java/src/benchmark:annotation_processor"],
data = ["//inputs:2016/12"],
)

java_binary(
name = "benchmark",
main_class = "org.openjdk.jmh.Main",
runtime_deps = [
"@maven//:org_openjdk_jmh_jmh_core",
":benchmark_library",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.saser.adventofcode.year2016.day12;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;

@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@Measurement(iterations = 1, time = 1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 1)
public class Day12Benchmark {
private Reader input;

@Setup
public void setup() throws IOException {
var path = FileSystems.getDefault().getPath("inputs", "2016", "12");
var contents = Files.readString(path);
this.input = new StringReader(contents);
}

@Benchmark
public void part1() throws IOException {
Day12.part1(this.input);
this.input.reset();
}

@Benchmark
public void part2() throws IOException {
Day12.part2(this.input);
this.input.reset();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//visibility:public"])

java_library(
name = "assembunny",
srcs = glob(["*.java"]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.github.saser.adventofcode.year2016.assembunny;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class VM {
public final String[] program;
public int pc;
public Map<String, Integer> registers;

public VM(String[] program, int pc, int a, int b, int c, int d) {
this.program = program;
this.pc = pc;
this.registers = new HashMap<>(Map.of("a", a, "b", b, "c", c, "d", d));
}

public VM(String[] program) {
this(program, 0, 0, 0, 0, 0);
}

public VM() {
this(new String[0]);
}

public static VM from(Reader r) {
return new VM(new BufferedReader(r).lines().toArray(String[]::new));
}

public static VM from(String program) {
return VM.from(new StringReader(program));
}

public int a() {
return this.registerValueOf("a").get();
}

public int b() {
return this.registerValueOf("b").get();
}

public int c() {
return this.registerValueOf("c").get();
}

public int d() {
return this.registerValueOf("d").get();
}

public void a(int i) {
this.registers.put("a", i);
}

public void b(int i) {
this.registers.put("b", i);
}

public void c(int i) {
this.registers.put("c", i);
}

public void d(int i) {
this.registers.put("d", i);
}

public void runAll() {
while (this.pc < this.program.length) {
this.run();
}
}

public void run() {
var instruction = this.program[this.pc];
var parts = instruction.split(" ", 2);
var op = parts[0];
var params = parts[1].split(" ");
var delta = 1;
switch (op) {
case "cpy":
this.registers.put(params[1], valueOf(params[0]));
break;
case "inc":
case "dec":
this.registers.merge(params[0], op.equals("inc") ? 1 : -1, Integer::sum);
break;
case "jnz":
if (valueOf(params[0]) != 0) {
delta = valueOf(params[1]);
}
break;
default:
throw new IllegalArgumentException(String.format("invalid op: %s", op));
}
this.pc += delta;
}

private int valueOf(String x) {
var immediate = this.immediateValueOf(x);
if (immediate.isPresent()) {
return immediate.get();
}
var register = this.registerValueOf(x);
if (register.isPresent()) {
return register.get();
}
throw new IllegalArgumentException(String.format("invalid parameter: %s", x));
}

private Optional<Integer> immediateValueOf(String x) {
try {
var i = Integer.parseInt(x);
return Optional.of(i);
} catch (NumberFormatException e) {
return Optional.empty();
}
}

private Optional<Integer> registerValueOf(String x) {
return Optional.ofNullable(this.registers.get(x));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package(default_visibility = ["//visibility:public"])

java_library(
name = "day12",
srcs = glob(["*.java"]),
deps = [
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
"//java/src/main/java/com/github/saser/adventofcode/year2016/assembunny:assembunny",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.saser.adventofcode.year2016.day12;

import java.io.Reader;

import com.github.saser.adventofcode.Result;
import com.github.saser.adventofcode.year2016.assembunny.VM;

public final class Day12 {
public static Result part1(Reader r) {
return solve(r, 1);
}

public static Result part2(Reader r) {
return solve(r, 2);
}

private static Result solve(Reader r, int part) {
int a = 1;
int b = 1;
int d = 26;
if (part == 2) {
d += 7;
}
var result = fibonacci(a, b, d) + 19 * 11;
return Result.ok(Integer.toString(result));
}

private static int fibonacci(int a, int b, int d) {
do {
int c = a;
a += b;
b = c;
d--;
} while (d != 0);
return a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
java_test(
name = "test",
srcs = glob(["*.java"]),
test_class = "com.github.saser.adventofcode.year2016.assembunny.VMTest",
resources = ["//java/src/test/resources/com/github/saser/adventofcode/year2016/assembunny:testdata"],
deps = [
"@maven//:junit_junit",
"//java/src/main/java/com/github/saser/adventofcode/year2016/assembunny",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.github.saser.adventofcode.year2016.assembunny;

import java.io.InputStreamReader;

import org.junit.Assert;
import org.junit.Test;

public class VMTest {
@Test
public void testCpyImmediate() {
var vm = VM.from("cpy 1 a");
vm.runAll();
Assert.assertEquals(1, vm.a());
}

@Test
public void testCpyRegister() {
var program = new String[] {
"cpy 1 a",
"cpy a b",
"cpy b c",
"cpy c d",
};
var vm = new VM(program);
vm.runAll();
Assert.assertEquals(1, vm.a());
Assert.assertEquals(1, vm.b());
Assert.assertEquals(1, vm.c());
Assert.assertEquals(1, vm.d());
}

@Test
public void testIncOnce() {
var vm = VM.from("inc a");
vm.runAll();
Assert.assertEquals(1, vm.a());
}

@Test
public void testIncTwice() {
var program = new String[] {
"inc a",
"inc a",
};
var vm = new VM(program);
vm.runAll();
Assert.assertEquals(2, vm.a());
}

@Test
public void testDecOnce() {
var vm = VM.from("dec a");
vm.runAll();
Assert.assertEquals(-1, vm.a());
}

@Test
public void testDecTwice() {
var program = new String[] {
"dec a",
"dec a",
};
var vm = new VM(program);
vm.runAll();
Assert.assertEquals(-2, vm.a());
}

@Test
public void testJnzZero() {
var program = new String[] {
"jnz 0 3",
"cpy 1 a",
"jnz a 2",
"cpy 2 a",
};
var vm = new VM(program);
vm.runAll();
Assert.assertEquals(1, vm.a());
}

@Test
public void testJnzNotZero() {
var program = new String[] {
"jnz 1 3",
"cpy 1 a",
"jnz a 2",
"cpy 2 a",
};
var vm = new VM(program);
vm.runAll();
Assert.assertEquals(2, vm.a());
}

@Test
public void testSetRegisters() {
var vm = new VM(new String[0], 0, 0, 0, 0, 0);
vm.a(1);
Assert.assertEquals(1, vm.a());
vm.b(2);
Assert.assertEquals(2, vm.b());
vm.c(3);
Assert.assertEquals(3, vm.c());
vm.d(4);
Assert.assertEquals(4, vm.d());
}

@Test
public void testDay12Example() {
var r = new InputStreamReader(this.getClass().getResourceAsStream("day12example"));
var vm = VM.from(r);
vm.runAll();
Assert.assertEquals(42, vm.a());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
java_test(
name = "test",
srcs = glob(["*.java"]),
test_class = "com.github.saser.adventofcode.year2016.day12.Day12Test",
deps = [
"@maven//:junit_junit",
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
"//java/src/main/java/com/github/saser/adventofcode/year2016/day12:day12",
],
data = ["//inputs:2016/12"],
)
Loading