organic_compound.py 636 B

12345678910111213141516
  1. '''
  2. > https://www.codingame.com/ide/puzzle/organic-compounds
  3. @author: olivier.massot, 2019
  4. '''
  5. import re
  6. compounds = [re.findall("\(\d\)|CH\d|0", input().replace(" ", "0")) for i in range(int(input()))]
  7. values = [[int(re.search(r"\d", c)[0]) for c in row] for row in compounds]
  8. def getval(x, y):
  9. return values[y][x] if 0 <= y < len(values) and 0 <= x < len(values[y]) else 0
  10. valid = all((sum([getval(x, y), getval(x - 1, y), getval(x + 1, y), getval(x, y - 1), getval(x, y + 1)]) == 4 \
  11. for y, row in enumerate(compounds) for x, v in enumerate(row) if v[0] == 'C'))
  12. print("VALID" if valid else "INVALID")