Advertisement
Yanam

space_station_establishment

Feb 22nd, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. n = int(input())
  2. galaxy = [[x for x in input()] for _ in range(n)]
  3. # print(galaxy)
  4. energy = 0
  5.  
  6.  
  7. def begin(g):
  8.     for i in range(len(g)):
  9.         for j in range(len(g)):
  10.             if g[i][j] == 'S':
  11.                 s = [i, j]
  12.                 return s[0], s[1]
  13.  
  14.  
  15. def zero_position(galaxy):
  16.     p = []
  17.     for i in range(len(galaxy)):
  18.         for j in range(len(galaxy)):
  19.             if galaxy[i][j] == 'O':
  20.                 p.append(i)
  21.                 p.append(j)
  22.     return p
  23.  
  24.  
  25. def moves(c, row, col, z):
  26.     global galaxy
  27.     directions = {
  28.         'up': [row-1, col],
  29.         'down': [row+1, col],
  30.         'left': [row, col-1],
  31.         'right': [row, col+1]
  32.     }
  33.     galaxy[row][col] = '-'
  34.     if 0 <= directions[c][0] < len(galaxy) and 0 <= directions[c][1] < len(galaxy):
  35.         p_1, p_2 = directions[c][0], directions[c][1]
  36.         if galaxy[p_1][p_2] == 'O':
  37.             galaxy[p_1][p_2] = '-'
  38.             for i in range(0, len(z), 2):
  39.                 if p_1 != z[i]:
  40.                     galaxy[z[i]][z[i+1]] = 'S'
  41.                     p_1, p_2 = z[i], z[i+1]
  42.     else:
  43.         return
  44.     return p_1, p_2
  45.  
  46.  
  47. def action(r, c):
  48.     e = 0
  49.     if galaxy[r][c].isdigit() and galaxy[r][c] != '0':
  50.         e += int(galaxy[r][c])
  51.         galaxy[r][c] = 'S'
  52.     return e
  53.  
  54.  
  55. zeroes = zero_position(galaxy)
  56. start = begin(galaxy)
  57.  
  58. while True:
  59.     command = input()
  60.     try:
  61.         move_1, move_2 = moves(command, start[0], start[1], zeroes)
  62.         energy += action(move_1, move_2)
  63.         if energy >= 50:
  64.             print("Good news! Stephen succeeded in collecting enough star power!")
  65.             print(f"Star power collected: {energy}")
  66.             break
  67.     except TypeError:
  68.         print("Bad news, the spaceship went to the void.")
  69.         print(f"Star power collected: {energy}")
  70.         break
  71.     start = (move_1, move_2)
  72. [print(''.join(row)) for row in galaxy]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement