-
Notifications
You must be signed in to change notification settings - Fork 0
/
IBM Jan2021 Challenge.vb
364 lines (300 loc) · 8.6 KB
/
IBM Jan2021 Challenge.vb
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
363
364
Option Explicit
Dim Filas, Columnas, totalRelevantes As Integer
Dim matriz() As String
Dim posicion(1, 2) As Integer
Dim orientacion As String
Dim utilidad As Integer
Dim ultimoUno(1, 2) As Integer
Dim tamano As Integer
Dim posicionesRelevantes() As Integer
Dim MaxUtilidad(), NuevoRank() As Integer
Sub InitData()
Dim Fila As Integer
Dim Columna As Integer
Filas = Range("Filas")
Columnas = Range("Columnas")
ReDim matriz(Filas, Columnas)
ReDim MaxUtilidad(1, 5)
Sheet2.Cells.Clear
Sheet2.Cells(Range("BPOS1FIL"), Range("BPOS1COL")) = "B"
Sheet2.Cells(Range("BPOS2FIL"), Range("BPOS2COL")) = "B"
Sheet2.Cells(Range("BPOS3FIL"), Range("BPOS3COL")) = "B"
posicion(1, 1) = Range("CurrentPosX")
posicion(1, 2) = Range("CurrentPosY")
orientacion = Range("Orientation")
For Fila = 1 To Filas
For Columna = 1 To Columnas
If IsEmpty(Sheet2.Cells(Fila, Columna)) = True Then
Sheet2.Cells(Fila, Columna) = 0
End If
matriz(Fila, Columna) = Sheet2.Cells(Fila, Columna)
Next
Next
End Sub
Sub BlanquearMatriz()
Dim Fila, Columna As Integer
For Fila = 1 To Filas
For Columna = 1 To Columnas
matriz(Fila, Columna) = 0
Next
Next
End Sub
Sub NextStep()
lectura
advanceOne
funcionUtilidad
imprimir
End Sub
Sub Reiniciar()
Dim Fila, Columna As Integer
Range("CurrentPosX") = 1
Range("CurrentPosY") = 1
Range("Orientation") = "UP"
Range("utility") = 0
ultimoUno(1, 1) = 0
ultimoUno(1, 2) = 0
End Sub
Sub advanceOne()
Dim Fila, Columna As Integer
Fila = posicion(1, 1)
Columna = posicion(1, 2)
If matriz(posicion(1, 1), posicion(1, 2)) = "0" Then
matriz(posicion(1, 1), posicion(1, 2)) = "1"
If orientacion = "UP" Then
orientacion = "RIGHT"
ElseIf orientacion = "RIGHT" Then
orientacion = "DOWN"
ElseIf orientacion = "DOWN" Then
orientacion = "LEFT"
ElseIf orientacion = "LEFT" Then
orientacion = "UP"
End If
ultimoUno(1, 1) = posicion(1, 1)
ultimoUno(1, 2) = posicion(1, 2)
ElseIf matriz(posicion(1, 1), posicion(1, 2)) = "1" Then
matriz(posicion(1, 1), posicion(1, 2)) = "2"
If orientacion = "UP" Then
orientacion = "LEFT"
ElseIf orientacion = "LEFT" Then
orientacion = "DOWN"
ElseIf orientacion = "DOWN" Then
orientacion = "RIGHT"
ElseIf orientacion = "RIGHT" Then
orientacion = "UP"
End If
ElseIf matriz(posicion(1, 1), posicion(1, 2)) = "B" Then
If orientacion = "UP" Then
orientacion = "LEFT"
ElseIf orientacion = "LEFT" Then
orientacion = "DOWN"
ElseIf orientacion = "DOWN" Then
orientacion = "RIGHT"
ElseIf orientacion = "RIGHT" Then
orientacion = "UP"
End If
End If
If orientacion = "RIGHT" Then
If posicion(1, 2) = Columnas Then
Columna = 1
ElseIf posicion(1, 2) < Columnas Then
Columna = Columna + 1
End If
ElseIf orientacion = "DOWN" Then
If posicion(1, 1) = Filas Then
Fila = 1
ElseIf posicion(1, 1) < Filas Then
Fila = Fila + 1
End If
ElseIf orientacion = "LEFT" Then
If posicion(1, 2) = 1 Then
Columna = Columnas
ElseIf posicion(1, 2) > 1 Then
Columna = Columna - 1
End If
ElseIf orientacion = "UP" Then
If posicion(1, 1) = 1 Then
Fila = Filas
ElseIf posicion(1, 1) > 1 Then
Fila = Fila - 1
End If
End If
posicion(1, 1) = Fila
posicion(1, 2) = Columna
End Sub
Sub imprimir()
Dim Fila As Integer
Dim Columna As Integer
Range("CurrentPosX") = posicion(1, 1)
Range("CurrentPosY") = posicion(1, 2)
Range("Orientation") = orientacion
For Fila = 1 To Filas
For Columna = 1 To Columnas
Sheet2.Cells(Fila, Columna).Value = matriz(Fila, Columna)
Next
Next
Range("utility") = utilidad
End Sub
Sub lectura()
posicion(1, 1) = Range("CurrentPosX")
posicion(1, 2) = Range("CurrentPosY")
orientacion = Range("Orientation")
End Sub
Sub Next50()
Dim contador As Long
lectura
For contador = 1 To 12000000
advanceOne
Next
funcionUtilidad
'imprimir
MsgBox utilidad
End Sub
Sub AnswerFinder()
Dim Fila, TotalResp As Integer
Dim Columna As Integer
Dim NumeroRespuestas As Integer
Dim Respuestas() As Integer
Dim Recorredor, contador As Long
ReDim NuevoRank(Range("OtrasPosRank").Rows.Count, 2)
NumeroRespuestas = 0
For TotalResp = 1 To Range("OtrasPosRank").Rows.Count
NuevoRank(TotalResp, 1) = Range("OtrasPosRank").Cells(TotalResp, 1)
NuevoRank(TotalResp, 2) = Range("OtrasPosRank").Cells(TotalResp, 2)
Next
lectura
For contador = 1 To 50000
advanceOne
Next
funcionUtilidad
For Fila = 1 To Filas
For Columna = 1 To Columnas
If matriz(Fila, Columna) = "1" Or matriz(Fila, Columna) = "2" Then
NumeroRespuestas = NumeroRespuestas + 1
End If
Next
Next
ReDim Respuestas(NumeroRespuestas, 3)
NumeroRespuestas = 0
For Fila = 1 To Filas
For Columna = 1 To Columnas
If matriz(Fila, Columna) = "1" Or matriz(Fila, Columna) = "2" Then
NumeroRespuestas = NumeroRespuestas + 1
Respuestas(NumeroRespuestas, 1) = Fila
Respuestas(NumeroRespuestas, 2) = Columna
End If
Next
Next
For Recorredor = 1 To NumeroRespuestas
BlanquearMatriz
matriz(2, 2) = "B"
'matriz(41, 1) = "B"
matriz(Respuestas(Recorredor, 1), Respuestas(Recorredor, 2)) = "B"
orientacion = "UP"
posicion(1, 1) = 1
posicion(1, 2) = 1
For contador = 1 To 50000
advanceOne
Next
funcionUtilidad
Respuestas(Recorredor, 3) = utilidad
If utilidad > 19995 Then
MsgBox "Found!"
Stop
End If
Next
'MsgBox "Nada :("
For Fila = 1 To NumeroRespuestas
Sheet1.Cells(Fila + 11, 1) = Respuestas(Fila, 1)
Sheet1.Cells(Fila + 11, 2) = Respuestas(Fila, 2)
Sheet1.Cells(Fila + 11, 3) = Respuestas(Fila, 3)
Next
End Sub
Sub funcionUtilidad()
Dim Fila As Integer
Dim Columna As Integer
utilidad = 0
For Fila = 1 To Filas
For Columna = 1 To Columnas
If matriz(Fila, Columna) = "1" Then
utilidad = utilidad + 1
ElseIf matriz(Fila, Columna) = "2" Then
utilidad = utilidad + 2
End If
Next
Next
End Sub
Sub AnswerLastOne()
Dim Fila As Integer
Dim Columna, contador As Integer
Dim Selectas() As Integer
InitData
lectura
ReDim Selectas(Range("MejoresRespuestas").Rows.Count, 3)
For Fila = 1 To Range("MejoresRespuestas").Rows.Count
BlanquearMatriz
orientacion = "UP"
posicion(1, 1) = 1
posicion(1, 2) = 1
Selectas(Fila, 1) = Range("MejoresRespuestas").Cells(Fila, 1)
Selectas(Fila, 2) = Range("MejoresRespuestas").Cells(Fila, 2)
matriz(Selectas(Fila, 1), Selectas(Fila, 2)) = "B"
For contador = 1 To 30000
advanceOne
Next
encuentraRelevantes
For Columna = 1 To totalRelevantes
BlanquearMatriz
orientacion = "UP"
posicion(1, 1) = 1
posicion(1, 2) = 1
matriz(Selectas(Fila, 1), Selectas(Fila, 2)) = "B"
matriz(posicionesRelevantes(Columna, 1), posicionesRelevantes(Columna, 2)) = "B"
For contador = 1 To 30000
advanceOne
Next
funcionUtilidad
If MaxUtilidad(1, 5) < utilidad Then
MaxUtilidad(1, 1) = Selectas(Fila, 1)
MaxUtilidad(1, 2) = Selectas(Fila, 2)
MaxUtilidad(1, 3) = posicionesRelevantes(Columna, 1)
MaxUtilidad(1, 4) = posicionesRelevantes(Columna, 2)
MaxUtilidad(1, 5) = utilidad
End If
Next
Next
For Fila = 1 To Range("UtilidadUltUno").Rows.Count
Range("UtilidadUltUno").Cells(Fila, 1) = Selectas(Fila, 0)
Next
End Sub
Sub encuentraRelevantes()
Dim Fila As Integer
Dim Columna As Integer
totalRelevantes = 0
For Fila = 1 To Filas
For Columna = 1 To Columnas
If matriz(Fila, Columna) = "1" Or matriz(Fila, Columna) = "2" Then
totalRelevantes = totalRelevantes + 1
End If
Next
Next
ReDim posicionesRelevantes(totalRelevantes, 2)
totalRelevantes = 0
For Fila = 1 To Filas
For Columna = 1 To Columnas
If matriz(Fila, Columna) = "1" Or matriz(Fila, Columna) = "2" Then
totalRelevantes = totalRelevantes + 1
posicionesRelevantes(totalRelevantes, 1) = Fila
posicionesRelevantes(totalRelevantes, 2) = Columna
End If
Next
Next
End Sub
Sub Programa()
Dim contador As Integer
lectura
For contador = 1 To 30000
advanceOne
Next
AnswerFinder
imprimir
End Sub