-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay16.java
128 lines (100 loc) · 3.11 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
package aoc17;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Day16 {
private final int DANCER_COUNT = 16;
private Queue<String> danceCommands;
public Day16(File input) {
danceCommands = getDanceCommands(input);
}
/**
* part 2
*
* @return permutation after 1.000.000.000 dances using cycle detection and
* modulo arithmetic
*/
public String lastDance() {
Set<String> uniqueDances = new LinkedHashSet<>();
List<Character> dancers = getDancers();
// add the initial state
uniqueDances.add("abcdefghijklmnop");
String dance = executeDance(dancers);
// repeat till dances start to repeat
while (!uniqueDances.contains(dance)) {
uniqueDances.add(dance);
dance = executeDance(dancers);
}
// calculate amount of dances left
int repeatCount = uniqueDances.size();
long dancesLeft = 1000000000L - repeatCount;
// get the target index
int indexOfTargetVal = (int) (dancesLeft % repeatCount);
int count = 0;
String target = "";
for (String currentDance : uniqueDances) {
if (count == indexOfTargetVal)
target = currentDance;
count++;
}
return target;
}
// part 1
public String executeDance() {
return executeDance(getDancers());
}
private String executeDance(List<Character> dancers) {
Queue<String> commands = new LinkedList<>(danceCommands);
while (!commands.isEmpty()) {
executeDanceCommand(commands.poll(), dancers);
}
return dancers.stream().map(s -> Character.toString(s)).collect(Collectors.joining());
}
private List<Character> getDancers() {
int intValueA = 'a';
return IntStream.range(0, DANCER_COUNT).mapToObj(s -> (char) (s + intValueA)).collect(Collectors.toList());
}
private void executeDanceCommand(String command, List<Character> dancers) {
switch (command.charAt(0)) {
case 's':
Collections.rotate(dancers, Integer.parseInt(command.substring(1)));
break;
case 'x':
Collections.swap(dancers, Integer.parseInt(command.substring(1, command.indexOf('/'))),
Integer.parseInt(command.substring(command.indexOf('/') + 1)));
break;
case 'p':
Collections.swap(dancers, dancers.indexOf(command.charAt(command.indexOf('/') - 1)),
dancers.indexOf(command.charAt(command.indexOf('/') + 1)));
break;
default:
throw new IllegalArgumentException();
}
}
private Queue<String> getDanceCommands(File input) {
Queue<String> commands = new LinkedList<>();
try {
Scanner sc = new Scanner(input);
sc.useDelimiter(",");
while (sc.hasNext()) {
commands.offer(sc.next());
}
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
return commands;
}
public static void main(String[] args) {
Day16 test = new Day16(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2017\\Day 16\\InputFile1.txt"));
System.out.println(test.lastDance());
}
}