-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHashMapCounterspionage.java
212 lines (182 loc) · 10.1 KB
/
HashMapCounterspionage.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/*
Под мудрым руководством товарища Сталина, подготовь Красную Армию к наступлению.
(править можно только то, на что указал товарищ Сталин)
*/
public class HashMapCounterspionage {
private static final Random random = new Random();
static class Abwehr {
private static List<Unterscharfuhrer> abwehrOfficers = new ArrayList<>();
private static int killedN = 0;
static {
List<String> names = List.of("ABRAHAM", "EUGEN", "FERDINAND", "GERHOLD");
List<String> lastNames = List.of("KLEIN", "RICHTER", "WEBER", "SCHMIDT");
IntStream.rangeClosed(1, 5).forEach((i) -> {
abwehrOfficers.add(new Unterscharfuhrer(
lastNames.get(random.nextInt(lastNames.size())),
names.get(random.nextInt(names.size())),
random.nextInt(37) + 18
));
});
}
static void infiltrate(List<Comrade> redArmy) {
Iterator<Comrade> iterator = redArmy.iterator();
abwehrOfficers.forEach(officer -> {
if (random.nextBoolean()) {
redArmy.add(random.nextInt(redArmy.size()), officer.disguiseWithForgedTicket());
} else {
Comrade killed = redArmy.remove(random.nextInt(redArmy.size()));
killedN++;п
redArmy.add(random.nextInt(redArmy.size()), officer.disguiseWithRealTicket(killed));
}
});
}
}
static class NKVD {
private static Map<ComradePartyTicket, Comrade> comrades = new HashMap<>();
static {
List<String> names = List.of("ИВАН", "ДМИТРИЙ", "ВАСИЛИЙ", "ФЕОФАН", "ИЛЬЯ", "ПЕТР", "ВЛАДИМИР", "ГЛЕБ");
List<String> lastNames = List.of("ИВАНОВ", "ПЕТРОВ", "САМОЙЛОВ", "ДАВЫДОВ", "СЕМЕНОВ", "ВЫКРУТАСОВ");
List<String> patronymics = List.of("ИВАНОВИЧ", "ПЕТРОВИЧ", "ИЛЬИЧ", "ВЛАДИМИРОВИЧ");
IntStream.rangeClosed(1, 50).forEach((i) -> {
String name = names.get(random.nextInt(names.size()));
String lastName = lastNames.get(random.nextInt(lastNames.size()));
String patronymic = patronymics.get(random.nextInt(patronymics.size()));
Comrade comrade = new Comrade(name, lastName, patronymic,
new ComradePartyTicket(i,
lastName + " " + name + " " + patronymic,
random.nextInt(37) + 18,
ComradePartyTicket.BracketsMaterial.IRON));
comrades.put(comrade.ticket.archiveCopy(), comrade);
});
}
static List<Comrade> findComrades(List<ComradePartyTicket> partyTickets) {
return partyTickets.stream().map(ticket -> comrades.get(ticket)).filter(Objects::nonNull).collect(Collectors.toList());
}
static List<ComradePartyTicket> findSuspicious(List<Comrade> redArmy) {
return Stream.concat(
redArmy.stream().filter(comrade -> !comrades.getOrDefault(comrade.ticket, comrade).equals(comrade) || !checkTicketValid(comrade.ticket)),
redArmy.stream().filter(comrade -> random.nextBoolean() && random.nextBoolean() && random.nextBoolean() && random.nextBoolean()))
.distinct()
.map(comrade -> comrade.ticket.archiveCopy())
.collect(Collectors.toList());
}
static List<Comrade> killTraitors(List<Comrade> redArmy, List<ComradePartyTicket> traitorTickets) {
List<Comrade> traitors = redArmy.stream()
.filter(comrade -> traitorTickets.contains(comrade.ticket))
.collect(Collectors.toList());
redArmy.removeAll(traitors);
return traitors;
}
static boolean checkTicketValid(ComradePartyTicket ticket) {
// Stalin thinks this is not effective
return ticket != null;
}
}
static class SMERSH {
public static List<ComradePartyTicket> detectSpies(List<Comrade> redArmy, List<ComradePartyTicket> suspicious) {
return Stream
.concat(
redArmy.stream()
.filter(comrade -> suspicious.contains(comrade.ticket))
.filter(s ->
!s.ticket.fio.contains(s.lastName) ||
!s.ticket.fio.contains(s.name) ||
!s.ticket.fio.contains(s.patronymic)),
redArmy.stream()
.filter(comrade -> suspicious.contains(comrade.ticket))
.filter(s -> s.ticket.bracketsMaterial != ComradePartyTicket.BracketsMaterial.IRON))
.distinct()
.map(comrade -> comrade.ticket.archiveCopy())
.collect(Collectors.toList());
}
}
static class ComradePartyTicket {
// Stalin thinks ComradePartyTicket could be better
int N;
String fio;
int age;
BracketsMaterial bracketsMaterial;
boolean copy = false;
public ComradePartyTicket(int n, String fio, int age, BracketsMaterial bracketsMaterial) {
N = n;
this.fio = fio;
this.age = age;
this.bracketsMaterial = bracketsMaterial;
}
public ComradePartyTicket archiveCopy() {
ComradePartyTicket copy = new ComradePartyTicket(N, fio, age, bracketsMaterial);
copy.copy = true;
return copy;
}
public enum BracketsMaterial {
IRON,
STAINLESS_STEEL
}
}
static class Comrade {
// Stalin thinks You should watch Comarades
String name;
String lastName;
String patronymic;
ComradePartyTicket ticket;
public Comrade(String name, String lastName, String patronymic, ComradePartyTicket ticket) {
this.name = name;
this.lastName = lastName;
this.patronymic = patronymic;
this.ticket = ticket;
}
}
static class Unterscharfuhrer {
// Stalin said never forget about your enemies
String name;
String lastName;
int age;
public Unterscharfuhrer(String name, String lastName, int age) {
this.name = name;
this.lastName = lastName;
this.age = age;
}
public Comrade disguiseWithRealTicket(Comrade killedComrade) {
return new Comrade(name, lastName, "NACI", killedComrade.ticket);
}
public Comrade disguiseWithForgedTicket() {
return new Comrade(name, lastName, "",
new ComradePartyTicket(
random.nextInt(199) + 100,
name + " " + lastName,
age,
ComradePartyTicket.BracketsMaterial.STAINLESS_STEEL));
}
}
public static void main(String[] args) {
ArrayList<Comrade> redArmy = new ArrayList<>(NKVD.comrades.values());
Abwehr.infiltrate(redArmy);
List<ComradePartyTicket> spies = NKVD.findSuspicious(redArmy);
List<ComradePartyTicket> traitors = SMERSH.detectSpies(redArmy, spies);
List<Comrade> killedComrades = NKVD.findComrades(traitors);
List<Comrade> killedTraitors = NKVD.killTraitors(redArmy, traitors);
if (killedComrades.size() == Abwehr.killedN && killedTraitors.size() == Abwehr.abwehrOfficers.size()) {
System.out.println("Внимание! Внимание! Говорит Москва! Красная Армия перешла в наступление!");
System.out.println(String.format("Предательски в спину были убиты бойцы: %s", killedComrades.stream().map(s -> s.ticket.fio).collect(Collectors.joining("\n"))));
System.out.println(String.format("Но храбрые бойцы НКВД не спят! Раскрыта и уничтожена вражеская группа в составе: %s", killedTraitors.stream().map(traitor -> traitor.lastName + " " + traitor.name).collect(Collectors.joining("\n"))));
System.out.println(String.format("Героическая Советская армия в составе %s \nведет бой с врагом", redArmy.stream().map(s -> s.ticket.fio).collect(Collectors.joining("\n"))));
} else {
System.out.println("На фронте без перемен");
if (!killedComrades.isEmpty()) {
System.out.println(String.format("Предательски в спину были убиты бойцы: %s", killedComrades.stream().map(s -> s.ticket.fio).collect(Collectors.joining("\n"))));
}
if (!killedTraitors.isEmpty()) {
System.out.println(String.format("Но храбрые бойцы НКВД не спят! Раскрыта и уничтожена вражеская группа в составе: %s", killedTraitors.stream().map(traitor -> traitor.lastName + " " + traitor.name).collect(Collectors.joining("\n"))));
}
if (killedComrades.size() < Abwehr.killedN) {
System.out.println(String.format("В лесу найдено %d неопознанных тела", Abwehr.killedN - killedComrades.size()));
}
System.out.println(String.format("Героическая Советская армия в составе:\n %s \n\nведет неравный бой с врагом", redArmy.stream().map(s -> s.ticket.fio).collect(Collectors.joining("\n"))));
}
}
}