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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
#!/usr/bin/env python
from enum import Enum
from random import randint
class Color(Enum):
NONE = 0
JAUNE = 1
ROUGE = 2
BLEU = 3
def __str__(self):
if self == Color.ROUGE:
return "🔴"
if self == Color.JAUNE:
return "🟢"
if self == Color.BLEU:
return "🔵"
return "◾️"
class PlanName(Enum):
"""z"""
SOMMET = 0
HAUT = 1
BAS = 2
BASE = 3
class Boule:
"""mini-cubes"""
def __init__(self, x, y, z, color=Color.NONE) -> None:
self.x = x
self.y = y
self.z = z
self.color = color
def __str__(self) -> str:
return f"{self.x}/{self.y}/{self.z} {self.color}"
def in_plan(self) -> PlanName:
"""retourne position du mini-cube"""
return PlanName(self.z)
def my_ligne(self, boules: list) -> list:
return [b for b in boules if b.z == self.z and b.y == self.y]
def my_colonne(self, boules: list) -> list:
return [b for b in boules if b.z == self.z and b.x == self.x]
def my_profondeur(self, boules: list) -> list:
return [b for b in boules if b.y == self.y and b.x == self.x]
class ligne:
def __init__(self, boules) -> None:
self.boules = boules
def __str__(self) -> str:
ret = ""
for b in self.boules:
ret += f"{str(b)} "
return ret
class Cube:
def __init__(self, size: int) -> None:
self.size = size
self.cubes = []
x, y, z = 0, 0, 0
for y in range(self.size):
for x in range(self.size):
for z in range(self.size):
self.cubes.append(
Boule(x, y, z, Color.NONE)
)
print("Nombre de cubes:", len(self.cubes))
def set_colors(self):
b: Boule
nb = (self.size * self.size * self.size) // int((len(Color)-1))
print("nb couleurs par color:", nb)
for c in Color:
if c == Color.NONE:
continue
while True:
reste = [b for b in self.cubes if b.color == Color.NONE]
b = reste[randint(0, len(reste)-1)]
if b.color != Color.NONE:
continue
b.color = c
if len([b for b in self.cubes if b.color == c]) >= nb:
break
for b in (b for b in self.cubes if b.color == Color.NONE):
b.color = Color.ROUGE
def permuter(self, mauvais=None) ->tuple[Boule,Boule]:
a: Boule
b: Boule
if mauvais:
a = mauvais[randint(0, len(mauvais)-1)]
else:
a = self.cubes[randint(0, len(self.cubes)-1)]
b = a
while a == b:
reste = [b for b in self.cubes if b.color != a.color]
b = reste[randint(0, len(reste)-1)]
#print(" # permuter ", a, "<->", b, end=" ==> ")
a.color, b.color = b.color, a.color
#print(a, "<->", b)
return a, b
def boules_is_valid(self, boules:tuple[Boule,Boule]) -> tuple[bool, list]:
for boule in boules:
# LIGNE test
ls = boule.my_ligne(self.cubes)
#print("test:", ligne(ls))
line = set(b.color for b in ls)
if len(line) >= len(Color)-1: # >= 3
#print(f" # ligne non valide:", end=" ")
return False, ls
else:
print(f"OK: ligne {boule.y} ")
# COLONNE test
ls = boule.my_colonne(self.cubes)
line = set(b.color for b in ls)
if len(line) >= len(Color)-1: # >= 3
#print(f" # colonne non valide:", end=" ")
return False, ls
else:
print(f"OK: colonne {boule.x} ")
# PROFONDEUR test
ls = boule.my_profondeur(self.cubes)
line = set(b.color for b in ls)
if len(line) >= len(Color)-1: # >= 3
#print(f" # z non valide:", end=" ")
return False, ls
else:
print(f"OK: z {boule.x}-{boule.y} ")
return True, []
def plan_is_valid(self, nameplan: PlanName) -> tuple[bool, list]:
for y in range(0, self.size):
# LIGNE test
ls = [
b for b in self.cubes
if b.z == nameplan.value and
b.y == y
]
#print("test:", ligne(ls))
line = set(b.color for b in ls)
if len(line) >= len(Color)-1: # >= 3
#print(f" # {nameplan}, ligne non valide:", end=" ")
return False, ls
else:
print(f"OK: {nameplan} ligne {y} ")
# COLONNE test
ls = [
b for b in self.cubes
if b.z == nameplan.value and
b.x == y
]
line = set(b.color for b in ls)
if len(line) >= len(Color)-1: # >= 3
#print(f" # {nameplan}, colonne non valide:", end=" ")
return False, ls
else:
print(f"OK: {nameplan} colonne {y} ")
return True, []
def is_valid(self, a:Boule, b:Boule) -> tuple[bool, list]:
ok, mauvais = self.boules_is_valid((a, b))
if ok:
return self.validate()
return ok, mauvais
def validate(self) -> tuple[bool, list]:
for plan in PlanName:
ok, mauvais = self.plan_is_valid(plan)
if not ok:
return False, mauvais
return True, []
def get_face(self, plan: PlanName, display=False) -> list[Boule]:
ls = [b for b in self.cubes if b.in_plan() == plan]
if display:
print(plan.name)
for i, b in enumerate(ls):
print(b, end="\n" if (i+1) % self.size == 0 else " ")
return ls
def __str__(self):
return str(self.cubes)
def display(self):
print()
self.get_face(PlanName.SOMMET, True)
self.get_face(PlanName.HAUT, True)
self.get_face(PlanName.BAS, True)
self.get_face(PlanName.BASE, True)
# MAIN
jeux = Cube(4)
jeux.set_colors()
jeux.display()
maxi = 500000
i = 1
mauvais = []
while True:
#print(f"----- permutation ({i})...")
a,b = jeux.permuter(mauvais)
ok, mauvais = jeux.is_valid(a, b) # retourne mauvaise face
if ok:
print(f"YES !, au bout de {i} fois")
jeux.display()
exit(0)
i += 1
if i >= maxi:
print(f"ABANDON au bout de {i} permutations")
jeux.display()
print(f"ABANDON au bout de {i} permutations")
print("last bad:", f"{ligne(mauvais)}")
break
exit(1) |