kostovhg

Star Enigma

Aug 1st, 2021 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def shifttext(inp: str, shift: int):
  5.     data = []
  6.     for i in inp:
  7.         data.append(chr(ord(i) - shift))
  8.     output = ''.join(data)
  9.     return output
  10.  
  11.  
  12. star = re.compile(r"[star]", re.I)
  13. msg = re.compile(r"""([^\@\-\!\:\>]*?)          # group for any character except @, -, !, :, >
  14.                    @(?P<planet>[A-Za-z]+)      # that should be the planet name
  15.                    ([^\@\-\!\:\>]*?)           # first group
  16.                    \:\d+                       # any digits starting with :
  17.                    ([^\@\-\!\:\>]*?)           # first group
  18.                    \!(?P<type>[AD])\!          # Type, A or D
  19.                    ([^\@\-\!\:\>]*?)           # first group
  20.                    \-\>\d+""", re.X)           # digits after ->
  21. planets = {'Attacked': [], 'Destroyed': []}
  22. count = int(input())
  23.  
  24. for _ in range(count):
  25.     encrypted = input()
  26.     shift = len(re.findall(star, encrypted))
  27.     decrypted = shifttext(encrypted, shift)
  28.     m = re.search(msg, decrypted)
  29.     if m:
  30.         planet = m.group('planet')
  31.         t = m.group('type')
  32.         if t == 'A':
  33.             planets['Attacked'].append(planet)
  34.         else:
  35.             planets['Destroyed'].append(planet)
  36.  
  37. for t in planets:
  38.     ps = sorted(planets[t])
  39.     print(f"{t} planets: {len(ps)}")
  40.     [print(f"-> {p}") for p in ps]
  41.  
Add Comment
Please, Sign In to add comment