Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def occurences_at(posx, posy, key, atlas):
- if posy not in range(len(atlas)) or posx not in range(len(atlas[0])):
- return 0
- if atlas[posy][posx] != key[0]:
- return 0
- if len(key) == 1:
- return 1
- return sum(occurences_at(posx + i, posy + j, key[1:], atlas)
- for i in (-1, 0, 1) for j in (-1, 0, 1) if i or j)
- def count_win_occurence(my_map):
- atlas = my_map.strip().split('\n')
- ws = []
- for i, line in enumerate(atlas):
- for j, c in enumerate(line):
- if c == 'W':
- ws += [(j, i)]
- return sum(occurences_at(w[0], w[1], 'WIN', atlas) for w in ws)
- my_map = """
- +------------+
- |xxxxWxxxxxxx|
- |xxxNIIxxxxxx|
- |xxxxNxNxxxxx|
- |xxxxxxxxxxxx|
- +------------+
- """
- print(count_win_occurence(my_map))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement