Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. def occurences_at(posx, posy, key, atlas):
  2. if posy not in range(len(atlas)) or posx not in range(len(atlas[0])):
  3. return 0
  4. if atlas[posy][posx] != key[0]:
  5. return 0
  6. if len(key) == 1:
  7. return 1
  8. return sum(occurences_at(posx + i, posy + j, key[1:], atlas)
  9. for i in (-1, 0, 1) for j in (-1, 0, 1) if i or j)
  10.  
  11. def count_win_occurence(my_map):
  12. atlas = my_map.strip().split('\n')
  13. ws = []
  14. for i, line in enumerate(atlas):
  15. for j, c in enumerate(line):
  16. if c == 'W':
  17. ws += [(j, i)]
  18. return sum(occurences_at(w[0], w[1], 'WIN', atlas) for w in ws)
  19.  
  20. my_map = """
  21. +------------+
  22. |xxxxWxxxxxxx|
  23. |xxxNIIxxxxxx|
  24. |xxxxNxNxxxxx|
  25. |xxxxxxxxxxxx|
  26. +------------+
  27. """
  28.  
  29. print(count_win_occurence(my_map))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement