-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDay21.java
More file actions
132 lines (111 loc) · 4.35 KB
/
Day21.java
File metadata and controls
132 lines (111 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.ck.adventofcode.year2016;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.IntBinaryOperator;
import java.util.function.UnaryOperator;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;
@Solution(
id = 20162101,
name = "Day 21: Scrambled Letters and Hash",
url = "https://adventofcode.com/2016/day/21",
category = "2016")
@Solution(
id = 20162102,
name = "Day 21: Scrambled Letters and Hash - Part 2",
url = "https://adventofcode.com/2016/day/21",
category = "2016")
public class Day21 extends AOCSolution {
@Override
protected void runPartOne(final Scanner in) {
run(
in,
UnaryOperator.identity(),
Day21::rotateRight,
Day21::rotateLeft,
(length, position) -> (position + (position > 3 ? 2 : 1)) % length,
(left, second) -> left,
(left, second) -> second);
}
@Override
protected void runPartTwo(final Scanner in) {
run(
in,
List::reversed,
Day21::rotateLeft,
Day21::rotateRight,
(length, position) ->
position % 2 == 1
? length - ((position + 1) / 2)
: ((((length - position) % length) / 2) + (length - 1)) % length,
(left, second) -> second,
(left, second) -> left);
}
private void run(
final Scanner in,
final UnaryOperator<List<String>> sort,
final IntBinaryOperator getRotateRightCount,
final IntBinaryOperator getRotateLeftCount,
final IntBinaryOperator getPositionBasedCount,
final IntBinaryOperator getMoveFrom,
final IntBinaryOperator getMoveTo) {
final StringBuilder password = new StringBuilder(in.nextLine());
final List<String> steps = new ArrayList<>();
while (in.hasNextLine()) {
steps.add(in.nextLine());
}
for (final String step : sort.apply(steps)) {
final String[] parts = step.split(" ");
if ("swap".equals(parts[0]) && "position".equals(parts[1])) {
final int x = Integer.parseInt(parts[2]);
final int y = Integer.parseInt(parts[5]);
swapCharacters(password, x, y);
} else if ("swap".equals(parts[0]) && "letter".equals(parts[1])) {
final int x = password.indexOf(parts[2]);
final int y = password.indexOf(parts[5]);
swapCharacters(password, x, y);
} else if ("rotate".equals(parts[0]) && ("right").equals(parts[1])) {
final int count = Integer.parseInt(parts[2]);
rotateRight(password, getRotateRightCount.applyAsInt(password.length(), count));
} else if ("rotate".equals(parts[0]) && "left".equals(parts[1])) {
final int count = Integer.parseInt(parts[2]);
rotateRight(password, getRotateLeftCount.applyAsInt(password.length(), count));
} else if ("rotate".equals(parts[0]) && "based".equals(parts[1])) {
final int position = password.indexOf(parts[6]);
final int count = getPositionBasedCount.applyAsInt(password.length(), position);
rotateRight(password, count);
} else if ("reverse".equals(parts[0])) {
final int start = Integer.parseInt(parts[2]);
final int stop = Integer.parseInt(parts[4]) + 1;
password.replace(
start, stop, new StringBuilder(password.substring(start, stop)).reverse().toString());
} else if ("move".equals(parts[0])) {
final int x = Integer.parseInt(parts[2]);
final int y = Integer.parseInt(parts[5]);
final int from = getMoveFrom.applyAsInt(x, y);
final int to = getMoveTo.applyAsInt(x, y);
final char temp = password.charAt(from);
password.deleteCharAt(from);
password.insert(to, temp);
}
}
print(password);
}
private static int rotateRight(int length, int count) {
return count;
}
private static int rotateLeft(int length, int count) {
return length - count;
}
private static void swapCharacters(final StringBuilder password, final int x, final int y) {
final char temp = password.charAt(x);
password.setCharAt(x, password.charAt(y));
password.setCharAt(y, temp);
}
private static void rotateRight(final StringBuilder password, final int count) {
password
.insert(0, password.substring(password.length() - count))
.delete(password.length() - count, password.length());
}
}