Advertisement
philRG

Starter OOC reloaded 2021

Nov 4th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. import random
  2. import sys
  3. import math
  4. import time
  5. from copy import deepcopy
  6.  
  7. from typing import Tuple, List, Optional
  8.  
  9.  
  10. def idebug(*args):
  11.     # return
  12.     print(*args, file=sys.stderr, flush=True)
  13.  
  14.  
  15. def debug(*args):
  16.     # return
  17.     print(*args, file=sys.stderr, flush=True)
  18.  
  19.  
  20. def dist(x, y, X, Y):
  21.     return abs(X - x) + abs(Y - y)
  22.  
  23.  
  24. def get_directions(grid, x, y):
  25.     directions = []
  26.     for _dir, (dx, dy) in DIRECTIONS.items():
  27.         new_x, new_y = x + dx, y + dy
  28.         if not (0 <= new_x < width and 0 <= new_y < width) or grid[new_y][new_x] != '.' or (new_x, new_y) in visited:
  29.             continue
  30.         directions.append(_dir)
  31.     return directions
  32.  
  33. def bfs(grid, submarine, phantom):
  34.     x, y = submarine
  35.     X, Y = phantom
  36.  
  37.  
  38. # Auto-generated code below aims at helping you parse
  39. # the standard input according to the problem statement.
  40.  
  41. DIRECTIONS = {'E': (1, 0), 'S': (0, 1), 'W': (-1, 0), 'N': (0, -1)}
  42.  
  43. width, height, my_id = [int(i) for i in input().split()]
  44. idebug(width, height, my_id)
  45.  
  46. grid = []
  47. for i in range(height):
  48.     line = input()
  49.     idebug(line)
  50.     grid.append(list(line))
  51.  
  52. # Write an action using print
  53. # To debug: print("Debug messages...", file=sys.stderr, flush=True)
  54.  
  55. start_positions = [(x, y) for x in range(height) for y in range(width) if grid[y][x] == '.']
  56. start_x, start_y = random.choice(start_positions)
  57.  
  58. print(start_x, start_y)
  59.  
  60. phantoms = {i + width * j: [(i, j)] for j in range(15) for i in range(15) if grid[i][j] == '.'}
  61. debug(f'{len(phantoms)} phantoms: {phantoms}')
  62.  
  63. visited = []
  64. # game loop
  65. while True:
  66.     line = input()
  67.     idebug(line)
  68.     x, y, my_life, opp_life, torpedo_cooldown, sonar_cooldown, silence_cooldown, mine_cooldown = [int(i) for i in line.split()]
  69.     sonar_result = input()
  70.     idebug(sonar_result)
  71.     opponent_orders = input()
  72.     idebug(opponent_orders)
  73.  
  74.     visited.append((x, y))
  75.  
  76.     """Orders parsing"""
  77.     op_torpedo = None
  78.     if opponent_orders != 'NA':
  79.         orders = opponent_orders.split('|')
  80.         debug(orders)
  81.         for order in orders:
  82.             order = order.split()
  83.             if order[0] == 'TORPEDO':
  84.                 op_torpedo = list(map(int, order[1:3]))
  85.                 debug(op_torpedo)
  86.             else:
  87.                 phantom_dir = order[1]
  88.  
  89.         debug(f'{len(phantoms)} phantoms: {phantoms}')
  90.  
  91.         """Updates phantom possible positions"""
  92.         for i in range(225):
  93.             if i in phantoms:
  94.                 x, y = phantoms[i][-1]
  95.                 dx, dy = DIRECTIONS[phantom_dir]
  96.                 new_x, new_y = x + dx, y + dy
  97.                 if not (0 <= new_x < width and 0 <= new_y < width) or grid[new_y][new_x] != '.' or (op_torpedo and dist(x, y, *op_torpedo) > 4):
  98.                     del phantoms[i]
  99.                     continue
  100.                 phantoms[i].append((new_x, new_y))
  101.  
  102.  
  103.         debug(orders)
  104.  
  105.         debug(f'{len(phantoms)} phantoms: {phantoms}')
  106.  
  107.     moves = get_directions(grid, x, y)
  108.  
  109.     if moves:
  110.         move = random.choice(moves)
  111.         print(f'MOVE {move} TORPEDO')
  112.     else:
  113.         print('SURFACE')
  114.         visited.clear()
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement