-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.groovy
executable file
·73 lines (52 loc) · 1.42 KB
/
solution.groovy
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
#!/usr/bin/env groovy
void doRotate(List<String> list, String arg) {
def amount = Integer.valueOf(arg.substring(1))
Collections.rotate(list, amount)
}
void exchangePos(List<String> list, String arg) {
def parts = arg.substring(1).split("/")
list.swap(Integer.valueOf(parts[0]), Integer.valueOf(parts[1]))
}
void exchangeProg(List<String> list, String arg) {
def p1 = list.indexOf(arg.substring(1, 2));
def p2 = list.indexOf(arg.substring(3, 4));
list.swap(p1, p2);
}
def input = System.in.newReader().readLine()
def moves = input.split(",")
def state = new LinkedList(Arrays.asList("abcdefghijklmnop".split("")))
String dance(List<String> state, String[] moves) {
for (move in moves) {
switch (move.charAt(0)) {
case 's':
doRotate(state, move)
break;
case 'x':
exchangePos(state, move)
break;
case 'p':
exchangeProg(state, move)
break;
default:
println("Unknown type!")
}
}
return state.join("")
}
String danceOften(String[] moves, Integer amount) {
def state = new LinkedList(Arrays.asList("abcdefghijklmnop".split("")))
def seen = new HashSet<String>()
def order = new ArrayList<String>()
def summary = state.join("");
for (i in 0..<amount) {
if (seen.contains(summary)) {
return order.get(amount % i)
}
seen.add(summary)
order.add(summary)
summary = dance(state, moves)
}
return summary;
}
println dance(state, moves)
println danceOften(moves, 1000000000)