-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rb
executable file
·362 lines (304 loc) · 8.58 KB
/
main.rb
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
#!/usr/bin/ruby
DATA = File.read('data.txt')
UNITS = DATA.each_line.each_with_index.map do |line, y|
line
.each_char
.each_with_index
.select{ |c, x| c =~ /[GE]/ }
.map{ |c, x| { type: c, x: x, y: y, hp: 200 } }
end.flatten(1).sort_by{ |c| [c[:y], c[:x]] }
MAP = DATA.gsub(/[GE]/, '.').each_line.to_a
SEARCH_ORDER = [[0, -1], [-1, 0], [1, 0], [0, 1]]
$debug = false
def find_closest_enemy u, units
m = MAP.join.each_line.to_a
# puts m.join
units.each{ |u| m[u[:y]][u[:x]] = u[:type] }
enemy = case u[:type]
when 'G'
'E'
when 'E'
'G'
end
# search = SEARCH_ORDER.map do |dx, dy|
# x = u[:x] + dx
# y = u[:y] + dy
# [x, y]
# end
# .reject{ |x, y| m[y][x] == u[:type] }
# .reject{ |x, y| m[y][x] == '#' }
search = [[u[:x], u[:y]]]
# p search
found = []
200.times do |n|
new_search = []
search.each do |sx, sy|
# p [ 99, sx, sy]
SEARCH_ORDER.each do |dx, dy|
# dx, dy = a
x = sx + dx
y = sy + dy
next if x >= m[0].size or y >= m.size
next if x < 0 or y < 0
next if m[y][x] == '#'
next if m[y][x] == u[:type]
next if m[y][x] == 'X'
if m[y][x] == enemy
found << [x, y]
else
m[y][x] = 'X'
new_search << [x, y]
end
end
end
search = new_search
# p search
break if found.size > 0
end
# puts m.join
found = found.sort.uniq
if found.size > 0
# p found
return [found.sort_by{ |x, y| [y, x] }.first]
else
return nil
end
end
def next_step_to_closest_enemy u, units
# p ['unit', u]
m = MAP.join.each_line.to_a
# puts m.join
units.each{ |u| m[u[:y]][u[:x]] = u[:type] }
finished = false
enemy = case u[:type]
when 'G'
'E'
when 'E'
'G'
end
distances = Array.new(m.size){ Array.new(m[0].size){ 9999 } }
# p distances
# search = units.select{ |u| u[:type] == enemy }.map{ |u| [u[:x], u[:y]] }
search = find_closest_enemy u, units
return nil if not search
if $debug
puts 'Search: %s' % search.inspect
end
500.times do |n|
# p ['s', search]
new_search = []
search.each do |sx, sy|
SEARCH_ORDER.each_with_index.each do |a, i|
dx, dy = a
x = sx+dx
y = sy+dy
next if x >= m[0].size or y >= m.size
next if x < 0 or y < 0
next if m[y][x] != '.'
# m[y][x] = (0x31 + n).chr
# p [66, x, y]
if n == 0
dist = i
else
dist = distances[sy][sx]+4
end
next if distances[y][x] <= dist
distances[y][x] = dist
new_search << [x, y]
end
end
search = new_search
break if search.size == 0
end
if $debug
puts m.join
p u
puts distances.map{ |a| a.map{ |x| '%5s' % x }.join(' ') }.join("\n")
end
arr = SEARCH_ORDER
.map{ |x, y| m[u[:y]+y][u[:x]+x] }
.each_with_index
.reject{ |c, _| c =~ /[GE#\.]/ }
arr = SEARCH_ORDER
.map{ |x, y| [u[:x]+x, u[:y]+y] }
.map{ |x, y| distances[y][x] }
.each_with_index
.reject{ |d, _| d > 9995 }
.to_a
# p arr
idx = arr
.reverse
.min
# .reverse
# p idx
return [nil, nil] unless idx
SEARCH_ORDER[idx.last]
end
def find_enemy_to_attack u, units
enemy = case u[:type]
when 'G'
'E'
when 'E'
'G'
end
available_enemies = SEARCH_ORDER
.map{ |x, y| [u[:x]+x, u[:y]+y] }
.map{ |x, y| units.find{ |u| [u[:x],u[:y]] == [x, y] } }
.select(&:itself)
.select{ |u| u[:type] == enemy }
# .first
return nil unless available_enemies.size > 0
return available_enemies
.each_with_index
.sort_by{ |e, i| [e[:hp], i] }
.first.first
# p available_enemies
enemies = units
.each_with_index
.map{ |u, i| [u[:hp], i] }
.sort
.map{ |_, i| units[i] }
.select{ |u| u[:type] == enemy }
puts '000'
p [u, u.object_id]
p enemies
return nil if enemies.size == 0
enemies = enemies
.select{ |u| u[:hp] == enemies.first[:hp] }
p enemies
p SEARCH_ORDER
.map{ |x, y| p [u[:x]+x, u[:y]+y] }
.map{ |x, y| p enemies.find{ |u| [u[:x],u[:y]] == [x, y] } }
.select(&:itself)
.first
# SEARCH_ORDER
# .select(&:itself)
# .reject{ |u| u[:type] != enemy }
# .reverse
# .map{ |u| [u[:hp], u] }
# .min
end
def do_round units, eap = 3
idx = 0
loop do
# p [idx, units.size]
break if idx >= units.size
break if units.map{ |u| u[:type] }.sort.uniq.size == 1
# p units
enemy = find_enemy_to_attack units[idx], units
# p enemy
if not enemy
x, y = next_step_to_closest_enemy units[idx], units
# p [units[idx], x, y]
if x and y
units[idx][:x] += x
units[idx][:y] += y
end
print_units units if $debug
enemy = find_enemy_to_attack units[idx], units
end
# p units[idx]
idx += 1
next unless enemy
enemy_idx = units.each_with_index
.find{ |u, i| u.object_id == enemy.object_id }
# p [idx-1, enemy_idx.last]
if enemy_idx
enemy_idx = enemy_idx.last
if units[enemy_idx][:type] == 'G'
units[enemy_idx][:hp] -= eap
else
units[enemy_idx][:hp] -= 3
end
# puts '%s[%s,%s] -> %s[%s,%s] (hp %s -> %s)' % [
# units[idx-1][:type],
# units[idx-1][:x],
# units[idx-1][:y],
# units[enemy_idx][:type],
# units[enemy_idx][:x],
# units[enemy_idx][:y],
# (units[enemy_idx][:hp]+3),
# units[enemy_idx][:hp],
# ]
if units[enemy_idx][:hp] < 1
idx -= 1
units.delete_at enemy_idx
end
end
end
units
end
def print_units units
m = MAP.join.each_line.map(&:strip)
units.each do |u|
m[u[:y]][u[:x]] = u[:type]
end
m.each_with_index do |l, i|
m[i] += ' ' + units
.select{ |u| u[:y] == i }
.sort_by{ |u| u[:x] }
.map{ |u, i| '%{type}(%{hp})' % u }
.join(', ')
end
puts m.join("\n")
end
def do_sim units
n = 1
units = units.sort_by{ |u| [u[:y], [u[:x]]] }
loop do
# $debug = true if n == 24
# $debug = true
# p $debug
units = do_round units
units = units.sort_by{ |u| [u[:y], [u[:x]]] }
n += 1
puts '=' * 80
puts "Round #{n-1}"
print_units units
p units
puts '=' * 80
# break if n > 1
break if units.map{ |u| u[:type] }.sort.uniq.size == 1
end
print_units units
p units
p [n, units.map{ |u| u[:hp] }.sum]
(n-2) * units.map{ |u| u[:hp] }.sum
end
p do_sim UNITS
def do_sim_huj units_orig
elves_count = units_orig.count{ |u| u[:type] == 'E' }
(4..100).each do |eap|
n = 1
units = units_orig.map(&:clone).sort_by{ |u| [u[:y], [u[:x]]] }
loop do
# $debug = true if n == 24
# $debug = true
# p $debug
units = do_round units, eap
units = units.sort_by{ |u| [u[:y], [u[:x]]] }
n += 1
# puts '=' * 80
# puts "Round #{n-1}"
# print_units units
# p units
# puts '=' * 80
# break if n > 1
break if units.map{ |u| u[:type] }.sort.uniq.size == 1
end
curr = units.count{ |u| u[:type] == 'E' }
if curr == elves_count
puts "eap: #{eap}"
print_units units
p units
p [n, units.map{ |u| u[:hp] }.sum]
(n-2) * units.map{ |u| u[:hp] }.sum
break
end
end
end
# p do_sim_huj UNITS
# p UNITS
# p do_round UNITS
# puts 'Part 1: %s' % PART1
# puts 'Part 2: %s' % PART2