Skip to content

java: add solutions for year 2016, day 13 #104

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 6 commits into from
Jan 8, 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/day13:day13",
],
plugins = ["//java/src/benchmark:annotation_processor"],
data = ["//inputs:2016/13"],
)

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.day13;

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 Day13Benchmark {
private Reader input;

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

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

@Benchmark
public void part2() throws IOException {
Day13.part2(this.input);
this.input.reset();
}
}
27 changes: 27 additions & 0 deletions java/src/main/java/com/github/saser/adventofcode/geo/Point2D.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.saser.adventofcode.geo;

import java.util.Objects;

public final class Point2D {
public int x;
public int y;
Expand All @@ -23,6 +25,31 @@ public void add(Point2D other) {
this.y += other.y;
}

public Point2D plus(Point2D other) {
var copy = this.clone();
copy.add(other);
return copy;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point2D point2D = (Point2D) o;
return x == point2D.x &&
y == point2D.y;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}

@Override
public String toString() {
return String.format("(%d, %d)", this.x, this.y);
}

@Override
public Point2D clone() {
return new Point2D(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//visibility:public"])

java_library(
name = "tuple",
srcs = glob(["*.java"]),
)
36 changes: 36 additions & 0 deletions java/src/main/java/com/github/saser/adventofcode/tuple/Tuple2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.saser.adventofcode.tuple;

import java.util.Objects;

public class Tuple2<T1, T2> {
public final T1 v1;
public final T2 v2;

public Tuple2(T1 v1, T2 v2) {
this.v1 = v1;
this.v2 = v2;
}

@Override
public String toString() {
var s = new String[] {
this.v1.toString(),
this.v2.toString(),
};
return "(" + String.join(", ", s) + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tuple2<?, ?> tuple2 = (Tuple2<?, ?>) o;
return v1.equals(tuple2.v1) &&
v2.equals(tuple2.v2);
}

@Override
public int hashCode() {
return Objects.hash(v1, v2);
}
}
40 changes: 40 additions & 0 deletions java/src/main/java/com/github/saser/adventofcode/tuple/Tuple3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.saser.adventofcode.tuple;

import java.util.Objects;

public class Tuple3<T1, T2, T3> {
public final T1 v1;
public final T2 v2;
public final T3 v3;

public Tuple3(T1 v1, T2 v2, T3 v3) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}

@Override
public String toString() {
var s = new String[] {
this.v1.toString(),
this.v2.toString(),
this.v3.toString(),
};
return "(" + String.join(", ", s) + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tuple3<?, ?, ?> tuple3 = (Tuple3<?, ?, ?>) o;
return v1.equals(tuple3.v1) &&
v2.equals(tuple3.v2) &&
v3.equals(tuple3.v3);
}

@Override
public int hashCode() {
return Objects.hash(v1, v2, v3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package(default_visibility = ["//visibility:public"])

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

import java.io.BufferedReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import com.github.saser.adventofcode.Result;
import com.github.saser.adventofcode.geo.Point2D;
import com.github.saser.adventofcode.tuple.Tuple2;

public final class Day13 {
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) {
try {
var br = new BufferedReader(r);
var input = br.readLine();
var favorite = Integer.parseInt(input);
var from = new Point2D(1, 1);
var target = new Point2D(31, 39);
var steps = Day13.steps(from, target, favorite);
if (part == 1) {
return Result.ok(Integer.toString(steps.get(target)));
}
var within50 = steps.values()
.stream()
.filter((length) -> length <= 50)
.count();
return Result.ok(Long.toString(within50));
} catch (Exception e) {
e.printStackTrace();
return Result.err(e.getMessage());
}
}

private static Map<Point2D, Integer> steps(Point2D from, Point2D target, int favorite) {
var queue = new LinkedList<Tuple2<Point2D, Integer>>();
queue.add(new Tuple2<>(from, 0));
var visited = new HashMap<Point2D, Integer>();
while (!queue.isEmpty()) {
var tuple = queue.remove();
var point = tuple.v1;
var steps = tuple.v2;
if (visited.containsKey(point)) {
continue;
}
visited.put(point, steps);
if (point.equals(target)) {
break;
}
var neighbors = new Point2D[] {
point.plus(new Point2D(1, 0)),
point.plus(new Point2D(-1, 0)),
point.plus(new Point2D(0, 1)),
point.plus(new Point2D(0, -1)),
};
for (var neighbor : neighbors) {
if (neighbor.x < 0 || neighbor.y < 0) {
continue;
}
if (!Day13.isWall(neighbor, favorite)) {
queue.add(new Tuple2<>(neighbor, steps + 1));
}
}
}
return visited;
}

private static boolean isWall(Point2D point, int favorite) {
var x = point.x;
var y = point.y;
return Integer.bitCount((x*x + 3*x + 2*x*y + y + y*y) + favorite) % 2 == 1;
}
}
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.day13.Day13Test",
deps = [
"@maven//:junit_junit",
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
"//java/src/main/java/com/github/saser/adventofcode/year2016/day13:day13",
],
data = ["//inputs:2016/13"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.saser.adventofcode.year2016.day13;

import java.io.FileReader;
import java.io.IOException;

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

public class Day13Test {
@Test
public void part1Actual() throws IOException {
try (var input = new FileReader("inputs/2016/13")) {
var output = "92";
var result = Day13.part1(input);
Assert.assertEquals("no error", "", result.error);
Assert.assertEquals("correct output", output, result.answer);
}
}

@Test
public void part2Actual() throws IOException {
try (var input = new FileReader("inputs/2016/13")) {
var output = "124";
var result = Day13.part2(input);
Assert.assertEquals("no error", "", result.error);
Assert.assertEquals("correct output", output, result.answer);
}
}
}