forked from mcarton/Hashcode-final-round
-
Notifications
You must be signed in to change notification settings - Fork 0
/
berdes.py
executable file
·293 lines (224 loc) · 8.92 KB
/
berdes.py
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
#!/usr/bin/env python3
import sys
import random
import copy
cover = []
class Point:
def __init__(self, i, j):
self.i = i
self.j = j
def valid(self, problem):
return self.i >= 0 and self.i < problem.rows and self.j >= 0 and self.j < problem.cols
def __repr__(self):
return 'Point(%d, %d)' % (self.i, self.j)
class Coord:
def __init__(self, pt, alt):
self.pt = pt
self.alt = alt
def __repr__(self):
return 'Coord(%d, %d, %d)' % (self.pt.i, self.pt.j, self.alt)
class Problem:
def __init__(self, rows, cols, altitudes, targets, radius, num_ballons, turns, starting_cell, mov_grids):
self.rows = rows
self.cols = cols
self.altitudes = altitudes
self.targets = targets
self.radius = radius
self.num_ballons = num_ballons
self.turns = turns
self.starting_cell = starting_cell
self.mov_grids = mov_grids
def print(self):
print('rows: %d' % self.rows)
print('cols: %d' % self.cols)
print('altitudes: %d' % self.altitudes)
print('radius: %d' % self.radius)
print('num_ballons: %d' % self.num_ballons)
print('starting_cell: %s' % self.starting_cell)
print('turns: %d' % self.turns)
print('targets:')
for t in self.targets[:5]:
print('\t%s' % t)
print('mov_grids[1]:')
for wind in self.mov_grids[1][:2]:
print('\t%s' % wind)
print('mov_grids[2]:')
for wind in self.mov_grids[2][:2]:
print('\t%s' % wind)
for a in range(1, self.altitudes + 1):
assert(all(len(row) == self.cols for row in self.mov_grids[a]))
class Solution:
def __init__(self, problem):
self.problem = problem
self.balloons = [Coord(self.problem.starting_cell, 0) for _ in range(self.problem.num_ballons)]
def score(problem, solution):
global cover
# solution[turn][balloon_id] = {1, 0, -1}
balloons_pos = [problem.starting_cell for _ in range(problem.num_ballons)]
balloons_alt = [0 for _ in range(problem.num_ballons)]
r = 0
grid = [[-2 for _ in range(problem.cols)] for _ in range(problem.rows)]
balloons_score = [0 for _ in range(problem.num_ballons)]
tmp_point = Point(0,0)
for turn_id in range(problem.turns):
# déplacement en altitude
for b_id in range(problem.num_ballons):
pos = balloons_pos[b_id]
if pos.valid(problem):
balloons_alt[b_id] += solution[turn_id][b_id]
# vent
for b_id in range(problem.num_ballons):
pos = balloons_pos[b_id]
alt = balloons_alt[b_id]
if alt > 0 and pos.valid(problem):
i = pos.i + problem.mov_grids[alt][pos.i][pos.j].i
j = (pos.j + problem.mov_grids[alt][pos.i][pos.j].j) % problem.cols
balloons_pos[b_id] = Point(i, j)
# on calcul les points du tour
for b_id in range(problem.num_ballons):
pos = balloons_pos[b_id]
if balloons_alt[b_id] > 0 and pos.valid(problem): # ballon valide
for cell in cover:
tmp_point.i = cell.i+pos.i
tmp_point.j = (cell.j+pos.j)%problem.cols
if tmp_point.valid(problem):
if grid[tmp_point.i][tmp_point.j] == -2:
grid[tmp_point.i][tmp_point.j] = b_id
else:
grid[tmp_point.i][tmp_point.j] = -1
for target in problem.targets:
target_status = grid[target.i][target.j]
if target_status > -2:
if target_status >= 0:
balloons_score[target_status] += 1
r += 1
grid[target.i][target.j] = -2
return (r, balloons_score.index(min(balloons_score)))
def parse_problem(f):
rows, cols, altitudes = map(int, f.readline().strip().split())
num_targets, radius, num_ballons, turns = map(int, f.readline().strip().split())
starting_cell_i, starting_cell_j = map(int, f.readline().strip().split())
targets = []
for _ in range(num_targets):
i, j = map(int, f.readline().strip().split())
targets.append(Point(i, j))
mov_grids = []
mov_grids.append([]) # altitude 0
for _ in range(altitudes):
grid = []
for _ in range(rows):
in_ = list(map(int, f.readline().strip().split()))
wind = []
while in_:
wind.append(Point(in_[0], in_[1]))
in_ = in_[2:]
grid.append(wind)
mov_grids.append(grid)
return Problem(rows, cols, altitudes, targets, radius, num_ballons, turns,
Point(starting_cell_i, starting_cell_j), mov_grids)
def parse_solution(problem, f):
solution = []
for _ in range(problem.turns):
solution.append(list(map(int, f.readline().strip().split())))
return solution
def generate_solution(problem, solution):
result = ''
for turn in solution:
result += ' '.join(str(move) for move in turn) + '\n'
return result
def write_solution(problem, solution, filename):
with open(filename, 'w') as f:
f.write(generate_solution(problem, solution))
def column_dist(problem, c1, c2):
diff = abs(c1 - c2)
return min(diff, problem.cols - diff)
def is_covered(problem, balloon, target):
r, c = balloon.pt.i, balloon.pt.j
u, v = target.i, target.j
return (r - u)**2 + column_dist(c, v)**2 <= problem.radius**2
def nb_covered(solution):
nb = 0
for target in solution.prb.targets:
for balloon in solution.balloons:
if is_covered(solution.prb, balloon, target):
nb += 1
break
return nb
def random_balloon(problem, solution, b_id, iterations=None):
max_solution = None
max_score = 0
if iterations is None:
iterations = 10 ** 9
for _ in range(iterations):
alt = 0
for turn_id in range(problem.turns):
if alt == 0:
solution[turn_id][b_id] = 1
elif alt == problem.altitudes:
solution[turn_id][b_id] = random.choice((-1, 0))
elif alt == 1:
solution[turn_id][b_id] = random.choice((0, 1))
else:
solution[turn_id][b_id] = random.choice((-1, 0, 1))
alt += solution[turn_id][b_id]
(s, _) = score(problem, solution)
if s > max_score:
max_score = s
max_solution = copy.deepcopy(solution)
return (max_solution, max_score)
def random_balloon_suite(problem, solution, b_id, iterations=None):
max_solution = None
max_score = 0
max_min_b_id = b_id
if iterations is None:
iterations = 10 ** 9
for _ in range(iterations):
alt = 0
for turn_id in range(problem.turns):
if alt == 0:
solution[turn_id][b_id] = 1
elif alt == problem.altitudes:
solution[turn_id][b_id] = random.choice((-1, 0))
elif alt == 1:
solution[turn_id][b_id] = random.choice((0, 1))
else:
solution[turn_id][b_id] = random.choice((-1, 0, 1))
alt += solution[turn_id][b_id]
(s, min_b_id) = score(problem, solution)
if s > max_score:
max_score = s
max_solution = copy.deepcopy(solution)
max_min_b_id = min_b_id
return (max_solution, max_score, max_min_b_id)
def random_solution(problem, iterations):
solution = [[0 for _ in range(problem.num_ballons)] for _ in range(problem.turns)]
for b_id in range(problem.num_ballons):
(solution, s) = random_balloon(problem, solution, b_id, iterations)
print('current score %d: %d' % (b_id, s))
(s, next_b_id) = score(problem, solution)
solution_back = copy.deepcopy(solution)
while True:
(solution, next_s, next_b_id) = random_balloon_suite(problem, solution, next_b_id, iterations)
if next_s > s:
s = next_s
write_solution(problem, solution, '%d.out' % s)
solution_back = copy.deepcopy(solution)
else:
solution = solution_back
print('current score + : %d' % s)
return solution
if __name__ == '__main__':
if len(sys.argv) < 2:
print('usage: %s FILE' % sys.argv[0], file=sys.stderr)
exit(1)
with open(sys.argv[1], 'r') as f:
problem = parse_problem(f)
p = Point(0,0)
for i in range(p.i - problem.radius, p.i + problem.radius + 1):
for j in range(p.j - problem.radius, p.j + problem.radius + 1):
pos = Point(i, j % problem.cols)
if (p.i - pos.i)**2 + column_dist(problem, p.j, pos.j)**2 <= problem.radius**2:
cover.append(pos)
solution = random_solution(problem, 300)
s = score(problem, solution)
write_solution(problem, solution, '%d.out' % s)