Skip to content

java: add solution for year 2016, day 19 #110

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

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

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

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

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

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

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

import java.io.BufferedReader;
import java.io.Reader;

import com.github.saser.adventofcode.Result;

public final class Day19 {
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) {
var input = new BufferedReader(r)
.lines()
.findFirst()
.get();
var n = Integer.parseInt(input);
int winner;
if (part == 1) {
winner = play1(n);
} else {
winner = play2(n);
}
return Result.ok(Integer.toString(winner));
}

// I found this solution after several insights.
// What I did first was that I wrote the elves using zero-indexing, instead
// of 1-indexing. So for a ring with `n` elves, they are numbered as 0
// through `n - 1`.
// My first insight was that every other elf is removed, meaning that the
// size of the ring is halved after one "loop" (loop meaning that the elves
// are about to wrap around to the beginning of the ring, i.e., elf 0). What
// happens when `n` is odd, then? It turns out that if `n` is odd, the last
// elf in the ring (with the number `n - 1`) will play, removing elf
// 0. Then, we have a new ring with an even number of elves, but where the
// starting elf has number 2 and the elves' numbers increase by 2, instead
// of 1. I noticed that this was a new instance of the same problem, but
// with a new starting number and a new increase. So we can just solve it
// recursively! This was my second insight.
//
// Call the number of the starting elf `s`, and the difference between the
// numbers of the elves is `d`. The number of elves in the ring is `n`. If
// `n` is odd, then elf `s` will eventually be removed, yielding a new ring
// starting at `s + d`, and with a difference of `2 * d`. I wrote something
// along the lines of this pseudocode for my first solution:
//
// s := 0
// d := 2
// while (n > 1) {
// if (n % 2 == 1) {
// s += d
// }
// d *= 2
// n /= 2
// }
// return s + 1
//
// But wait, I recognized this... This is just summing up bits in the binary
// representation of `n`! But something is a little off: the first bit
// (meaning, the first iteration of the `while` loop) represents `2^1`, and
// we ignore the last bit since the loop terminates when `n <= 1`. But then
// we add 1 to the final result, so that is where the last bit comes back!
// What we have done is basically a rotation of the bits in `n`: the most
// significant bit is shifted around and becomes the least significant
// bit. So if we can do that rotation, we are done! This was my third insight.
//
// Since `int`s are signed 32-bit integers in Java, we have to find the most
// significant bit, call it `k`. Bits are 1-indexed: the first bit, meaning
// the bit that represents `2^0`, has index 1. `Integer.highestOneBit`
// returns a number equal to `1 << k`. We can remove the most significant
// bit by simply calculating `n - k`. Then we shift the resulting number
// left one step (this is where the rotation happens), and then we add on
// the shifted out bit as the least significant bit (which is what `+ 1`
// represents).
private static int play1(int n) {
int k = Integer.highestOneBit(n);
return ((n - k) << 1) + 1;
}

// After trying out my original solution for all inputs up to 100, I started
// to notice a pattern. Excerpts from the output (`n` is left, answer is right):
// ...
// 7 -> 5
// 8 -> 7
// 9 -> 9
// 10 -> 1
// 11 -> 2
// ...
// 17 -> 8
// 18 -> 9
// 19 -> 11
// 20 -> 13
// ...
// My conclusion was this: if `n` is equal to a power of 3, then `n` is the
// winner. Otherwise, calculate the largest power of 3 less than `n`, and
// call that `p`. If `n <= 2 * p`, then the answer is `n - p`. Otherwise,
// meaning `n > 2 * p`, then the answer was `n - p + (n % (2 * p))`. Since
// `n > 2 * p`, then `n % (2 * p) = n - 2 * p`, so we can simplify:
// n - p + (n % 2 * p)
// -> n - p + (n - 2 * p).
// Therefore, the answer is `n - p + max(n - 2 * p, 0)`.
// I am not sure how to prove that this is the correct answer, but it turns
// out that it is.
private static int play2(int n) {
var p = (int) (Math.pow(3, (int) (Math.log(n) / Math.log(3))));
if (p == n) {
return n;
}
return n - p + Math.max(n - 2 * p, 0);
}
}
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.day19.Day19Test",
deps = [
"@maven//:junit_junit",
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
"//java/src/main/java/com/github/saser/adventofcode/year2016/day19:day19",
],
data = ["//inputs:2016/19"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.saser.adventofcode.year2016.day19;

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

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

public class Day19Test {
@Test
public void part1Example() {
var input = new StringReader("5");
var output = "3";
var result = Day19.part1(input);
Assert.assertEquals("no error", "", result.error);
Assert.assertEquals("correct output", output, result.answer);
}

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

@Test
public void part2Example() {
var input = new StringReader("5");
var output = "2";
var result = Day19.part2(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/19")) {
var output = "1417887";
var result = Day19.part2(input);
Assert.assertEquals("no error", "", result.error);
Assert.assertEquals("correct output", output, result.answer);
}
}
}