-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22.java
172 lines (149 loc) · 3.86 KB
/
Day22.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
public class Day22 {
public static void main(String[] args) throws IOException {
List<String> lines = Files.readAllLines(Paths.get("input22.txt"));
Queue<Integer> first = new ArrayBlockingQueue<>(50);
Queue<Integer> second = new ArrayBlockingQueue<>(50);
boolean firstPlayer = true;
for (int i = 1; i < lines.size(); i++) {
String line = lines.get(i);
if (line.isEmpty()) {
firstPlayer = false;
++i;
continue;
}
if (firstPlayer)
first.add(Integer.parseInt(line));
else
second.add(Integer.parseInt(line));
}
Queue<Integer> firstP = copy(first);
Queue<Integer> secondP = copy(second);
System.out.println(sumPoints(playNormal(firstP, secondP)));
int winner = playAdvanced(first, second, new HashSet<>());
if (winner == 1) System.out.println(sumPoints(first));
else System.out.println(sumPoints(second));
}
private static Queue<Integer> playNormal(Queue<Integer> first, Queue<Integer> second) {
while (true) {
int card1 = first.remove();
int card2 = second.remove();
if (card1 > card2) {
first.add(card1);
first.add(card2);
} else {
second.add(card2);
second.add(card1);
}
if (first.size() == 0 || second.size() == 0)
break;
}
if (first.size() == 0) return second;
else return first;
}
private static int playAdvanced(Queue<Integer> first, Queue<Integer> second, Set<List<Integer>> situations) {
while (true) {
List<Integer> situation = makeList(copy(first), copy(second));
if (situations.contains(situation)) {
return 1;
}
situations.add(situation);
int card1 = first.remove();
int card2 = second.remove();
int winner = -1;
if (first.size() >= card1 && second.size() >= card2) {
Queue<Integer> fCopy = takeFirst(card1, first);
Queue<Integer> sCopy = takeFirst(card2, second);
winner = playAdvanced(fCopy, sCopy, new HashSet<>());
} else {
winner = card1 > card2 ? 1 : 2;
}
if (winner == 1) {
first.add(card1);
first.add(card2);
} else {
second.add(card2);
second.add(card1);
}
if (first.size() == 0 || second.size() == 0)
break;
}
if (first.size() == 0) return 2;
else return 1;
}
private static Queue<Integer> takeFirst(int noOfCards, Queue<Integer> first) {
Queue<Integer> queue = new ArrayBlockingQueue<>(50);
boolean flag = true;
int head = -1;
int counter = 0;
while (true) {
if (flag) {
flag = false;
head = first.peek();
queue.add(head);
first.add(head);
first.remove();
++counter;
}
int value = first.peek();
if (value == head)
break;
if (counter < noOfCards) {
queue.add(value);
}
first.add(value);
first.remove();
++counter;
}
return queue;
}
private static Queue<Integer> copy(Queue<Integer> first) {
Queue<Integer> queue = new ArrayBlockingQueue<>(50);
boolean flag = true;
int head = -1;
while (true) {
if (flag) {
flag = false;
head = first.peek();
queue.add(head);
first.add(head);
first.remove();
}
int value = first.peek();
if (value == head)
break;
queue.add(value);
first.add(value);
first.remove();
}
return queue;
}
private static List<Integer> makeList(Queue<Integer> firstCopy, Queue<Integer> secondCopy) {
List<Integer> list = new ArrayList<>();
while (!firstCopy.isEmpty()) {
list.add(firstCopy.remove());
}
list.add(-1);
while (!secondCopy.isEmpty()) {
list.add(secondCopy.remove());
}
return list;
}
private static int sumPoints(Queue<Integer> first) {
int sum = 0;
int size = first.size();
while (first.size() != 0) {
sum += size * first.remove();
size--;
}
return sum;
}
}