forked from mcarton/Hashcode-final-round
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxima.py
executable file
·252 lines (189 loc) · 7.64 KB
/
maxima.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
#!/usr/bin/env python3
import sys
import random
import copy
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):
# 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 = [[False for _ in range(problem.cols)] for _ in range(problem.rows)]
cells = []
for i in range(-problem.radius, problem.radius + 1):
for j in range(-problem.radius, problem.radius + 1):
if i**2 + j**2 <= problem.radius**2:
cells.append((i, j))
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):
if balloons_alt[b_id] == 0:
assert(solution[turn_id][b_id] == 0 or solution[turn_id][b_id] == 1)
balloons_alt[b_id] += solution[turn_id][b_id]
else:
balloons_alt[b_id] += solution[turn_id][b_id]
assert(balloons_alt[b_id] > 0 and balloons_alt[b_id] <= problem.altitudes)
# 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_i, cell_j in cells:
i = pos.i + cell_i
j = (pos.j + cell_j) % problem.cols
if i >= 0 and i < problem.rows and j >= 0 and j < problem.cols:
grid[i][j] = True
for target in problem.targets:
if grid[target.i][target.j]:
r += 1
grid[target.i][target.j] = False
return r
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 covered_cells(problem, p):
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 pos.valid(problem) and (p.i - pos.i)**2 + column_dist(problem, p.j, pos.j)**2 <= problem.radius**2:
yield pos
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
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 = random_balloon(problem, solution, b_id, iterations)
print('current score: %d' % score(problem, solution))
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)
solution = random_solution(problem, 10)
s = score(problem, solution)
write_solution(problem, solution, '%d.out' % s)