-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23A.java
95 lines (75 loc) · 3.26 KB
/
Day23A.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
package advent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
public class Day23A {
private static int MOVES = 100;
public static void main(String[] args) throws IOException {
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Paste the input:");
final List<Integer> cups = new ArrayList<>();
do {
final String readLine = bufferedReader.readLine();
if (readLine != null && !readLine.isEmpty()) {
for (final String cupNumber : readLine.trim().split("")) {
cups.add(Integer.parseInt(cupNumber));
}
} else {
break;
}
} while (true);
final int totalCups = cups.size();
System.out.println("Got " + totalCups + " cups");
final Instant start = Instant.now();
// final Integer highestValue = cups.stream().max(Comparator.naturalOrder()).get();
int position = 0;
for (int i = 0; i < MOVES; i++) {
// System.out.println((i + 1) + ": " + cups);
// System.out.println("position: " + position);
final int fixedCupNumber = cups.get(position);
//Remove cups
final List<Integer> nextThreeCups = new ArrayList<>();
for (int j = 0; j < 3; j++) {
if (position + 1 >= cups.size()) {
nextThreeCups.add(cups.remove(0));
} else {
nextThreeCups.add(cups.remove(position + 1));
}
}
// System.out.println("removedCups: " + nextThreeCups);
int destinationCupNumber = fixedCupNumber;
do {
destinationCupNumber--;
if (destinationCupNumber == 0) destinationCupNumber = totalCups;
} while (nextThreeCups.contains(destinationCupNumber));
// System.out.println("destinationCupNumber: " + destinationCupNumber);
final int destinationCupPosition = cups.indexOf(destinationCupNumber);
// System.out.println("destinationCupPosition: " + destinationCupPosition);
cups.addAll(destinationCupPosition + 1, nextThreeCups);
// System.out.println("--- " + cups);
for (int j = cups.indexOf(fixedCupNumber) - position; j > 0; j--) {
// System.out.println("Fixing<");
cups.add(cups.size() - 1, cups.remove(0));
}
position++;
if (position >= totalCups) position = 0;
}
// System.out.println("END: " + cups);
final StringBuilder cupOrder = new StringBuilder();
int i = cups.indexOf(1);
do {
i++;
if (i == totalCups) i = 0;
final Integer integer = cups.get(i);
if (integer == 1) break;
cupOrder.append(cups.get(i));
} while (true);
final Instant finish = Instant.now();
final long timeElapsed = Duration.between(start, finish).toMillis();
System.out.println("Cup order is " + cupOrder + " in " + timeElapsed + "ms");
}
}