1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#!/bin/env python
""" Perl Weekly challenge 077 Task 2
https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
Solution Lubos Kolouch """
class LoneX:
""" Class for solving the challenge """
def __init__(self, matrix: list):
""" init the solution """
self.matrix = matrix
self.solution_count = 0
def check_position(self, pos_x: int, pos_y: int):
""" check if the position is lone """
for d_x in [-1, 0, 1]:
for d_y in [-1, 0, 1]:
if d_x == d_y == 0:
continue
if pos_x + d_x < 0 or pos_x + d_x >= len(self.matrix):
continue
if pos_y + d_y < 0 or pos_y + d_y >= len(self.matrix):
continue
if self.matrix[pos_x + d_x][pos_y + d_y] == 'X':
return False
return True
def count_lone(self):
""" count the occurences of lone X """
for pos_x, line in enumerate(self.matrix):
for pos_y, char in enumerate(line):
if char == "O":
continue
if self.check_position(pos_x, pos_y):
self.solution_count += 1
def get_count(self):
""" just return the solutions count """
return self.solution_count
lonex = LoneX([['O', 'O', 'X'], ['X', 'O', 'O'], ['X', 'O', 'O']])
lonex.count_lone()
assert lonex.get_count() == 1
lonex2 = LoneX([['O', 'O', 'X', 'O'], ['X', 'O', 'O', 'O'],
['X', 'O', 'O', 'X'], ['O', 'X', 'O', 'O']])
lonex2.count_lone()
assert lonex2.get_count() == 2
|