-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackToThecode.js
168 lines (158 loc) · 5.6 KB
/
backToThecode.js
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
var opponentCount = parseInt(readline());
var complete = false;
var target, last, complete;
while (true) {
var gameRound = parseInt(readline());
var inputs = readline().split(' ');
var x = parseInt(inputs[0]); // Your x position
var y = parseInt(inputs[1]); // Your y position
var backInTimeLeft = parseInt(inputs[2]); // Remaining back in time
for (var i = 0; i < opponentCount; ++i) {
var inputs = readline().split(' ');
var opponentX = parseInt(inputs[0]);
var opponentY = parseInt(inputs[1]);
var opponentBackInTimeLeft = parseInt(inputs[2]);
}
var map = [];
for (var i = 0; i < 20; ++i) {
map.push(readline());
/*for(var j = 0; j < line.length; ++j) {
}*/ // One line of the map ('.' = free, '0' = you, otherwise the id of the opponent)
}
// Write an action using print()
// To debug: printErr('Debug messages...');
if(gameRound == 1) {
target = {};
target.x = (x < 17)? 0 : 34;
target.y = (y < 10)? 0 : 19;
last = calculateLast(target, {x: x, y: y});
} else if(target.x === x && target.y === y) {
if(last.x === x && last.y === y) {
var maxfree = -1;
for(var i = 0; i < gameRound * 2 + 50; ++i) {
var x1 = Math.floor(Math.random() * 35);
var y1 = Math.floor(Math.random() * 20);
var dist = Math.abs(x1 - x) + Math.abs(y1 - y);
var newfree = calculateFree(map, x1, y1, x, y) / dist;
if(newfree > maxfree) {
target.x = x1;
target.y = y1;
maxfree = newfree;
}
}
} else {
if(shouldComplete(map, x, y, last.x, last.y)) {
complete = true;
target.x = last.x;
target.y = last.y;
} else {
complete = false;
var maxfree = -1;
for(var i = 0; i < gameRound * 2 + 50; ++i) {
var x1 = Math.floor(Math.random() * 35);
var y1 = Math.floor(Math.random() * 20);
var dist = Math.abs(x1 - x) + Math.abs(y1 - y);
var newfree = calculateFree(map, x1, y1, x, y) / dist;
if(newfree > maxfree) {
target.x = x1;
target.y = y1;
maxfree = newfree;
}
}
last = calculateLast(target, {x: x, y: y});
}
}
}
if(gameRound == 100 && backInTimeLeft > 0 && countPoints(map) < 50) {
print("BACK 25");
} else {
if(x === target.x) {
print(target.x + ' ' + target.y + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
} else {
if(target.x > x && map[y][x+1] !== '.') {
if(y !== 19 && map[y+1][x] === '.') {
print(target.x + ' ' + (y+1) + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
} else if(y !== 0 && map[y-1][x] === '.') {
print(target.x + ' ' + (y-1) + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
} else {
print(target.x + ' ' + target.y + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
}
} else if(map[y][x-1] !== '.') {
if(y !== 19 && map[y+1][x] === '.') {
print(target.x + ' ' + (y+1) + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
} else if(y !== 0 && map[y-1][x] === '.') {
print(target.x + ' ' + (y-1) + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
} else {
print(target.x + ' ' + target.y + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
}
} else {
print(target.x + ' ' + target.y + ' :' + x + ' ' + y + ' ' + last.x + ' ' + last.y + ' ' + complete);
}
}
}
}
function calculateFree(map, x1, y1, x, y) {
var freeCount = 0;
var i, j;
if(y > y1) {
for(i = y; i >= y1; --i) {
if(map[i][x] == '.') {
freeCount++;
}
}
} else {
for(i = y; i <= y1; ++i) {
if(map[i][x] == '.') {
freeCount++;
}
}
}
if(x > x1) {
for(i = x; i >= x1; --i) {
if(map[y1][i] == '.') {
freeCount++;
}
}
} else {
for(i = x; i <= x1; ++i) {
if(map[y1][i] == '.') {
freeCount++
}
}
}
return freeCount;
}
function shouldComplete(map, x1, y1, x2, y2) {
var p1 = {x: Math.min(x1, x2), y: Math.min(y1, y2)};
var p2 = {x: Math.max(x1, x2), y: Math.max(y1, y2)};
var empty = 0;
for(var i = p1.x; i < p2.x; ++i) {
for(var j = p1.y; j < p2.y; ++j) {
if(map[j][i] === '.') {
++empty;
} else if(map[j][i] !== '0') {
return false;
}
}
}
return empty > 4;
}
function countPoints(map) {
var points = 0;
for(var i = 0; i < 20; ++i) {
for(var j = 0; j < 35; ++j) {
if(map[i][j] === '0') {
++points;
}
}
}
return points;
}
function calculateLast(target, current) {
if(target.y > current.y) {
current.y += 1;
} else if(target.y < current.y) {
current.y -= 1;
}
return current;
}