Skip to content

Instantly share code, notes, and snippets.

@roccodev
Last active December 10, 2017 11:55
Show Gist options
  • Select an option

  • Save roccodev/bb65b186dcd52fc800a886f202f9e96e to your computer and use it in GitHub Desktop.

Select an option

Save roccodev/bb65b186dcd52fc800a886f202f9e96e to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day10
List<String> bloccs = Files.readAllLines(Paths.get(new File("res/day10").getPath())).stream()
.collect(Collectors.toList());
String x = bloccs.get(0).trim();
String[] lengths = x.split(",");
int[] array = IntStream.range(0, 256).toArray();
int skipSize = 0;
int currentPos = 0;
for (String s : lengths) {
int i = Integer.parseInt(s);
int cut = 0;
int[] temp = new int[i];
if (i <= array.length - currentPos) {
cut = i;
} else {
cut = array.length - currentPos;
}
int leftover = i - cut;
System.out.println(currentPos + " / " + i);
System.arraycopy(array, currentPos, temp, 0, cut);
System.arraycopy(array, 0, temp, cut, leftover);
ArrayUtils.reverse(temp);
System.arraycopy(temp, 0, array, currentPos, cut);
System.arraycopy(temp, i - leftover, array, 0, leftover);
currentPos += (i + skipSize);
currentPos %= array.length;
skipSize++;
}
System.out.println(array[0] * array[1]);
}
public static void main(String[] args) throws IOException {
List<String> bloccs = Files.readAllLines(Paths.get(new File("res/day10").getPath())).stream()
.collect(Collectors.toList());
String x = bloccs.get(0).trim();
int[] lengths = toAscii(x);
int[] array = IntStream.range(0, 256).toArray();
int[] ascii = new int[255];
int skipSize = 0;
int currentPos = 0;
for(int i1 = 0; i1 < 64; i1++) {
for (int i : lengths) {
int cut = 0;
int[] temp = new int[i];
if (i <= array.length - currentPos) {
cut = i;
} else {
cut = array.length - currentPos;
}
int leftover = i - cut;
System.out.println(currentPos + " / " + i);
System.arraycopy(array, currentPos, temp, 0, cut);
System.arraycopy(array, 0, temp, cut, leftover);
ArrayUtils.reverse(temp);
System.arraycopy(temp, 0, array, currentPos, cut);
System.arraycopy(temp, i - leftover, array, 0, leftover);
currentPos += (i + skipSize);
currentPos %= array.length;
skipSize++;
}
}
int[] result = new int[16];
for(int a = 0; a < 16; a++) {
for(int b = 0; b < 16; b++) {
result[a] ^= array[a * 16 + b];
}
}
StringBuilder sb = new StringBuilder();
for(int i : result) {
String toAppend = Integer.toString(i, 16);
sb.append(toAppend.length() == 1 ? '0' + toAppend : toAppend);
}
System.out.println(sb.toString());
}
private static int[] toAscii(String src) {
int[] toAdd = {17, 31, 73, 47, 23};
return ArrayUtils.addAll(src.chars().toArray(), toAdd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment