-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.py
120 lines (96 loc) · 4.31 KB
/
pattern.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from helper import find_start, relocate, sorted_coords, coords_le, coords_add, coords_sub, coords_max, coords_move, coords_positive
# Visual pattern on the "lotto board". First coord should be on x = 0. Number of
# coords can be one to six.
# name: Name of the pattern (to see why it was discarded)
# coords: One up to six coords [[x, y], ...] of the pattern, values zero to six
# misses: Amount of coords that don't have to match pattern before testing.
# L, R - Rotate. F - Flip. M - Mirror: ["R", "F"]
class Pattern:
def __init__(self, name, coords, misses = 0):
self.name = name
if not coords:
raise ValueError('Pattern needs coords')
self.coords = sorted_coords(coords)
self.misses = misses
# pattern bounds min are by definition at 0,0
# so just look at the last coords for sizes
# width in coords, so 4 means 3....
self.width = max([c[0] for c in self.coords])
self.height = self.coords[-1][1]
def get_name(self):
return self.name
# sorted
def get_coords(self):
return self.coords
def get_misses(self):
return self.misses
def matches(self, tipp):
# a lot of methods from helper.py were inlined because of dramatic
# performance improvements (x2)
tipp_coords = tipp.get_coords_set()
inner_coords_max = coords_max(tipp_coords)
# debug:
#print("w %d h %d - inner max %s" % (self.width, self.height, str(inner_coords_max)))
for tipp_coord in tipp_coords:
for pattern_anchor in self.coords:
# debug:
#print("for tipp: %s pattern: %s" % (tipp_coord, pattern_anchor))
relative = [tipp_coord[0] - pattern_anchor[0], tipp_coord[1] - pattern_anchor[1]]
# debug:
#print("rw: %d rh: %d" % (inner_coords_max[0] - relative[0], inner_coords_max[1] - relative[1]))
if self.misses == 0 and ((inner_coords_max[0] - relative[0]) < self.width or (inner_coords_max[1] - relative[1]) < self.height):
continue
# debug:
#for ci in self.coords:
# print(" %s -- %s in %s?" % (str(relative), [ci[0] + relative[0], ci[1] + relative[1]], tipp_coords))
match_count = sum(tuple(c) in tipp_coords for c in ([ci[0] + relative[0], ci[1] + relative[1]] for ci in self.coords))
#print("%s %d of %d: %s" % (self.name, match_count, len(self.coords) - self.misses, (match_count >= len(self.coords) - self.misses)))
if match_count >= len(self.coords) - self.misses:
return True
return False
def __str__(self):
return self.str_pretty(1)
def __eq__(self, other):
if not isinstance(other, Pattern):
# don't attempt to compare against unrelated types
return NotImplemented
# Note: name is not compared!
# Lazy man's deep equals using str... *sigh* we only have number
# contents
return len(self.coords) == len(other.coords) and str(self.coords) == str(other.coords) and self.misses == other.misses
def __hash__(self):
# Note: name is not compared!
# wow, using str here again since python lists seem not that useful
# here (no deep hashing capabilities by default)
return hash((str(self.coords), self.misses))
def str_pretty(self, verbosity = 1):
s = ''
if verbosity > 0:
if self.name:
s += "name: %s" % self.name
if self.misses:
s += " misses: %d" % self.misses
if verbosity == 1:
s += "\n" + str(self.coords)
if verbosity > 1:
s += "\n" + self._print_sorted_coords(self.coords) + "\n"
return s
def _print_sorted_coords(self, csorted):
max_x = 0
max_y = 0
for c in csorted:
max_x = max(max_x, c[0])
max_y = max(max_y, c[1])
s = ''
c = [0,0]
for y in range(max_y + 1):
c[1] = y
for x in range(max_x + 1):
c[0] = x
if c in csorted:
s += "x"
csorted = csorted[1:]
else:
s += "."
s += "\n"
return s