Skip to content

java: add solutions for year 2016, day 21 #113

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 3 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/day21:day21",
],
plugins = ["//java/src/benchmark:annotation_processor"],
data = ["//inputs:2016/21"],
)

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

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

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

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

@Benchmark
public void part2() throws IOException {
Day21.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 = "day21",
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,174 @@
package com.github.saser.adventofcode.year2016.day21;

import java.io.BufferedReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.github.saser.adventofcode.Result;

public final class Day21 {
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 chars = (part == 1 ? "abcdefgh" : "fbgdceah").toCharArray();
var instructions = new BufferedReader(r)
.lines()
.collect(Collectors.toList());
apply(chars, instructions, part == 2);
return Result.ok(new String(chars));
}

private static void apply(char[] chars, List<String> instructions, boolean reverse) {
var swapPositionRE = Pattern.compile("swap position (\\d+) with position (\\d+)");
var swapLetterRE = Pattern.compile("swap letter (\\w) with letter (\\w)");
var rotateStepsRE = Pattern.compile("rotate (left|right) (\\d+) steps?");
var rotateLetterRE = Pattern.compile("rotate based on position of letter (\\w)");
var reverseRE = Pattern.compile("reverse positions (\\d+) through (\\d+)");
var moveRE = Pattern.compile("move position (\\d+) to position (\\d+)");
if (reverse) {
Collections.reverse(instructions);
}
for (var instruction : instructions) {
Matcher matcher;
matcher = swapPositionRE.matcher(instruction);
if (matcher.matches()) {
var x = Integer.parseInt(matcher.group(1));
var y = Integer.parseInt(matcher.group(2));
if (reverse) {
swap(chars, y, x);
} else {
swap(chars, x, y);
}
continue;
}
matcher = swapLetterRE.matcher(instruction);
if (matcher.matches()) {
var x = find(chars, matcher.group(1).charAt(0));
var y = find(chars, matcher.group(2).charAt(0));
if (reverse) {
swap(chars, y, x);
} else {
swap(chars, x, y);
}
continue;
}
matcher = rotateStepsRE.matcher(instruction);
if (matcher.matches()) {
var right = matcher.group(1).equals("right");
var steps = Integer.parseInt(matcher.group(2));
if (reverse) {
right = !right;
}
rotate(chars, right, steps);
continue;
}
matcher = rotateLetterRE.matcher(instruction);
if (matcher.matches()) {
var index = find(chars, matcher.group(1).charAt(0));
int steps;
if (reverse) {
if (index == 0) {
steps = 1;
} else if (index % 2 == 1) {
steps = index / 2 + 1;
} else {
steps = 5 + index / 2;
}
} else {
steps = 1 + index;
if (index >= 4) {
steps++;
}
}
rotate(chars, !reverse, steps);
continue;
}
matcher = reverseRE.matcher(instruction);
if (matcher.matches()) {
var x = Integer.parseInt(matcher.group(1));
var y = Integer.parseInt(matcher.group(2));
reverse(chars, x, y);
continue;
}
matcher = moveRE.matcher(instruction);
if (matcher.matches()) {
var x = Integer.parseInt(matcher.group(1));
var y = Integer.parseInt(matcher.group(2));
if (reverse) {
move(chars, y, x);
} else {
move(chars, x, y);
}
continue;
}
throw new IllegalArgumentException(String.format("invalid instruction: %s", instruction));
}
}

private static void swap(char[] chars, int x, int y) {
var temp = chars[x];
chars[x] = chars[y];
chars[y] = temp;
}

private static void rotate(char[] chars, boolean right, int steps) {
var copy = chars.clone();
var n = chars.length;
var delta = right ? steps : n - steps;
for (var i = 0; i < n; i++) {
chars[(i + delta) % n] = copy[i];
}
}

private static void reverse(char[] chars, int x, int y) {
for (int i = x, j = y; i < j; i++, j--) {
swap(chars, i, j);
}
}

private static void move(char[] chars, int from, int to) {
var copy = chars.clone();
var c = chars[from];
int low, high, d;
if (from < to) {
low = from;
high = to;
d = 1;
} else {
low = to + 1;
high = from + 1;
d = -1;
}
for (var i = 0; i < chars.length; i++) {
if (i == to) {
chars[i] = c;
continue;
}
var ci = i;
if (i >= low && i < high) {
ci += d;
}
chars[i] = copy[ci];
}
}

private static int find(char[] chars, char c) {
for (var i = 0; i < chars.length; i++) {
if (chars[i] == c) {
return i;
}
}
return -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.day21.Day21Test",
deps = [
"@maven//:junit_junit",
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
"//java/src/main/java/com/github/saser/adventofcode/year2016/day21:day21",
],
data = ["//inputs:2016/21"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.saser.adventofcode.year2016.day21;

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

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

public class Day21Test {
@Test
public void part1Actual() throws IOException {
try (var input = new FileReader("inputs/2016/21")) {
var output = "bfheacgd";
var result = Day21.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/21")) {
var output = "gcehdbfa";
var result = Day21.part2(input);
Assert.assertEquals("no error", "", result.error);
Assert.assertEquals("correct output", output, result.answer);
}
}
}