Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active December 10, 2017 07:14
Show Gist options
  • Save snarkbait/d30a6ae69a8bdf2a9937520ddca7cf2e to your computer and use it in GitHub Desktop.
Save snarkbait/d30a6ae69a8bdf2a9937520ddca7cf2e to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 10
package Advent2017;
import util.ArrayUtils;
import util.BitUtils;
import util.FileIO;
import util.Timer;
import java.util.Arrays;
import java.util.stream.IntStream;
public class Day10 {
private int pos;
private int skips;
private static final int[] SUFFIX = { 17, 31, 73, 47, 23 };
private String input;
private int[] lengths;
private int[] nums;
public Day10(String input) {
this.input = input;
}
private void reset() {
pos = 0;
skips = 0;
nums = IntStream.range(0, 256).toArray();
}
public void hash() {
for (int each : lengths) {
int[] temp = new int[each];
int cut = 0;
if (each <= nums.length - pos) {
cut = each;
} else {
cut = (nums.length - pos);
}
int leftover = each - cut;
System.arraycopy(nums, pos, temp, 0, cut);
System.arraycopy(nums, 0, temp, cut, leftover);
temp = ArrayUtils.reverse(temp);
// copy back into array
System.arraycopy(temp, 0, nums, pos, cut);
System.arraycopy(temp, each - leftover, nums, 0, leftover);
pos += each + skips;
pos %= nums.length;
skips++;
}
}
public int part1() {
lengths = FileIO.StringArrayToInt(input.split(","));
reset();
hash();
return nums[0] * nums[1];
}
public String part2() {
lengths = input.chars().toArray();
lengths = Arrays.copyOf(lengths, lengths.length + SUFFIX.length);
System.arraycopy(SUFFIX, 0, lengths, lengths.length - SUFFIX.length, SUFFIX.length);
reset();
for (int i = 0; i < 64; i++) {
hash();
}
return knotHash();
}
private String knotHash() {
int[] dense = new int[16];
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
dense[i] ^= nums[(i * 16) + j];
}
}
StringBuilder output = new StringBuilder();
for (int each : dense) {
output.append(BitUtils.toHexString(each, 8));
}
return output.toString();
}
public static void main(String[] args) {
String input = FileIO.getFileAsString("advent2017_day10.txt");
Timer.startTimer();
Day10 day10 = new Day10(input);
System.out.println("Part 1: " + day10.part1());
System.out.println("Part 2: " + day10.part2());
System.out.println(Timer.endTimer());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment