Advertisement
Nenogzar

Untitled

Jun 22nd, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. def directions(position, direction, matrix):
  2. row, col = position
  3. matrix[row][col] = "-"
  4.  
  5. moves = {'up': (-1, 0),
  6. 'down': (1, 0),
  7. 'left': (0, -1),
  8. 'right': (0, 1)}
  9.  
  10. d_row, d_col = moves[direction]
  11. row = (row + d_row) % len(matrix)
  12. col = (col + d_col) % len(matrix[row])
  13.  
  14. return row, col, matrix
  15.  
  16.  
  17. field_size = int(input())
  18. bee, hive, empty = 'B', 'H', '-'
  19. bee_energy = 15
  20. field = []
  21. found_hive = False
  22. nectar, nectar_max = 0, 30
  23. energy_restored = False
  24.  
  25.  
  26. position = (-1, -1)
  27. for row_ in range(field_size):
  28. line = list(input())
  29. if bee in line:
  30. position = (row_, line.index(bee))
  31. field.append(line)
  32.  
  33. def print_matrix(matrix):
  34. for row in matrix:
  35. print("".join(map(str, row)))
  36.  
  37. while bee_energy > 0 and not found_hive:
  38. if bee_energy <= 0:
  39. break
  40.  
  41. direction = input()
  42.  
  43. new_row, new_col, field = directions(position, direction, field)
  44. bee_energy -= 1
  45. position = (new_row, new_col)
  46.  
  47. step = field[new_row][new_col]
  48. if step.isdigit():
  49. nectar += int(step)
  50. field[new_row][new_col] = empty
  51.  
  52. if nectar >= nectar_max and bee_energy < 1:
  53. if not energy_restored:
  54. energy_restored = True
  55. energy_to_restore = nectar - nectar_max
  56. bee_energy += energy_to_restore
  57. nectar = nectar_max
  58. print(f"Energy restored! Energy added: {energy_to_restore}")
  59.  
  60. elif step == hive:
  61. found_hive = True
  62. if bee_energy > 0:
  63. print(f"Great job, Beesy! The hive is full. Energy left: {bee_energy}")
  64. else:
  65. if nectar >= nectar_max:
  66. print(f"Great job, Beesy! The hive is full. Energy left: {bee_energy}")
  67. else:
  68. print("Beesy did not manage to collect enough nectar.")
  69.  
  70. if bee_energy == 0:
  71. if nectar >= nectar_max and not found_hive:
  72. if not energy_restored: # Restore energy only once
  73. energy_restored = True
  74. energy_to_restore = nectar - nectar_max
  75. bee_energy += energy_to_restore
  76. nectar = nectar_max
  77. print(f"Energy restored! Energy added: {energy_to_restore}")
  78. else:
  79. print("This is the end! Beesy ran out of energy.")
  80. break
  81. else:
  82. print("This is the end! Beesy ran out of energy.")
  83. break
  84.  
  85. field[position[0]][position[1]] = bee
  86.  
  87.  
  88. print_matrix(field)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement