Skip to content

java: add solutions for year 2016, day 10 #100

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

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

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

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

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

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

import java.io.BufferedReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import com.github.saser.adventofcode.Result;

public final class Day10 {
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 network = Network.parse(r);
var paths = network.flow();
if (part == 1) {
var visited61 = new HashSet<>(paths.get(61));
var visited17 = new HashSet<>(paths.get(17));
visited61.retainAll(visited17);
var bot = visited61.iterator().next();
return Result.ok(Integer.toString(bot));
}
var product = 1;
var found = new boolean[3];
for (var entry : paths.entrySet()) {
var list = entry.getValue();
var output = list.get(list.size() - 1);
if (output >= 0 && output <= 2) {
if (found[output]) {
continue;
}
found[output] = true;
product *= entry.getKey();
}
if (found[0] && found[1] && found[2]) {
break;
}
}
return Result.ok(Integer.toString(product));
}

private static class Network {
public final Map<Integer, Sink> sources;
public final Map<Integer, Sink> highSinks;
public final Map<Integer, Sink> lowSinks;

public Network(Map<Integer, Sink> sources, Map<Integer, Sink> highSinks, Map<Integer, Sink> lowSinks) {
this.sources = sources;
this.highSinks = highSinks;
this.lowSinks = lowSinks;
}

public static Network parse(Reader r) {
var sources = new HashMap<Integer, Sink>();
var highSinks = new HashMap<Integer, Sink>();
var lowSinks = new HashMap<Integer, Sink>();
var br = new BufferedReader(r);
var it = br.lines().iterator();
var sourceRE = Pattern.compile("value (\\d+) goes to bot (\\d+)");
var highLowRE = Pattern.compile("bot (\\d+) gives (low|high) to (output|bot) (\\d+) and (low|high) to (output|bot) (\\d+)");
while (it.hasNext()) {
var line = it.next();
var sourceMatcher = sourceRE.matcher(line);
if (sourceMatcher.matches()) {
var value = Integer.parseInt(sourceMatcher.group(1));
var bot = Integer.parseInt(sourceMatcher.group(2));
sources.put(value, new Sink(bot, false));
continue;
}
var highLowMatcher = highLowRE.matcher(line);
if (highLowMatcher.matches()) {
var sourceBot = Integer.parseInt(highLowMatcher.group(1));
var isLow1 = highLowMatcher.group(2).equals("low");
var isOutput1 = highLowMatcher.group(3).equals("output");
var number1 = Integer.parseInt(highLowMatcher.group(4));
(isLow1 ? lowSinks : highSinks).put(sourceBot, new Sink(number1, isOutput1));
var isLow2 = highLowMatcher.group(5).equals("low");
var isOutput2 = highLowMatcher.group(6).equals("output");
var number2 = Integer.parseInt(highLowMatcher.group(7));
(isLow2 ? lowSinks : highSinks).put(sourceBot, new Sink(number2, isOutput2));
continue;
}
throw new IllegalArgumentException(String.format("invalid line: %s", line));
}
return new Network(sources, highSinks, lowSinks);
}

public Map<Integer, List<Integer>> flow() {
var q = new LinkedList<Pair<Integer, Sink>>();
for (var entry : this.sources.entrySet()) {
q.add(new Pair<>(entry.getKey(), entry.getValue()));
}
var paths = new HashMap<Integer, List<Integer>>();
var held = new HashMap<Integer, Integer>();
while (!q.isEmpty()) {
var entry = q.removeFirst();
var value = entry.first;
var sink = entry.second;
if (!paths.containsKey(value)) {
paths.put(value, new LinkedList<>());
}
paths.get(value).add(sink.target);
if (sink.isOutput) {
continue;
}
var bot = sink.target;
if (!held.containsKey(bot)) {
held.put(bot, value);
continue;
}
var current = held.get(bot);
var low = Math.min(current, value);
var high = Math.max(current, value);
q.addLast(new Pair<>(low, lowSinks.get(bot)));
q.addLast(new Pair<>(high, highSinks.get(bot)));
}
return paths;
}

private static class Pair<T1, T2> {
public final T1 first;
public final T2 second;

public Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}
}
}

private static class Sink {
public final int target;
public final boolean isOutput;

public Sink(int target, boolean isOutput) {
this.target = target;
this.isOutput = isOutput;
}
}
}
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.day10.Day10Test",
deps = [
"@maven//:junit_junit",
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
"//java/src/main/java/com/github/saser/adventofcode/year2016/day10:day10",
],
data = ["//inputs:2016/10"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.saser.adventofcode.year2016.day10;

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

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

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