-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.java
139 lines (117 loc) · 3.37 KB
/
Day16.java
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
133
134
135
136
137
138
139
import java.util.ArrayList;
import java.util.List;
public class Day16 {
static class Command {
char op;
int a;
int b;
Command(char op, int a, int b) {
this.op = op;
this.a = a;
this.b = b;
}
@Override
public String toString() {
return op + " " + a + " " + b;
}
}
private static char[] _tmp = new char[16];
static char[] part1(char[] dancers, List<Command> input) {
int offset = 0;
for (Command move : input) {
char c = move.op;
switch (c) {
case 's':
offset = move.a;
break;
case 'x':
char x = dancers[move.a];
dancers[move.a] = dancers[move.b];
dancers[move.b] = x;
break;
case 'p': {
char a = (char) move.a;
char b = (char) move.b;
int t = -1;
int i = 0;
while (i < 16 && t != -2) {
if (dancers[i] == a || dancers[i] == b) {
if (t == -1) {
t = i;
}
else {
char tmp = dancers[i];
dancers[i] = dancers[t];
dancers[t] = tmp;
t = -2;
}
}
i++;
}
break;
}
}
}
offset = offset % dancers.length;
System.arraycopy(dancers, dancers.length - offset, _tmp, 0, offset);
System.arraycopy(dancers, 0, dancers, offset, dancers.length - offset);
System.arraycopy(_tmp, 0, dancers, 0, offset);
return dancers;
}
static String part2(char[] dancers, String[] split) {
List<Command> moves = convert(split, dancers.length);
int init = 1;
List<String> results = new ArrayList<>();
results.add(new String(dancers));
for (int i = init; i < 1000000000; i++) {
dancers = part1(dancers, moves);
if (results.contains(new String(dancers))) {
return results.get(1000000000 % (i));
}
else {
results.add(new String(dancers));
}
}
return new String(dancers);
}
static List<Command> convert(String[] input, int nDancers) {
List<Command> result = new ArrayList<>(input.length);
int offset = 0;
for (String move : input) {
char c = move.charAt(0);
if (c == 's') {
offset = (offset + Integer.parseInt(move.substring(1))) % nDancers;
}
else if (c == 'x') {
int idx = move.indexOf('/');
int posA = (Integer.parseInt(move.substring(1, idx)) + nDancers - offset) % nDancers;
int posB = (Integer.parseInt(move.substring(idx + 1)) + nDancers - offset) % nDancers;
result.add(new Command('x', posA, posB));
}
else if (c == 'p') {
int idx = move.indexOf('/');
char a = move.charAt(1);
char b = move.charAt(idx + 1);
result.add(new Command('p', a, b));
}
}
result.add(new Command('s', offset, -1));
return result;
}
public static void main(String[] args) {
List<String> input = Util.readInput("day16.input");
String[] split = input.iterator().next().split(",");
char[] dancers = getChars(16);
// bkgcdefiholnpmja
// System.out.println(part1(dancers, convert(split, dancers.length)));
// knmdfoijcbpghlea
System.out.println(part2(dancers, split));
}
private static char[] getChars(int n) {
char[] dancers = new char[n];
for (int c = 0; c < n; c++) {
dancers[c] = (char) (c + 'a');
}
return dancers;
}
}