Last active
March 21, 2024 09:22
-
-
Save aflaag/229ffb287dd3b2b645102979d189169b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ex(colors, D, img_properties): | |
if img_properties == "pattern_diff_": | |
images = [] | |
pattern_diff(set(colors), D, [[]], images, (-1,), tuple(), 0, 0) | |
if D == 1: | |
return [] | |
return list(map(lambda image: tuple(map(tuple, image)), images)) | |
else: | |
return ex1(colors, D, img_properties) | |
def ex1(colors, D, img_properties): | |
if img_properties == "pattern_cross_": | |
images = set() | |
pattern_cross(colors, images, tuple(), 0) | |
return pattern_cross_images(images, D) | |
if img_properties == "pattern_hrect_": | |
images = set() | |
colors = map(lambda color: (color,), colors) | |
pattern_rect(set(colors), D, images, tuple(), -1, 0) | |
return list(map(lambda perm: tuple(map(lambda c: (c,) * D, perm)), images)) | |
if img_properties == "pattern_vrect_": | |
images = set() | |
colors = map(lambda color: (color,), colors) | |
pattern_rect(set(colors), D, images, tuple(), -1, 0) | |
return list(map(lambda perm: (perm,) * D, images)) | |
else: | |
images = set() | |
colors = map(lambda color: (color,), colors) | |
permutations(set(colors), D * D, images, tuple(), 0) | |
r = set(map(lambda i: i*D, range(D))) | |
return list(map(lambda perm: tuple(map(lambda i: perm[i :i + D], r)), images)) | |
def pattern_diff(colors, D, matrix, result, prev, prev2, x, y): | |
if x == D: | |
if y == D - 1: | |
return result.append(matrix) | |
prev = (-1,) | |
prev2 = prev + tuple(matrix[y]) + prev | |
x = 0 | |
y += 1 | |
matrix.append([]) | |
check = set(prev2[x:x + 3] + prev) | |
for color in colors - check: | |
matrix_copy = list(map(list, matrix)) | |
matrix_copy[-1].append(color) | |
pattern_diff(colors, D, matrix_copy, result, (color,), prev2, x + 1, y) | |
def pattern_cross(colors, pairs, pair, n): | |
if n == 2: | |
return pairs.add(pair) | |
for color in colors.copy(): | |
if not n: | |
colors = colors[1:] | |
pattern_cross(colors, pairs, pair + (color,), 1) | |
else: | |
pattern_cross(colors[1:], pairs, pair + (color,), n + 1) | |
# pattern_cross(colors[1:] if n else (colors := colors[1:]), pairs, pair + (color,), n + 1) | |
def pattern_cross_images(color_pairs, D): | |
images = [] | |
one = 1 | |
if D & one: | |
half_D1 = (D + one) >> one | |
for pair in color_pairs: | |
stripe = pair * half_D1 | |
image = (stripe[:D], stripe[one:]) * half_D1 | |
images.extend((image[:D], image[one:])) | |
else: | |
half_D = D >> one | |
for pair in color_pairs: | |
stripe = pair * half_D + pair[:one] | |
first_stripe = stripe[:D] | |
image = (first_stripe, stripe[one:]) * half_D + (first_stripe,) | |
images.extend((image[:D], image[one:])) | |
return images | |
def pattern_rect(colors, D, results, curr_list, prev,n): | |
if n == D: | |
return results.add(curr_list) | |
for color in colors: | |
if color != prev: | |
pattern_rect(colors, D, results, curr_list + color, color, n + 1) | |
def permutations(colors, length, result, curr_list, n): | |
if n == length: | |
return result.add(curr_list) | |
for color in colors: | |
permutations(colors, length, result, curr_list + color, n + 1) | |
# # colors = [0, 1, 2, 3, 4, 5] | |
# colors = [0, 1, 2, 3] | |
# # colors = [0, 1, 2, 3, 4, 5, 6, 7] | |
# D = 3 | |
# pattern = "pattern_cross_" | |
# result = ex(colors, D, pattern) | |
# # print(result) | |
# print(len(result) == len(set(result))) | |
# print(len(result), len(set(result))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment