2016-10-22 20:34:23 +02:00
|
|
|
import copy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Point(object):
|
|
|
|
|
def __init__(self, x, y):
|
|
|
|
|
self.x = x
|
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return 'Point({}:{})'.format(self.x, self.y)
|
|
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
|
return Point(self.x + other.x, self.y + other.y)
|
|
|
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
|
return Point(self.x - other.x, self.y - other.y)
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
return self.x == other.x and self.y == other.y
|
|
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
2017-04-02 17:33:57 +02:00
|
|
|
return not (self == other)
|
2016-10-22 20:34:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
DIRECTIONS = (Point(1, 0), Point(1, -1), Point(1, 1), Point(-1, -1),
|
|
|
|
|
Point(0, -1), Point(0, 1), Point(-1, 1), Point(-1, 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WordSearch(object):
|
|
|
|
|
def __init__(self, puzzle):
|
|
|
|
|
self.rows = puzzle.split()
|
|
|
|
|
self.width = len(self.rows[0])
|
|
|
|
|
self.height = len(self.rows)
|
|
|
|
|
|
|
|
|
|
def find_char(self, coordinate):
|
|
|
|
|
if coordinate.x < 0 or coordinate.x >= self.width:
|
|
|
|
|
return
|
|
|
|
|
if coordinate.y < 0 or coordinate.y >= self.height:
|
|
|
|
|
return
|
|
|
|
|
return self.rows[coordinate.y][coordinate.x]
|
|
|
|
|
|
|
|
|
|
def find(self, word, position, direction):
|
2017-04-02 17:33:57 +02:00
|
|
|
current = copy.copy(position)
|
|
|
|
|
for letter in word:
|
|
|
|
|
if self.find_char(current) != letter:
|
|
|
|
|
return
|
|
|
|
|
current += direction
|
|
|
|
|
return position, current - direction
|
2016-10-22 20:34:23 +02:00
|
|
|
|
|
|
|
|
def search(self, word):
|
2017-04-02 17:33:57 +02:00
|
|
|
positions = (Point(x, y)
|
|
|
|
|
for x in range(self.width) for y in range(self.height))
|
2016-10-22 20:34:23 +02:00
|
|
|
for pos in positions:
|
|
|
|
|
for d in DIRECTIONS:
|
|
|
|
|
result = self.find(word, pos, d)
|
|
|
|
|
if result:
|
|
|
|
|
return result
|
|
|
|
|
return None
|