-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkynetTheVirus.cpp
More file actions
140 lines (128 loc) · 3.69 KB
/
SkynetTheVirus.cpp
File metadata and controls
140 lines (128 loc) · 3.69 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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
struct points {
int point1;
int point2;
};
class Link {
public:
Link(int &point1, int &point2) {
m_points = new points();
m_points->point1 = point1;
m_points->point2 = point2;
m_broken = false;
}
~Link() {
delete m_points;
}
void breakLink() {
m_broken = true;
}
bool isBroken() { return m_broken; }
points getLinks() {
return *m_points;
}
private:
points* m_points;
bool m_broken;
};
static bool contains(int value, std::vector<int> vec)
{
for (int i = 0; i < vec.size(); i++)
{
if (vec[i] == value)
{
return true;
}
}
return false;
}
int main()
{
auto nodeCount = 0; // the total number of nodes in the level, including the gateways
auto numberOfLinks = 0; // the number of links
auto numberOfGateways = 0; // the number of exit gateways
vector<int> *gateways = new vector<int>();
vector<Link*> *links = new vector<Link*>();
cin >> nodeCount >> numberOfLinks >> numberOfGateways; cin.ignore();
cerr << "node count " << nodeCount << " number of links " << numberOfLinks << " number of gateways " << numberOfGateways << endl;
// get the links
for (int i = 0; i < numberOfLinks; i++) {
int n1;
int n2;
cin >> n1 >> n2; cin.ignore();
Link* n = new Link(n1, n2);
links->push_back(n);
}
// get the indexes
for (int i = 0; i < numberOfGateways; i++) {
int gateway = 0; // the index of a gateway node
cin >> gateway; cin.ignore();
gateways->push_back(gateway);
}
int lastSI = -1; // the last known location of the Skynet bot - init to -1 (out of bounds)
// game loop
while (1) {
cerr << "entering game loop" << endl;
int SI; // The index of the node on which the Skynet agent is positioned this turn
cin >> SI; cin.ignore();
cerr << "SI IS: " << SI << endl;
bool noGatewayNode = true;
for (int i = 0; i < numberOfLinks; i++) {
// if link contains SI and gateway - break it
// if it's already broken, ignore it
if (links->at(i)->isBroken())
{
continue;
}
// if SI in points and gateway in points
// delete link
if ((lastSI != links->at(i)->getLinks().point1) && (lastSI != links->at(i)->getLinks().point2))
{
if ((links->at(i)->getLinks().point1 == SI) || (links->at(i)->getLinks().point2 == SI))
{
// Great, one of the points matches the SI
// Now check for the gateway in points 1 and 2
if (contains(links->at(i)->getLinks().point1, *gateways)) {
cerr << "found in if block 1" << "loop count:" << i
<< " Output is " << links->at(i)->getLinks().point1
<< " " << links->at(i)->getLinks().point2 << endl;
cout << links->at(i)->getLinks().point1 << " " << links->at(i)->getLinks().point2 << endl;
links->at(i)->breakLink();
lastSI = SI;
noGatewayNode = false;
break;
}
if (contains(links->at(i)->getLinks().point2, *gateways)) {
cerr << "found in if block 2" << "loop count:" << i
<< " Output is " << links->at(i)->getLinks().point1
<< " " << links->at(i)->getLinks().point2 << endl;
cout << links->at(i)->getLinks().point1 << " " << links->at(i)->getLinks().point2 << endl;
links->at(i)->breakLink();
lastSI = SI;
noGatewayNode = false;
break;
}
}
}
cerr << "loop " << i << endl; // print the loop count to stderr
}
if (noGatewayNode) {
for (int j = 0; j < numberOfLinks; j++)
{
if (links->at(j)->isBroken())
{
continue;
}
cerr << "Hitting a random link because the gateway link could not be found" << endl;
cout << links->at(j)->getLinks().point1 << " " << links->at(j)->getLinks().point2 << endl;
links->at(j)->breakLink();
lastSI = SI;
break;
}
}
}
}