-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOceanOfCode.py
More file actions
1847 lines (1455 loc) · 58.4 KB
/
OceanOfCode.py
File metadata and controls
1847 lines (1455 loc) · 58.4 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import math
import copy
import random
import time
import gc
TURN_OPP_SILENCE = -1
game_board = [ None , None ]
movement_f = { 'N': None,'S': None,'E': None,'W': None }
state = 0
k_STATE_AGENT_STARTING = 0
k_STATE_AGENT_DISCRETE = 1
k_STATE_AGENT_SEARCHING = 2
k_STATE_AGENT_MINING = 3
k_STATE_AGENT_WARING = 4
print_agent = {
k_STATE_AGENT_STARTING: 'k_STATE_AGENT_STARTING',
k_STATE_AGENT_DISCRETE: 'k_STATE_AGENT_DISCRETE',
k_STATE_AGENT_SEARCHING: 'k_STATE_AGENT_SEARCHING',
k_STATE_AGENT_MINING: 'k_STATE_AGENT_MINING',
k_STATE_AGENT_WARING: 'k_STATE_AGENT_WARING'
}
STATE_MOVE=[
[ 'SILENCE' , 'TORPEDO' , 'MINE' , 'SONAR' ],
[ 'TORPEDO' , 'SILENCE' , 'MINE' , 'SONAR' ],
[ 'SILENCE' , 'SONAR' , 'MINE' , 'TORPEDO' ],
[ 'MINE' , 'SILENCE' , 'TORPEDO' , 'SONAR' ],
[ 'TORPEDO' , 'MINE' , 'SILENCE' , 'SONAR' ],
]
PREVIOUS_SONAR = 0
WIDTH, HEIGHT, MY_ID, OPP_ID = 0 , 0, 0, 0
TREASURE_MAP = []
STARTING_SYMBOLS = 'S'
FINISHING_SYMBOLS = 'F'
OBSTACLE_SYMBOL = 'x'
EMPTY_SYMBOLS = '.'
DIRS = [('N',-1, 0), ('S',1, 0), ('E',0, 1), ('W',0, -1)]
GET_DIRS = { (-1,0) : 'N' , (1,0) : 'S' , (0,1) : 'E' , (0,-1) : 'W' }
OPP_DIRS = { 'N' : ('S',1, 0) , 'S' : ('N',-1, 0) , 'E' : ('W',0, -1) , 'W' : ('E',0, 1) }
OPPONENT_SET = set()
k_PATH_HAMILTON = 1
k_PATH_FIRST = 2
k_PATH_LAST = 3
k_PATH_MAX = 4
# TODO: Can be improved
SECTOR_REDUCING = {
(6,3):(6,3,2,1,4,7,8,9,0),(6,9):(6,9,8,7,4,1,2,3,0),
(8,9):(8,9,6,3,2,1,4,7,0),(8,7):(8,7,4,1,2,3,6,9,0),
(4,7):(4,7,8,9,6,3,2,1,0),(4,1):(4,1,2,3,6,9,8,7,0),
(2,1):(2,1,4,7,8,9,6,3,0),(2,3):(2,3,6,9,8,7,4,1,0)
}
SECTOR_TRANSIT = {
5: (6,8,4,2),6: (3,9),8: (9,7),4: (7,1),2: (1,3),
1: (2,4), 3: (2,6), 7:(4,8), 9:(8,6)
}
SILENCE_NEED_FUSION = 0
def read_map():
global WIDTH, HEIGHT, MY_ID, OPP_ID
WIDTH, HEIGHT, MY_ID = [int(i) for i in input().split()]
OPP_ID = (MY_ID + 1) % 2
global TREASURE_MAP
for i in range(HEIGHT):
TREASURE_MAP.append(list(input()))
def t_check_map(TREASURE_MAP):
for i in range(HEIGHT):
print(TREASURE_MAP[i],file=sys.stderr)
DEEP_SILENCE2 = 41
SEARCH_OPP_TRIGGER = 3
SEARCH_OPP_TRIGGER_LOT = 11
class Node:
def __init__(self,clone):
self.x, self.y = 0, 0
self.privileged_dir, self.possible_dir = None, None
if clone is not None:
self.x, self.y = clone.x, clone.y
self.privileged_dir, self.possible_dir = self.privileged_dir, self.possible_dir
def __str__(self):
return f'(x:{self.x},y:{self.y} poss {self.possible_dir} priv {self.privileged_dir}'
def set_up(self,x_col,y_row):
self.x, self.y = x_col, y_row
def update_dir(self,solving,DIRS):
REDUCE_MAP = solving.grid
self.privileged_dir = copy.copy(DIRS)
self.possible_dir = copy.copy(DIRS)
legal = solving.legal
results = [dir for dir in DIRS if is_direction_legal(self,legal,dir)]
coord = self.y, self.x
length = len(results)
if len(results) == 0 :
y_row, x_col = coord
solving.delete.append(coord)
REDUCE_MAP[self.y][self.x] = 'x'
elif length <= 1 :
y_row, x_col = coord
d1, y_drow, x_dcol = results[0]
solving.delete.append(coord)
coord = y_row + y_drow, x_col + x_dcol
if coord not in solving.dirunknow :
solving.dirunknow.append(coord)
REDUCE_MAP[self.y][self.x] = 'x'
elif length == 2 :
self.possible_dir = copy.copy(results)
self.privileged_dir = None
solving.risk.append(coord)
solving.legal.update( { coord : self } )
else :
self.possible_dir = copy.copy(results)
self.privileged_dir = None
solving.legal.update( { coord : self } )
return REDUCE_MAP
def is_direction_legal(point,legal,dir):
orientation, y_drow, x_dcol = dir
coord = point.y + y_drow, point.x + x_dcol
return True if coord in legal else False
class PathSolving:
"""Solver for a Hamilton Path problem."""
def __init__(self, clone):
"""Initialize the HamiltonSolver instance from a grid, which must be a
list of strings, one for each row of the grid.
"""
self.grid = None
self.legal = {} #set()
self.start = None
self.dirunknow = []
self.risk = []
self.delete = []
self.sector = [None] * 10
self.last = None
if clone is not None :
self.grid = clone.grid
self.legal = clone.legal
self.start = clone.start
self.risk = clone.risk
def set_up(self,TREASURE_MAP):
self.grid = copy.copy(TREASURE_MAP)
self.start = None
self.legal = {} #set()
self.risk = []
for r, row in enumerate(self.grid):
for c, item in enumerate(row):
if item in EMPTY_SYMBOLS:
self.legal.update( { (r,c) : None } )
#self.legal.add((r, c))
def reset(self):
# TODO: Trop naze cette fonction...
self.legal = {} #set()
for r, row in enumerate(self.grid):
for c, item in enumerate(row):
if item in EMPTY_SYMBOLS:
self.legal.add((r, c))
def update(self):
for l1 in self.legal:
y_row, x_col = l1
n1 = Node(None)
n1.set_up(x_col,y_row)
self.grid = n1.update_dir(self,DIRS)
for coord in self.delete:
del self.legal[coord]
self.delete.clear()
for coord in self.dirunknow:
if coord not in self.legal :
continue
n1 = self.legal[coord]
y_row, x_col = coord
results = [dir for dir in n1.possible_dir if is_direction_legal(n1,self.legal,dir)]
n1.possible_dir = results
if len(n1.possible_dir) <= 2 :
self.risk.append(coord)
self.dirunknow.clear()
for coord in self.risk:
self.risk.pop(0)
self.update_risk(coord)
def update_risk(self,k_coord):
y_row, x_col = k_coord
if k_coord not in self.legal:
self.grid[y_row][x_col] = 'x'
return
if len(self.legal[k_coord].possible_dir) == 1 :
self.grid[y_row][x_col] = 'x'
del self.legal[k_coord]
return
MIN_REQUIRE_DEEP = 6
soluce = 0
start_time = time.time()
r , c = self.legal[k_coord].y, self.legal[k_coord].x
the_first_node = self.legal[k_coord]
for d1 in the_first_node.possible_dir:
direction, y1_drow, x1_dcol = d1
start = r + y1_drow, c + x1_dcol
legal = copy.copy(self.legal)
coord = r , c
del legal[coord]
# The next risky cell has been already deleted
# So we can delete this test and check next
if start not in legal :
return
n1 = legal[start]
coord = n1.y , n1.x
del legal[coord]
path = [n1]
iter_dir = [iter(n1.possible_dir)]
iter_delete = [n1]
path_append = path.append
path_pop = path.pop
iter_dir_extend = iter_dir.extend
iter_dir_append = iter_dir.append
iter_dir_pop = iter_dir.pop
while path:
n1 = path[-1]
y_row, x_col = n1.y , n1.x
for d1 in iter_dir[-1]:
orientation, y_drow, x_dcol = d1
new_coord = y_row + y_drow, x_col + x_dcol
if new_coord in legal and len(legal[new_coord].possible_dir) > 0 :
n1 = legal[new_coord]
path_append(n1)
del legal[new_coord]
if n1 not in iter_delete:
iter_delete.append(n1)
iter_dir_append(iter(n1.possible_dir))
if len(path) > MIN_REQUIRE_DEEP :
soluce += 1
# Recompute fucking iter
break
else:
iter_dir.pop()
n1 = path_pop()
coord = n1.y , n1.x
legal[coord] = n1
if len(path) > MIN_REQUIRE_DEEP:
break
if len(path) == 0 :
for n1 in iter_delete:
y_row, x_col = n1.y, n1.x
iter_delete_coord = n1.y, n1.x
self.grid[y_row][x_col] = 'x'
del self.legal[iter_delete_coord]
d2 = OPP_DIRS[direction]
risk = the_first_node.y + y1_drow, the_first_node.x + x1_dcol
if risk in self.legal:
self.legal[risk].possible_dir.remove(d2)
self.risk.append(risk)
y_row, x_col = k_coord
self.grid[y_row][x_col] = 'x'
del self.legal[k_coord]
break
def update_sector(self):
for i1 in range(0,10):
self.sector[i1] = {}
for c1,n1 in self.legal.items():
sector_id = sector(n1)
c1 = n1.y, n1.x
self.sector[sector_id][c1] = n1
def next_sector(self,path):
self.last = path[-1]
k_last_coord = self.last.y, self.last.x
k_last_sector = sector(self.last)
del self.sector[k_last_sector][k_last_coord]
def solve_sector(self,next_sector):
save = []
max_find = False
first_find = False
save_type = 0
for type, path in self.solve(next_sector):
if type == k_PATH_HAMILTON :
save_type = type
save = path
break
if type == k_PATH_MAX :
max_find = True
save_type = type
save = copy.copy(path)
if max_find == False and type == k_PATH_FIRST :
first_find = True
save_type = type
save = copy.copy(path) # This data is ephemerate
if max_find == False and first_find == False and type == k_PATH_LAST :
save_type = type
save = copy.copy(path) # This data is ephemerate
return save_type, save
def coord_random(self):
node = random.choice(list(self.legal))
return node
def read_turn(self,path,turn):
return path[turn]
def solve(self,sector_end):
y_row, x_col = self.last.y, self.last.x
sector_start = sector(self.last)
k_coord = y_row, x_col
SECTOR_DEEP = len(self.sector[sector_start])
DISTANCE_PATH = 0
n1 = self.last
coord1 = n1.y , n1.x
d1 = n1.possible_dir
path = [n1]
iter_dir = [iter(n1.possible_dir)]
path_append = path.append
path_pop = path.pop
iter_dir_extend = iter_dir.extend
iter_dir_append = iter_dir.append
iter_dir_pop = iter_dir.pop
first = 0
search_path_max = 10
search_path = None
while path:
n1 = path[-1]
y_row, x_col = n1.y , n1.x
for d1 in iter_dir[-1]:
orientation, y_drow, x_dcol = d1
new_coord = y_row + y_drow, x_col + x_dcol
if new_coord in self.sector[sector_start] :
n1 = self.sector[sector_start][new_coord]
path_append(n1)
del self.sector[sector_start][new_coord]
if len(self.sector[sector_start]) == 0 :
yield (k_PATH_LAST,path)
iter_dir_append(iter(n1.possible_dir))
# Recompute fucking iter
break
for k_end in sector_end:
if new_coord in self.sector[k_end] and len(self.sector[sector_start]) == 0 :
y_row, x_col = new_coord
n1 = self.sector[k_end][new_coord]
path_append(n1)
yield (k_PATH_HAMILTON, path)
return
if new_coord in self.sector[k_end] and first == 0:
first = 1
path_append(self.sector[k_end][new_coord])
yield (k_PATH_FIRST, path)
path_pop()
if new_coord in self.sector[k_end] and len(path) > search_path_max :
search_path = copy.copy(path)
search_path.append(self.sector[k_end][new_coord])
search_path_max = len(search_path)
else:
iter_dir.pop()
n1 = path_pop()
coord = n1.y , n1.x
self.sector[sector_start][coord] = n1
if search_path_max > 10 :
yield (k_PATH_MAX, search_path)
class LegalTorpedo():
def __init__(self,clone):
self.reducing_map = None
self.legal = set()
def set_up(self,REDUCING_MAP):
self.reducing_map = REDUCING_MAP
for y_row, row in enumerate(self.reducing_map):
for x_col, item in enumerate(row):
if item in EMPTY_SYMBOLS:
self.legal.add( (y_row, x_col) )
class MineAndTrigger():
def __init__(self,clone):
self.treasure_map = None
self.minefield = set()
self.legal = set()
if clone is not None :
self.minefield = clone.minefield
self.legal = clone.legal
self.treasure_map = clone.treasure_map
def set_up(self,TREASURE_MAP):
self.treasure_map = TREASURE_MAP
for y_row, row in enumerate(self.treasure_map):
for x_col, item in enumerate(row):
if item in EMPTY_SYMBOLS:
self.legal.add( (y_row, x_col) )
def mine(self,board):
dirs = [iter(DIRS)]
orientation, y_drow, x_dcol = '', 0, 0
for orientation, y_drow, x_dcol in dirs[-1]:
new_coord = board.y + y_drow, board.x + x_dcol
if new_coord in self.legal:
self.legal.remove(new_coord)
mine = Point(board.x + x_dcol,board.y + y_drow)
self.minefield.add(mine)
break
else:
return None
return orientation
def trigger(self,mine):
new_coord = mine.y, mine.x
self.legal.add(new_coord)
self.minefield.remove(mine)
def nearby(self,my_board,opp_board):
for mine in self.minefield:
if square(mine,my_board) == True:
continue
if square(mine,opp_board) == True:
return mine
return None
def __iter__(self):
for m1 in self.minefield:
yield m1
class StalkAndLegal():
def __init__(self,clone):
self.legal = set()
if clone is not None:
self.legal = copy.copy(clone.legal)
def __str__(self):
text = ''
for y_row, x_col in self.legal:
text = '{} ({},{})'.format(text,x_col,y_row)
return text
def set_up(self,TREASURE_MAP):
i1 = 0
for y_row, row in enumerate(TREASURE_MAP):
for x_col, item in enumerate(row):
if item in EMPTY_SYMBOLS:
self.legal.add((y_row, x_col))
def read_move(self,point,dy_row,dx_col):
new_coord = point.y + dy_row , point.x + dx_col
if new_coord in self.legal:
self.legal.remove(new_coord)
return True
return False
def read_surface(self,submarine):
self.legal.clear()
self.set_up(submarine.treasure_map)
new_coord = submarine.y , submarine.x
if new_coord in self.legal:
self.legal.remove(new_coord)
def read_silence(self,board1,length1,dy_row,dx_col):
legal = self.legal
legal_remove = self.legal.remove
for i1 in range(1,length1+1):
new_coord = board1.y + i1 * dy_row , board1.x + i1 * dx_col
if new_coord in legal:
legal_remove(new_coord)
else :
return False
return True
# In fact, it's the second method
STALKING_SILENCE = {
'E':( (-4,0),(-3,0),(-2,0),(-1,0),(0,1),(0,2),(0,3),(0,4),(1,0),(2,0),(3,0),(4,0) ),
'W':( (-4,0),(-3,0),(-2,0),(-1,0),(0,-1),(0,-2),(0,-3),(0,-4),(1,0),(2,0),(3,0),(4,0) ),
'N':( (0,-1),(0,-2),(0,-3),(0,-4),(-4,0),(-3,0),(-2,0),(-1,0),(0,1),(0,2),(0,3),(0,4) ),
'S':( (0,-1),(0,-2),(0,-3),(0,-4),(4,0),(3,0),(2,0),(1,0),(0,1),(0,2),(0,3),(0,4) )
}
# With DICTIONNARY, LIST, TUPLE of TUPLE
STALKING_SILENCE3 = {
'E':[
( (-1,0),(-2,0),(-3,0),(-4,0) ),
( (0,1),(0,2),(0,3),(0,4) ),
( (1,0),(2,0),(3,0),(4,0) )
],
'W':[
( (-1,0),(-2,0),(-3,0),(-4,0) ),
( (0,-1),(0,-2),(0,-3),(0,-4) ),
( (1,0),(2,0),(3,0),(4,0) )
],
'N':[
( (0,-1),(0,-2),(0,-3),(0,-4) ),
( (-1,0),(-2,0),(-3,0),(-4,0) ),
( (0,1),(0,2),(0,3),(0,4) )
],
'S':[
( (0,-1),(0,-2),(0,-3),(0,-4) ),
( (1,0),(2,0),(3,0),(4,0) ),
( (0,1),(0,2),(0,3),(0,4) )
]
}
STALKING_SILENCE_SECTOR = {
(0,'E') : (1,2,3,4,5,6,7,8,9) , (1,'E') : (2,3,5,6,8,9) , (2,'E') : (3,6,9) ,
(0,'W') : (1,2,3,4,5,6,7,8,9) , (1,'W') : (1,2,4,5,7,8) , (2,'W') : (1,4,7) ,
(0,'N') : (1,2,3,4,5,6,7,8,9) , (1,'N') : (1,2,3,4,5,6) , (2,'N') : (1,2,3) ,
(0,'S') : (1,2,3,4,5,6,7,8,9) , (1,'S') : (4,5,6,7,8,9) , (2,'S') : (7,8,9)
}
class StalkAndTorpedo():
def __init__(self,clone):
self.treasure_map = None
self.out = set()
self.inp = set()
self.previous_move = ''
self.previous_nb = 0
if clone is not None :
self.treasure_map, self.inp = clone.treasure_map, clone.out
self.previous_move = clone.previous_move
self.previous_nb = clone.previous_nb
def __str__(self):
text = ''
for board_in, stalk_in in self.inp:
text = '{}\n({},{},life{})'.format(text,board_in.x, board_in.y, board_in.life)
return text
def set_up(self,tuple_sector,TREASURE_MAP):
inp_add = self.inp.add
new_board, new_stalk = None, None
self.treasure_map = TREASURE_MAP
for y_row, row in enumerate(self.treasure_map):
for x_col, item in enumerate(row):
sector = 1 + (x_col // 5) + ( (y_row // 5) * 3 )
if sector not in tuple_sector :
continue
if item in EMPTY_SYMBOLS:
new_board = Board(None)
new_board.x, new_board.y = x_col, y_row
new_stalk = StalkAndLegal(None)
new_stalk.set_up(self.treasure_map)
inp_add((new_board,new_stalk))
def update(self,action,data):
action(self,data)
def read_move(self,data):
data = data[0]
d, dy_row, dx_col = next( result_dir for result_dir in DIRS if data in result_dir )
self.previous_nb = (self.previous_nb + 1) if (self.previous_move == d) else 0
self.previous_move = d
for board,stalk in self.inp:
result = stalk.read_move(board,dy_row,dx_col)
if result == True :
board.x, board.y = board.x + dx_col, board.y + dy_row
self.out.add( (board,stalk) )
def read_surface(self,data):
for board, stalk in self.inp:
board.life -= 1
if board.life > 0 :
# Reset TREASURE MAP for board, don't know If it's very necessary
#board.treasure_map = copy.deepcopy(TREASURE_MAP)
board.treasure_map = copy.copy(TREASURE_MAP)
stalk.read_surface(board)
self.out.add( (board,stalk) )
def read_surface2(self,data):
print("Read surface 2 {}".format(int(data[0])),file=sys.stderr)
for board, stalk in self.inp:
if int(data[0]) == sector(board):
#board.treasure_map = copy.deepcopy(TREASURE_MAP)
board.treasure_map = copy.copy(TREASURE_MAP)
stalk.read_surface(board)
self.out.add( (board,stalk) )
def read_torpedo(self,data):
x, y = int(data[0]), int(data[1])
point = Point(x,y)
for board, stalk in self.inp:
distance = manhattan(board,point)
if distance <= 4 :
self.out.add( (board,stalk) )
def read_trigger(self,data):
x, y = int(data[0]), int(data[1])
point = Point(x,y)
for board,stalk in self.inp:
if not square(point,board):
self.out.add( (board,stalk) )
def read_silence2(self,data):
global SILENCE_NEED_FUSION
inp = self.inp
out_add = self.out.add
if len(self.inp) >= DEEP_SILENCE2:
SILENCE_NEED_FUSION = 0
self.inp.clear()
identical_movement_number = self.previous_nb // 5
tuple_sector = STALKING_SILENCE_SECTOR[(identical_movement_number,self.previous_move)]
self.set_up(tuple_sector,self.treasure_map)
self.out = self.inp
else :
SILENCE_NEED_FUSION = 1
for board, stalk in inp:
out_add( (board, stalk))
t_stalking_silence = STALKING_SILENCE3[self.previous_move]
for tuple_dir in t_stalking_silence:
for y_drow,x_dcol in tuple_dir:
k_coord = board.y + y_drow, board.x + x_dcol
if k_coord in stalk.legal :
board1, stalk1 = Board(board), StalkAndLegal(stalk)
board1.y += y_drow
board1.x += x_dcol
out_add( (board1,stalk1) )
else :
break
def silence_fusion(self):
inp = self.inp
dico_coord = {}
out_add = self.out.add
for board, stalk in inp:
k_coord = board.y , board.x
if k_coord in dico_coord :
dico_coord[k_coord].append( (board, stalk) )
else:
dico_coord[k_coord] = [ (board,stalk) ]
for k_coord, d1 in dico_coord.items():
board, stalk = None, StalkAndLegal(None)
legal_add = stalk.legal.add
for board1,stalk1 in d1:
if board == None:
board = board1
for coord1 in stalk1.legal:
legal_add(coord1)
out_add( (board,stalk) )
def update_sonar(self,sonar_result,previous):
if sonar_result == 'Y':
for board, stalk in self.inp:
if previous == sector(board):
board.treasure_map = copy.copy(TREASURE_MAP)
stalk.read_surface(board)
self.out.add( (board,stalk) )
else:
for board, stalk in self.inp:
if previous != sector(board):
board.treasure_map = copy.copy(TREASURE_MAP)
stalk.read_surface(board)
self.out.add( (board,stalk) )
READ_COMMAND = [
( 'MOVE' , StalkAndTorpedo.read_move ),
( 'SURFACE' , StalkAndTorpedo.read_surface2 ),
( 'TORPEDO' , StalkAndTorpedo.read_torpedo ),
( 'SONAR' , None ),
( 'SILENCE' , StalkAndTorpedo.read_silence2 ),
( 'MINE' , None ),
( 'TRIGGER' , StalkAndTorpedo.read_trigger ),
]
MINE_MAP = []
#MINE_MAP.append(list(' '))
#MINE_MAP.append(list(' . . . . . . '))
#MINE_MAP.append(list(' . . . '))
#MINE_MAP.append(list(' . . . . . . '))
#MINE_MAP.append(list(' '))
#MINE_MAP.append(list(' '))
#MINE_MAP.append(list(' '))
#MINE_MAP.append(list(' '))
#MINE_MAP.append(list(' '))
#MINE_MAP.append(list(' '))
MINE_MAP.append(list(' '))
MINE_MAP.append(list(' . . . . . . '))
MINE_MAP.append(list(' . . . '))
MINE_MAP.append(list(' . . . . . . '))
MINE_MAP.append(list(' . . '))
MINE_MAP.append(list(' '))
MINE_MAP.append(list(' . . . . . . '))
MINE_MAP.append(list(' . . . '))
MINE_MAP.append(list(' . . . . . . '))
MINE_MAP.append(list(' '))
MINE_MAP.append(list(' . . '))
MINE_MAP.append(list(' . . . . . . '))
MINE_MAP.append(list(' . . . '))
MINE_MAP.append(list(' . . . . . . '))
MINE_MAP.append(list(' '))
class Point():
def __init__(self,x,y):
self.x, self.y = x, y
def __str__(self):
return f'({self.x},{self.y})'
class Mine(Point):
def __init__(self,x,y):
super().__init__(x,y)
self.nb = 0
class Submarine():
def __init__(self,clone):
self.out = ''
self.treasure_map = TREASURE_MAP
if clone is not None :
self.x, self.y = clone.x, clone.y
else :
self.x, self.y = 0, 0
def write_move(self,direction,load):
t = f'MOVE {direction} {load}'
self.out = t if self.out == '' else f'{self.out} | {t}'
def write_silence(self,direction,length):
t = f'SILENCE {direction} {length}'
self.out = t if self.out == '' else f'{self.out} | {t}'
def write_surface(self):
self.out = 'SURFACE' if self.out == '' else f'{self.out} | SURFACE'
def write_torpedo(self,x,y):
t = f'TORPEDO {x} {y}'
self.out = t if self.out == '' else f'{self.out} | {t}'
def write_mine(self,direction):
t = f'MINE {direction}'
self.out = t if self.out == '' else f'{self.out} | {t}'
def write_trigger(self,x,y):
t = f'TRIGGER {x} {y}'
self.out = t if self.out == '' else f'{self.out} | {t}'
def write_sonar(self,sector):
t = f'SONAR {sector}'
self.out = t if self.out == '' else f'{self.out} | {t}'
def order(self):
print(self.out)
class Board(Submarine):
def __init__(self,clone):
super().__init__(clone)
self.turn = 0
self.x , self.y , self.life = 0, 0, 6
self.torpedo, self.sonar, self.silence, self.mine = 0, 0, 0, 0
self.need_surface = False
self.legal_torpedo = None
self.unknow_opp = True
self.nearest = False
self.delayed_torpedo = False
if clone is not None:
self.turn = clone.turn + 1
self.x, self.y, self.life = clone.x, clone.y, clone.life
self.torpedo, self.sonar = clone.torpedo, clone.sonar
self.silence, self.mine = clone.silence, clone.mine
self.need_surface = clone.need_surface
self.legal_torpedo = clone.legal_torpedo # Only not lost link
self.unknow_opp = clone.unknow_opp
self.nearest = clone.nearest
self.delayed_torpedo = clone.delayed_torpedo
def __str__(self):
return '({},{})'.format(self.x,self.y)
def update(me,opp):
me.x, me.y, me.life, opp.life, me.torpedo, me.sonar, me.silence, me.mine = [int(i) for i in input().split()]
def update_order(text):
text, t1 = text.split('|'), ''
for t1 in text:
try:
c1, f1 = next( (c1,f1) for c1,f1 in READ_COMMAND if t1.find(c1) != -1 )
t_list = t1.split(' ')
_,d1 = t_list[0], t_list[1:]
yield c1, f1, d1
except:
return
def update_agent(state,board):
STATE_CHECK = {
'SILENCE' : board.silence, 'SONAR' : board.sonar,
'TORPEDO' : board.torpedo, 'MINE' : board.mine
}
info = STATE_MOVE[state]
text = ''
try:
text = next(n1 for n1 in info if STATE_CHECK[n1] != 0)
except:
text = 'TORPEDO'
return text
def sector(obj1):
return 1 + (obj1.x // 5) + ( (obj1.y // 5) * 3 )
def manhattan(obj1,obj2):
distance = abs(obj1.x - obj2.x) + abs(obj1.y - obj2.y)
return distance
def square(obj1,obj2):
if abs(obj1.x - obj2.x) > 1 :
return False
if abs(obj1.y - obj2.y) > 1 :
return False
return True
def square2(mine,my_board,opp_board):
if square(my_board,mine) == True :
return False
if square(opp_board,mine) == True :
return True
return False
def average_x( triple_x , obj1 ):
min_x, ave_x, max_x = triple_x
min_x = min(min_x, obj1.x)
max_x = max(max_x, obj1.x)
ave_x = ave_x + obj1.x
return (min_x, ave_x, max_x)
def average_y( triple_y , obj1 ):
min_y, ave_y, max_y = triple_y
min_y = min(min_y, obj1.y)
max_y = max(max_y, obj1.y)
ave_y = ave_y + obj1.y
return (min_y, ave_y, max_y)
OBSERVER_TORPEDO = ( (-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1) )
BOARD_OBSERVER_TORPEDO = (
(-4,0),
(-3,-1),(-3,0),(-3,1),
(-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),
(-1,-3),(-1,-2),(-1,2),(-1,3),
(0,-4),(0,-3),(0,-2),(0,2),(0,3),(0,4),
(1,-3),(1,-2),(1,2),(1,3),
(2,-2),(2,-1),(2,0),(2,1),(2,2),
(3,-1),(3,0),(3,1),
(4,0)
)
class TorpedoObserver():
def __init__(self,clone):
self.me = True
self.dict_torpedo = {}
for y_drow, x_dcol in BOARD_OBSERVER_TORPEDO:
self.dict_torpedo[(y_drow, x_dcol)] = 0
self.torpedo = (-1,-1)
def __str__(self):
y, x = self.torpedo
return f'Torpedo Obs: ({y},{x})'
def reset(self):
for y_drow, x_dcol in BOARD_OBSERVER_TORPEDO:
self.dict_torpedo[(y_drow, x_dcol)] = 0
self.torpedo = (-1,-1)
def notify_iter(self,submarine,kanban_mine,k_board_opp,k_stalk_opp):
distance = manhattan(submarine,k_board_opp)
if distance > 5 :
pass
else :
y_diff = k_board_opp.y - submarine.y
x_diff = k_board_opp.x - submarine.x
if (y_diff,x_diff) in self.dict_torpedo:
self.dict_torpedo[(y_diff,x_diff)] += 6
for y_drow, x_dcol in OBSERVER_TORPEDO:
y_diff_drow,x_diff_dcol = y_diff + y_drow, x_diff + x_dcol
y1_ = k_board_opp.y + y_drow
x1_ = k_board_opp.x + x_dcol
if (y1_,x1_) not in submarine.legal_torpedo.legal :
continue
if (y_diff_drow,x_diff_dcol) in self.dict_torpedo :
if self.dict_torpedo[(y_diff_drow,x_diff_dcol)] == 0 and distance == 5 :
continue
self.dict_torpedo[(y_diff_drow,x_diff_dcol)] += 1
return True
def notify_else(self,submarine,kanban_mine,kanban_opp):
y_drow,x_dcol = max(self.dict_torpedo, key = self.dict_torpedo.get)
if submarine.torpedo == 0 :
if self.dict_torpedo[(y_drow,x_dcol)] > 0 :
y_row, x_col = submarine.y + y_drow, submarine.x + x_dcol
submarine.write_torpedo(x_col,y_row)
submarine.torpedo = 3
self.torpedo = (y_row,x_col)
return True
return False
elif submarine.torpedo == 1 :
if self.dict_torpedo[(y_drow,x_dcol)] > 0 :
y_row, x_col = submarine.y + y_drow, submarine.x + x_dcol
submarine.delayed_torpedo = True
self.torpedo = (y_row,x_col)
return True
return False
return False
def notify_check(self,submarine,kanban_mine,kanban_opp):
k_coord = self.torpedo
y_torpedo, x_torpedo = k_coord
if submarine.torpedo == 0 :