Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2016 08:34
Show Gist options
  • Save anonymous/9152fa5b8a83bcadd8529d5f4550c05a to your computer and use it in GitHub Desktop.
Save anonymous/9152fa5b8a83bcadd8529d5f4550c05a to your computer and use it in GitHub Desktop.
Advent of Code 2016 Day 21 /u/Philboyd_Studge
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
/**
* @author /u/Philboyd_Studge on 12/20/2016.
*/
public class Advent2016_21 {
enum Command {
swap(Advent2016_21::swap),
move(Advent2016_21::move),
rotate_right(Advent2016_21::rotate_right),
rotate_left(Advent2016_21::rotate_left),
rotate_based(Advent2016_21::rotate_based),
reverse(Advent2016_21::reverse);
BiConsumer<Integer, Integer> func;
Command(BiConsumer<Integer, Integer> func) { this.func = func; }
}
// for part 2 - number of left shifts to reverse 'rotate based on'
final static int[] reverseLookup = { 1, 1, 6, 2, 7, 3, 0, 4 };
static int findIndex(int c) {
for (int i = 0; i < password.length; i++) {
if (password[i] == (char) c) return i;
}
return -1;
}
static void swap(int x, int y) {
if (x > 7) x = findIndex(x);
if (y > 7) y = findIndex(y);
char temp = password[x];
password[x] = password[y];
password[y] = temp;
}
static void move(int x, int y) {
char temp = password[x];
if (x < y) {
rotate_left(x, y);
} else {
rotate_right(x, y);
}
password[y] = temp;
}
static void rotate_right(int x, int y) {
if (y >= 0) {
for (int i = x; i > y; i--) {
swap(i, i - 1);
}
} else {
for (int i = 0; i < x; i++) {
for (int j = password.length - 1; j > 0; j--) {
swap(j, j - 1);
}
}
}
}
static void rotate_left(int x, int y) {
if (y >= 0) {
for (int i = x; i < y; i++) {
swap(i, i + 1);
}
} else {
for (int i = 0; i < x; i++) {
for (int j = 0; j < password.length - 1; j++) {
swap(j, j + 1);
}
}
}
}
static void rotate_based(int x, int y) {
int index = findIndex(x);
rotate_right(1 + index + (index >= 4 ? 1 : 0), -1);
}
static void reverse(int x, int y) {
for (int i = 0; i <= (y - x)/2; i++) {
swap(x + i, y - i);
}
}
static char[] password = "abcdefgh".toCharArray();
static void display() {
System.out.println(password);
}
public static void main(String[] args) {
List<String[]> input = FileIO.getFileLinesSplit("advent15.txt", " ");
for (String[] each : input) {
Command command;
int x = 0;
int y = -1;
if (each[0].equals("rotate")) {
command = Command.valueOf(each[0] + "_" + each[1]);
if (command == Command.rotate_based) {
x = (int) each[6].charAt(0);
} else {
x = Integer.parseInt(each[2]);
}
y = -1;
} else {
command = Command.valueOf(each[0]);
switch (each[1]) {
case "position" :
x = Integer.parseInt(each[2]);
y = Integer.parseInt(each[5]);
break;
case "letter" :
x = (int) each[2].charAt(0);
y = (int) each[5].charAt(0);
break;
case "positions" :
x = Integer.parseInt(each[2]);
y = Integer.parseInt(each[4]);
}
}
command.func.accept(x, y);
}
display();
System.out.println("part 2");
password = "fbgdceah".toCharArray();
Collections.reverse(input);
for (String[] each : input) {
Command command = null;
int x = 0;
int y = -1;
if (each[0].equals("rotate")) {
switch (each[1]) {
case "based" :
command = Command.rotate_left;
x = reverseLookup[findIndex(each[6].charAt(0))];
break;
case "left" :
command = Command.rotate_right;
x = Integer.parseInt(each[2]);
break;
case "right" :
command = Command.rotate_left;
x = Integer.parseInt(each[2]);
}
y = -1;
} else {
command = Command.valueOf(each[0]);
switch (each[1]) {
case "position" :
x = Integer.parseInt(each[5]);
y = Integer.parseInt(each[2]);
break;
case "letter" :
x = (int) each[5].charAt(0);
y = (int) each[2].charAt(0);
break;
case "positions" :
x = Integer.parseInt(each[2]);
y = Integer.parseInt(each[4]);
}
}
command.func.accept(x, y);
}
display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment