Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Created December 8, 2017 05:55
Show Gist options
  • Save snarkbait/7497bf906040f90789e14ab127b220d3 to your computer and use it in GitHub Desktop.
Save snarkbait/7497bf906040f90789e14ab127b220d3 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 8
package Advent2017;
import util.FileIO;
import util.Timer;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.ToIntBiFunction;
public class Day8 {
private static Map<String, BiPredicate<Integer, Integer>> comparisons = new HashMap<>();
static {
comparisons.put("==", (x, y) -> x.equals(y));
comparisons.put("!=", (x, y) -> !x.equals(y));
comparisons.put("<", (x, y) -> x < y);
comparisons.put(">", (x, y) -> x > y);
comparisons.put("<=", (x, y) -> x <= y);
comparisons.put(">=", (x, y) -> x >= y);
}
private static Map<String, ToIntBiFunction<Integer, Integer>> commands = new HashMap<>();
static {
commands.put("inc", (x, y) -> x + y);
commands.put("dec", (x, y) -> x - y);
}
public static void main(String[] args) {
List<String[]> input =FileIO.getFileLinesSplit("advent2017_day8.txt", " ");
Map<String, Integer> registers = new HashMap<>();
int highest = 0;
for (String[] each : input) {
String name = each[0];
String command = each[1];
int amount = Integer.parseInt(each[2]);
String testReg = each[4];
String comp = each[5];
int testAmt = Integer.parseInt(each[6]);
registers.putIfAbsent(name, 0);
if (comparisons.get(comp).test(registers.getOrDefault(testReg, 0), testAmt)) {
int current = registers.get(name);
registers.put(name, commands.get(command).applyAsInt(current, amount));
if (registers.get(name) > highest) {
highest = registers.get(name);
}
}
}
Timer.startNanoTimer();
System.out.println("Part 1: " + Collections.max(registers.values()));
System.out.println("Part 2: " + highest);
System.out.println(Timer.endTimer());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment