-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisease-simulation.cpp
More file actions
388 lines (387 loc) · 10.5 KB
/
disease-simulation.cpp
File metadata and controls
388 lines (387 loc) · 10.5 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "pch.h"
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <sstream>
using namespace std;
ofstream filetxt3("C:\\Users\\HP\\Desktop\\TURNS.txt");
vector<string>store;//All inputs from input will be here
string myhabitat[100][3] = { "\n" };// will do an entity check and put the entity name gate and its location here
int usetoendloc[100] = { 0 };
vector < vector<int> >location;//the position of all assets in the input will be here
vector<int>canbeill;
vector<int>normall;//immune assets
vector<int>tamdie;//dead assets that have completed the five-day period
vector<int>ht, tempyi, recover;
vector<vector< int>> willbehealthy;
vector<vector< int>>willdie;
vector<vector< int>>dieds;
map<int, string>endloc;//the last position of dead beings
int recovered = 0;
bool door = true;
//Conversion of string value to int
int turnint(string tempk) {
int set = 0;
for (int i = 0; i < tempk.size(); i++) {
int kat = 0;
kat = tempk[i];
kat = kat - 48;
set = set * 10 + kat;
}
return set;
}
//the situation in which the infected asset will recover or die
void allow(string search1, string myhabitat1) {
int temp = 0;
for (int i = 0; i < search1.size(); i++) {
if (search1[i] == 'A' || search1[i] == 'B' || search1[i] == 'C' || search1[i] == 'D') {
temp++;
}
}
int entity = turnint(myhabitat1);
if (temp < 3) {
tempyi.push_back(entity);
tempyi.push_back(1);
willbehealthy.push_back(tempyi);
}
else {
tempyi.push_back(entity);
tempyi.push_back(1);
willdie.push_back(tempyi);
}
tempyi.clear();
}
//Whether the non-infect entity will be ill
void notill(string search, int sizehbt) {
int temp = 0;
for (int i = 0; i < search.size(); i++) {
if (search[i] == 'A' || search[i] == 'B') {
temp++;
}
}
if (temp == 0) {
normall.push_back(sizehbt);
}
else {
canbeill.push_back(sizehbt);
}
}
//controlb control whether the infected entity will recover or die
void control(int j1) {
int willill = canbeill[j1];
string controlharf = myhabitat[willill - 1][1];
int tempi = 0;
for (int i = 0; i < controlharf.size(); i++) {
if (controlharf[i] == 'A' || controlharf[i] == 'B' || controlharf[i] == 'C' || controlharf[i] == 'D') {
tempi++;
}
}
if (tempi >= 3) {
tempyi.push_back(willill);
tempyi.push_back(1);
willdie.push_back(tempyi);
}
else {
tempyi.push_back(willill);
tempyi.push_back(1);
willbehealthy.push_back(tempyi);
}
tempyi.clear();
canbeill.erase(canbeill.cbegin() + j1);
ht.clear();
}
//Deletion of the same elements in the vector for controld
void htcontol() {
sort(ht.begin(), ht.end());
ht.erase(unique(ht.begin(), ht.end()), ht.end());
for (int it = 0; it < ht.size(); it++) {
int d = ht[it];
control(d);
}
}
//distance measurement from sick assets to potentially sick assets
void controlb1(int set) {
for (int j = 0; j < canbeill.size(); j++) {
int farx = location[set - 1][0] - location[(canbeill[j]) - 1][0];
if (farx < 0) { farx = farx * -1; }
int fary = location[set - 1][1] - location[(canbeill[j]) - 1][1];
if (fary < 0) { fary = fary * -1; }
if (farx <= 3 && fary <= 3) {
ht.push_back(j);
}
}
if (ht.size() >= 1) {
htcontol();
}
}
//string values of 4x5 incoming form are converted int and thrown into the location vector
void turnloc1(string loca) {
int sayac2 = 0;
for (int m = 0; m < loca.size(); m++) {
if (loca[m] == 'x') {
tempyi.push_back(sayac2);
sayac2 = 0;
}
else {
int sayac = 0;
sayac = loca[m];
sayac = sayac - 48;
sayac2 = sayac2 * 10 + sayac;
}
if (m == loca.size() - 1) {
tempyi.push_back(sayac2);
}
}
}
//here we check the distance between the healthy people who are sent and those who are sick.
void controlb() {
for (int i = 0; i < willbehealthy.size(); i++) {
if (willbehealthy[i][1] >= 4) {
controlb1(willbehealthy[i][0]);
}
}
for (int i = 0; i < willdie.size(); i++) {
if (willdie[i][1] >= 4) {
controlb1(willdie[i][0]);
}
}
for (int i = 0; i < dieds.size(); i++) {
for (auto adres2 = endloc.cbegin(); adres2 != endloc.cend(); ++adres2) {
int fk = adres2->first;
if (fk == dieds[i][0]) {
string loc = adres2->second;
turnloc1(loc);
for (int j = 1; j < canbeill.size(); j++) {
int farx = tempyi[0] - location[(canbeill[j]) - 1][0];
if (farx < 0) { farx = farx * -1; }
int fary = tempyi[1] - location[(canbeill[j]) - 1][1];
if (fary < 0) { fary = fary * -1; }
if (farx <= 3 && fary <= 3) {
ht.push_back(j);
}
}
tempyi.pop_back();
tempyi.pop_back();
}
}
if (ht.size() >= 1) {
htcontol();
}
}
}
//here, increase 1 per turn in turn and see if the time is up accordingly
void controlc(int turnumb) {
for (int i = 0; i < willbehealthy.size(); i++) {
willbehealthy[i][1]++;
if (willbehealthy[i][1] == 31) {
recover.push_back(willbehealthy[i][0]);
recovered++;
willbehealthy.erase(willbehealthy.cbegin() + i);
}
}
for (int i = 0; i < willdie.size(); i++) {
willdie[i][1]++;
if (willdie[i][1] == 15) {
tempyi.push_back(willdie[i][0]);
tempyi.push_back(6);
dieds.push_back(tempyi);
tempyi.clear();
endloc[willdie[i][0]] = store[willdie[i][0] + 1 + turnumb];;
willdie.erase(willdie.cbegin() + i);
}
}
for (int i = 0; i < dieds.size(); i++) {
dieds[i][1]--;
if (dieds[i][1] == 0) {
tamdie.push_back(dieds[i][0]);
dieds.erase(dieds.cbegin() + i);
}
}
}
//Check whether the desired element is in the given vector
bool findit(int tempy, vector<vector<int>>exam) {
int sayac = 0;
for (int i = 0; i < exam.size(); i++) {
if (exam[i][0] == tempy) {
sayac++;
}
}
if (sayac > 0) { return true; }
else { return false; }
}
//check whether the incoming asset is sick or dead and write its location
void writing(int i, ofstream& examp) {
bool door1 = true;
if (door1) {
for (int j = 0; j < canbeill.size(); j++) {
if (canbeill[j] == i) {
examp << "entity " << i << " " << location[i - 1][0] << "x" << location[i - 1][1] << " Normal" << endl;
door1 = false;
}
}
}
if (door1) {
for (int j = 0; j < recover.size(); j++) {
if (recover[j] == i) {
examp << "entity " << i << " " << location[i - 1][0] << "x" << location[i - 1][1] << " Immune" << endl;
door1 = false;
}
}
}
if (door1) {
for (int j = 0; j < tamdie.size(); j++) {
if (tamdie[j] == i) {
examp << "entity " << i << " " << endloc[i] << " Dead" << endl;
door1 = false;
}
}
}
if (door1) {
for (int j = 0; j < normall.size(); j++) {
if (normall[j] == i) {
examp << "entity " << i << " " << location[i - 1][0] << "x" << location[i - 1][1] << " Normal" << endl;
door1 = false;
}
}
}
if (door1) {
for (int j = 0; j < dieds.size(); j++) {
if (dieds[j][0] == i) {
examp << "entity " << i << " " << endloc[i] << " Dead" << endl;
door1 = false;
}
}
}
if (findit(i, willbehealthy) || findit(i, willdie)) {
examp << "entity " << i << " " << location[i - 1][0] << "x" << location[i - 1][1] << " Infected" << endl;
}
}
//convert the location to int and throw it into location
void turnloc(string loc) {
turnloc1(loc);
location.push_back(tempyi);
tempyi.pop_back();
tempyi.pop_back();
}
//turns
void turns(int i, int size) {
filetxt3 << "Turn " << i << ":" << endl;
for (int j = 0; j < size + 2; j++) {
filetxt3 << "- ";
}
filetxt3 << endl;
for (int m = 1; m < size + 1; m++) {
filetxt3 << "- ";
for (int j = 1; j < size + 1; j++) {
bool empty = true;
for (auto adres = endloc.cbegin(); adres != endloc.cend(); ++adres) {
if (usetoendloc[adres->first] <= 4) {
string lox = adres->second;
turnloc1(lox);
int ix = tempyi[0];
int iy = tempyi[1];
tempyi.clear();
if (ix == m && iy == j) {
filetxt3 << "D "; empty = false;
usetoendloc[adres->first]++;
}
}
}
for (int k = 0; k < location.size(); k++) {
if (location[k][0] == m && location[k][1] == j) {
for (int i = 0; i < willbehealthy.size(); i++) {
if (willbehealthy[i][0] == k + 1) {
filetxt3 << "X "; empty = false;
}
}
for (int i = 0; i < willdie.size(); i++) {
if (willdie[i][0] == k + 1) {
filetxt3 << "X "; empty = false;
}
}
for (int d = 0; d < canbeill.size(); d++) {
if (canbeill[d] == k + 1) {
filetxt3 << "O "; empty = false;
}
}
for (int d = 0; d < normall.size(); d++) {
if (normall[d] == k + 1) {
filetxt3 << "0 "; empty = false;
}
}
}
}
if (empty) {
filetxt3 << " ";
}
}
filetxt3 << "-" << endl;
}
for (int j = 0; j < size + 2; j++) {
filetxt3 << "- ";
}
filetxt3 << endl << endl;
}
int main() {
ifstream filetxt("C:\\Users\\HP\\Desktop\\INPUT.txt");
while (filetxt) {
string temp;
filetxt >> temp;
store.push_back(temp);
}
string tempy = store[1];
int size = turnint(tempy);
tempy = store[3];
int turn_count = turnint(tempy);
int k = 0, turnnumber = 3;
for (int i = 4; i < store.size(); i++) {
string entity = store[i];
if (entity == "entity") {
myhabitat[k][0] = store[i + 1];
myhabitat[k][1] = store[i + 2];
myhabitat[k][2] = store[i + 3];
if (store[i + 4] == "infected") {
allow(myhabitat[k][1], myhabitat[k][0]);
}
else {
notill(myhabitat[k][1], k + 1);
}
k++;
}
else if (entity == "turn") {
turnnumber++;
break;
}
turnnumber++;
}
int sayac = 0;
for (int i = turnnumber; i < store.size(); i++) {
if (store[i] == "turn") {
for (int j = i + 2; j < i + k + 2; j++) {
string loc = store[j];
turnloc(loc);
}
controlb();
turns(sayac + 1, size);//here we print the turns according to the incoming turn
sayac++;
if (store[i+k+2]=="turn") {
controlc(i);
location.clear();
}
}
}
ofstream filetxt1("C:\\Users\\HP\\Desktop\\OUTPUT.txt");
filetxt1 << "Normal :" << normall.size() + canbeill.size() << endl;
filetxt1 << "Infected :" << willbehealthy.size() + willdie.size() << endl;
filetxt1 << "Dead :" << dieds.size() + tamdie.size() << endl;
filetxt1 << "Recovered :" << recovered << endl;
//here we printed the final positions and states of the assets
for (int i = 0; i < k; i++) {
writing(i + 1, filetxt1);
}
return 0;
}