Reorganise gists folder

This commit is contained in:
TKE
2022-05-13 12:49:21 +02:00
parent ecd3c7fe2f
commit acd8d616dc
98 changed files with 63 additions and 20 deletions

View File

@@ -0,0 +1,25 @@
[[[1, 1, 1, 1, 22],
[6, 6, 6, 6, 22],
[2, 6, 9, 22, 22],
[9, 9, 9, 9, 22],
[10, 15, 15, 15, 15]],
[[2, 11, 1, 19, 20],
[2, 11, 13, 19, 21],
[2, 11, 11, 19, 23],
[2, 11, 17, 19, 24],
[10, 14, 18, 15, 25]],
[[3, 3, 3, 3, 20],
[4, 13, 13, 19, 21],
[8, 8, 8, 8, 23],
[10, 14, 17, 17, 24],
[10, 14, 18, 18, 25]],
[[4, 12, 3, 20, 20],
[4, 12, 13, 21, 21],
[4, 12, 8, 23, 23],
[4, 12, 17, 24, 24],
[10, 14, 18, 25, 25]],
[[5, 5, 5, 5, 20],
[7, 5, 13, 16, 21],
[7, 12, 16, 16, 23],
[7, 7, 17, 16, 24],
[7, 14, 18, 16, 25]]]

129
codegrab/puzzlebox/solve.py Normal file
View File

@@ -0,0 +1,129 @@
import pprint
import operator
import numpy as np
import math
from copy import copy, deepcopy
piece=[[0,0,0],[0,1,0],[0,2,0],[0,3,0],[1,2,0]]
sizeofcube=5
def init_cube(size=sizeofcube):
return [[[0 for x in range(0,size)] for y in range(0,size)] for z in range(0,size)]
def move_start_position(piece,index):
return [np.subtract(x, piece[index]) for x in piece]
def draw_cube(cube):
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect('equal')
ax.set_xlabel('x', fontsize=10)
ax.set_ylabel('y', fontsize=10)
ax.set_zlabel('z', fontsize=10)
ma=np.array(cube)
ax.voxels(ma, edgecolor="k")
plt.show()
def put_piece_in_cube(piece,cube,position,index):
cursors = [np.add(position,p) for p in piece]
in_cube = [ max(c) < len(cube) and min(c) >= 0 for c in cursors]
if all(in_cube):
for cursor in cursors:
try:
if cube[cursor[0]][cursor[1]][cursor[2]]!=0:
return False
except:
return False
for cursor in cursors:
cube[cursor[0]][cursor[1]][cursor[2]]=index
return True
else:
return False
def rotate_vector(vector,axis,angle):
x,y,z=vector
angle=math.radians(angle)
if axis == "z":
return (int(round((x*math.cos(angle)) - (y*math.sin(angle)))),int(round((x*math.sin(angle)) + (y*math.cos(angle)))),z)
if axis == "y":
return (int(round(x*math.cos(angle) + z*math.sin(angle))),y,int(round(-x*math.sin(angle) + z*math.cos(angle))))
if axis == "x":
return (x,int(round(y*math.cos(angle) - z*math.sin(angle))),int(round(y*math.sin(angle) + z*math.cos(angle))))
def rotate_piece(piece,axis,angle):
return [rotate_vector(x, axis, angle) for x in piece]
def shift_piece(piece,anchor_index):
anchor=piece[anchor_index]
return [np.subtract(p,anchor) for p in piece]
def generate_rotations(piece):
all_rotations=set()
for i in range(0,4):
for j in range(0,4):
for k in range(0,4):
for p in range(0,5):
rotated_piece=rotate_piece(rotate_piece(rotate_piece(shift_piece(piece,p),"x",k*90),"y",j*90),"z",i*90)
all_rotations.add(tuple(rotated_piece))
return frozenset(all_rotations)
def find_empty_spot(cube):
for z in range(0,sizeofcube):
for y in range(0,sizeofcube):
for x in range(0,sizeofcube):
if cube[x][y][z]==0:
return (x,y,z)
return None
def solve(cube,index):
#make copy of cube
global maxindex
if index > maxindex:
print(index)
maxindex=index
backup=deepcopy(cube)
# draw_cube(backup)
#make copy of available pieces
global all_rotations
pieces=set(all_rotations.copy())
# print("{}:find empty spot#########################".format(index))
empty_pos=find_empty_spot(backup)
if empty_pos==None:
pprint.pprint(cube)
draw_cube(cube)
return True
else:
(x,y,z)=empty_pos
# print("{}:empty_spot at ({},{},{})".format(index,x,y,z))
#found empty space > trying to fill it
while len(pieces)>0:
#use copy of cube without my parts
local_cube=deepcopy(backup)
piece=pieces.pop()
if put_piece_in_cube(piece, local_cube, (x,y,z), index):
# print("{}:found fitting piece {} ({} left)".format(index,piece,len(pieces)))
if solve(local_cube, index+1):
return True
else:
# print("{}:removing ({},{},{}):{}".format(index,x,y,z,len(pieces)))
pass
#nothing fits return fail
return False
maxindex=0
def main():
global all_rotations
all_rotations=generate_rotations(piece)
solve(init_cube(),1)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,85 @@
import numpy as np
import math
from operator import add
import matplotlib.pyplot as plt
import pprint
from mpl_toolkits.mplot3d import Axes3D
# g_cube=np.zeros((10,10,10))
n=6
g_cube=[[[0 for k in range(0,n)] for j in range(0,n)] for i in range(0,n)]
form=[[0,0,0],[1,0,0],[2,0,0],[3,0,0],[2,1,0]]
def set_origin(form,index):
newform=list()
for x in form:
newform.append(np.subtract(x,form[index]))
return newform
def vector_rotate(vector,angle,axis):
if axis=='x':
result=[vector[0],( ( vector[1]*math.cos(angle) ) - ( vector[2]*math.sin(angle) ) ),( ( vector[1]*math.sin(angle) ) + ( vector[2]*math.cos(angle) ) )]
if axis=='y':
result=[( ( vector[0]*math.cos(angle) ) + ( vector[2]*math.sin(angle) ) ),vector[1],( ( -vector[0]*math.sin(angle) ) + ( vector[2]*math.cos(angle) ) )]
if axis=='z':
result=[( ( vector[0]*math.cos(angle) ) - ( vector[1]*math.sin(angle) ) ),( ( vector[0]*math.sin(angle) ) + ( vector[1]*math.cos(angle) ) )]
def form_in_cube(form):
for cursor in form:
for element in cursor:
if element<=0 or element>=n:
return False
return True
def put_in(form,cube,offset,piece=1):
form_positions=[(x+offset[0],y+offset[1],z+offset[2]) for (x,y,z) in form]
# form_positions=list([map(add,p,offset) for p in form])
if form_in_cube(form_positions):
for cursor in form_positions:
cube[cursor[0]][cursor[1]][cursor[2]]=piece
print("set ({},{},{}) to {}".format(cursor[0],cursor[1],cursor[2],piece))
else:
print("out")
def draw_field(g_cube):
# g_cube=np.zeros((6,6,6))
# g_cube=cube
# prepare some coordinates
# x, y, z = np.indices((6, 6, 6))
x, y, z = np.indices((len(g_cube),len(g_cube[0]), len(g_cube[0][0])))
farben=["red","blue","green","cyan","magenta","yellow"]
list_of_cubes =list()
for x_pos in range(0,len(g_cube)):
for y_pos in range(0,len(g_cube[x_pos])):
for z_pos in range(0,len(g_cube[x_pos][y_pos])):
color=(g_cube[x_pos][y_pos][z_pos])
if color>0:
print("Voxel by ({},{},{}) : {}".format(x_pos,y_pos,z_pos,type(g_cube[x_pos][y_pos][z_pos])))
farbe=farben[int((color+1)%len(farben))]
list_of_cubes.append({"cube":(x < x_pos) & (x >= (x_pos-1) ) & (y < y_pos) & (y >= (y_pos-1) ) & (z < z_pos) & (z >= (z_pos-1) ),"farbe":farbe})
voxels=list_of_cubes[0]["cube"]
colors = np.empty(voxels.shape, dtype=object)
for x in list_of_cubes:
voxels=voxels | x["cube"]
colors[x["cube"]]=x["farbe"]
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.voxels(voxels, facecolors=colors, edgecolor='k')
plt.show()
put_in(set_origin(form,3),g_cube,(1,2,1),1)
put_in(set_origin(form,4),g_cube,(2,2,2),2)
put_in(set_origin(form,3),g_cube,(3,2,3),1)
put_in(set_origin(form,4),g_cube,(4,2,4),2)
draw_field(g_cube)

View File

@@ -0,0 +1,175 @@
import pprint
import operator
import numpy as np
import math
from copy import copy, deepcopy
import profile
piece=[[0,0,0],[0,1,0],[0,2,0],[0,3,0],[1,2,0]]
sizeofcube=5
def init_cube(size=sizeofcube):
return [[[0 for x in range(0,size)] for y in range(0,size)] for z in range(0,size)]
def move_start_position(piece,index):
return [np.subtract(x, piece[index]) for x in piece]
def draw_cube(cube):
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect('equal')
ax.set_xlabel('x', fontsize=10)
ax.set_ylabel('y', fontsize=10)
ax.set_zlabel('z', fontsize=10)
ma=np.array(cube)
ax.voxels(ma, edgecolor="k")
plt.show()
def set_cube_vals(cursors,cube,value):
for cursor in cursors:
cube[cursor[0]][cursor[1]][cursor[2]]=value
def is_valid(piece,position):
global sizeofcube
upper_x=sizeofcube-position[0]
upper_y=sizeofcube-position[1]
upper_z=sizeofcube-position[2]
for (x,y,z) in piece:
if x<-position[0] or x>upper_x:
return False
if y<-position[1] or y>upper_y:
return False
if z<-position[2] or z>upper_z:
return False
return True
def put_piece_in_cube(piece,cube,position,index):
if is_valid(piece,position):
# cursors = [np.add(position,p) for p in piece]
# for cursor in cursors:
cursors=[]
for (x,y,z) in piece:
cursor=[(x+position[0]),(y+position[1]),(z+position[2])]
cursors.append(cursor)
try:
if cube[cursor[0]][cursor[1]][cursor[2]]!=0:
return False
except:
return False
set_cube_vals(cursors, cube, index)
return True
else:
return False
def remove_piece_in_cube(piece,cube,position):
cursors = [np.add(position,p) for p in piece]
set_cube_vals(cursors, cube, 0)
def rotate_vector(vector,axis,angle):
x,y,z=vector
angle=math.radians(angle)
if axis == "z":
return (int(round((x*math.cos(angle)) - (y*math.sin(angle)))),int(round((x*math.sin(angle)) + (y*math.cos(angle)))),z)
if axis == "y":
return (int(round(x*math.cos(angle) + z*math.sin(angle))),y,int(round(-x*math.sin(angle) + z*math.cos(angle))))
if axis == "x":
return (x,int(round(y*math.cos(angle) - z*math.sin(angle))),int(round(y*math.sin(angle) + z*math.cos(angle))))
def rotate_piece(piece,axis,angle):
return [rotate_vector(x, axis, angle) for x in piece]
def shift_piece(piece,anchor_index):
anchor=piece[anchor_index]
return [np.subtract(p,anchor) for p in piece]
def generate_rotations(piece):
all_rotations=set()
for i in range(0,4):
for j in range(0,4):
for k in range(0,4):
for p in range(0,len(piece)):
rotated_piece=rotate_piece(rotate_piece(rotate_piece(shift_piece(piece,p),"x",k*90),"y",j*90),"z",i*90)
all_rotations.add(tuple(rotated_piece))
return frozenset(all_rotations)
def find_empty_spot(cube):
for z in range(0,sizeofcube):
for y in range(0,sizeofcube):
for x in range(0,sizeofcube):
if cube[x][y][z]==0:
return (x,y,z)
return None
def printstats():
global stat_counter
global stats
stat_counter=stat_counter+1
if stat_counter%10000==0:
print(stat_counter)
for x in stats:
print("{}:{}".format(x,stats[x]))
if x>5:
break
def parallel_pool_init():
global stats
global solutions
stats=dict()
solutions=list()
def parallel_solve(cube):
global all_rotations
all_rotations=generate_rotations(piece)
pieces=set(all_rotations.copy())
first_position=(0,0,0)
while len(pieces)>0:
piece=pieces.pop()
if put_piece_in_cube(piece, cube, first_position, index):
solve(cube, 2,):
def solve(cube,index):
global stats
global solutions
global all_rotations
pieces=set(all_rotations.copy())
# print("{}:find empty spot#########################".format(index))
empty_pos=find_empty_spot(cube)
if empty_pos==None:
pprint.pprint(cube)
draw_cube(cube)
solutions.append(cube)
return False
else:
(x,y,z)=empty_pos
while len(pieces)>0:
#use copy of cube without my parts
piece=pieces.pop()
if put_piece_in_cube(piece, cube, (x,y,z), index):
# print("{}:found fitting piece {} ({} left)".format(index,piece,len(pieces)))
stats[index]=len(pieces)
if solve(cube, index+1):
return True
else:
remove_piece_in_cube(piece, cube, (x,y,z))
#nothing fits return fail
return False
# maxindex=0
# stat_counter=0
# stats=dict()
# last_stats=dict()
def main():
parallel_solve(init_cube())
if __name__ == '__main__':
# profile.run('main()')
main()

View File

@@ -0,0 +1,177 @@
import pprint
import operator
import numpy as np
import math
from copy import copy, deepcopy
import profile
piece=[[0,0,0],[0,1,0],[0,2,0],[0,3,0],[1,2,0]]
sizeofcube=5
def init_cube(size=sizeofcube):
return [[[0 for x in range(0,size)] for y in range(0,size)] for z in range(0,size)]
def move_start_position(piece,index):
return [np.subtract(x, piece[index]) for x in piece]
def draw_cube(cube):
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect('equal')
ax.set_xlabel('x', fontsize=10)
ax.set_ylabel('y', fontsize=10)
ax.set_zlabel('z', fontsize=10)
ma=np.array(cube)
ax.voxels(ma, edgecolor="k")
plt.show()
def set_cube_vals(cursors,cube,value):
for cursor in cursors:
cube[cursor[0]][cursor[1]][cursor[2]]=value
def is_valid(piece,position):
global sizeofcube
upper_x=sizeofcube-position[0]
upper_y=sizeofcube-position[1]
upper_z=sizeofcube-position[2]
for (x,y,z) in piece:
if x<-position[0] or x>upper_x:
return False
if y<-position[1] or y>upper_y:
return False
if z<-position[2] or z>upper_z:
return False
return True
def put_piece_in_cube(piece,cube,position,index):
if is_valid(piece,position):
# cursors = [np.add(position,p) for p in piece]
# for cursor in cursors:
cursors=[]
for (x,y,z) in piece:
cursor=[(x+position[0]),(y+position[1]),(z+position[2])]
cursors.append(cursor)
try:
if cube[cursor[0]][cursor[1]][cursor[2]]!=0:
return False
except:
return False
set_cube_vals(cursors, cube, index)
return True
else:
return False
def remove_piece_in_cube(piece,cube,position):
cursors = [np.add(position,p) for p in piece]
set_cube_vals(cursors, cube, 0)
def rotate_vector(vector,axis,angle):
x,y,z=vector
angle=math.radians(angle)
if axis == "z":
return (int(round((x*math.cos(angle)) - (y*math.sin(angle)))),int(round((x*math.sin(angle)) + (y*math.cos(angle)))),z)
if axis == "y":
return (int(round(x*math.cos(angle) + z*math.sin(angle))),y,int(round(-x*math.sin(angle) + z*math.cos(angle))))
if axis == "x":
return (x,int(round(y*math.cos(angle) - z*math.sin(angle))),int(round(y*math.sin(angle) + z*math.cos(angle))))
def rotate_piece(piece,axis,angle):
return [rotate_vector(x, axis, angle) for x in piece]
def shift_piece(piece,anchor_index):
anchor=piece[anchor_index]
return [np.subtract(p,anchor) for p in piece]
def generate_rotations(piece):
all_rotations=set()
for i in range(0,4):
for j in range(0,4):
for k in range(0,4):
for p in range(0,len(piece)):
rotated_piece=rotate_piece(rotate_piece(rotate_piece(shift_piece(piece,p),"x",k*90),"y",j*90),"z",i*90)
all_rotations.add(tuple(rotated_piece))
return frozenset(all_rotations)
def find_empty_spot(cube):
for z in range(0,sizeofcube):
for y in range(0,sizeofcube):
for x in range(0,sizeofcube):
if cube[x][y][z]==0:
return (x,y,z)
return None
def printstats():
global stat_counter
global stats
stat_counter=stat_counter+1
if stat_counter%10000==0:
print(stat_counter)
for x in stats:
print("{}:{}".format(x,stats[x]))
if x>5:
break
def parallel_pool_init():
global stats
global solutions
stats=dict()
solutions=list()
def parallel_solve(cube):
global all_rotations
all_rotations=generate_rotations(piece)
pieces=set(all_rotations.copy())
first_position=(0,0,0)
while len(pieces)>0:
piece=pieces.pop()
if put_piece_in_cube(piece, cube, first_position, index):
stats["jobid"]={"0"=>len(pieces)}
solve(cube, 2,):
def solve(cube,index,jobid):
global stats
global solutions
global all_rotations
pieces=set(all_rotations.copy())
# print("{}:find empty spot#########################".format(index))
empty_pos=find_empty_spot(cube)
if empty_pos==None:
pprint.pprint(cube)
draw_cube(cube)
solutions.append(cube)
return False
else:
(x,y,z)=empty_pos
while len(pieces)>0:
#use copy of cube without my parts
piece=pieces.pop()
if put_piece_in_cube(piece, cube, (x,y,z), index):
# print("{}:found fitting piece {} ({} left)".format(index,piece,len(pieces)))
stats[index]=len(pieces)
if solve(cube, index+1):
return True
else:
remove_piece_in_cube(piece, cube, (x,y,z))
#nothing fits return fail
return False
# maxindex=0
# stat_counter=0
# stats=dict()
# last_stats=dict()
def main():
parallel_solve(init_cube())
if __name__ == '__main__':
# profile.run('main()')
main()

View File

@@ -0,0 +1,99 @@
'''
==========================
3D voxel / volumetric plot
==========================
Demonstrates plotting 3D volumetric objects with ``ax.voxels``
'''
import matplotlib.pyplot as plt
import numpy as np
import pprint
import random
from matplotlib import colors as mcolors
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
g_cube=np.zeros((6,6,6))
# prepare some coordinates
x, y, z = np.indices((6, 6, 6))
# farben=["red","blue","green","cyan","magenta","yellow"]
farben=[name for name in mcolors.CSS4_COLORS]
random.shuffle(farben)
g_cube=[[[1, 1, 1, 1, 22],
[6, 6, 6, 6, 22],
[2, 6, 9, 22, 22],
[9, 9, 9, 9, 22],
[10, 15, 15, 15, 15]],
[[2, 11, 1, 19, 20],
[2, 11, 13, 19, 21],
[2, 11, 11, 19, 23],
[2, 11, 17, 19, 24],
[10, 14, 18, 15, 25]],
[[3, 3, 3, 3, 20],
[4, 13, 13, 19, 21],
[8, 8, 8, 8, 23],
[10, 14, 17, 17, 24],
[10, 14, 18, 18, 25]],
[[4, 12, 3, 20, 20],
[4, 12, 13, 21, 21],
[4, 12, 8, 23, 23],
[4, 12, 17, 24, 24],
[10, 14, 18, 25, 25]],
[[5, 5, 5, 5, 20],
[7, 5, 13, 16, 21],
[7, 12, 16, 16, 23],
[7, 7, 17, 16, 24],
[7, 14, 18, 16, 25]]]
list_of_cubes =list()
color_counter=0
for x_pos in range(0,len(g_cube)):
for y_pos in range(0,len(g_cube[x_pos])):
for z_pos in range(0,len(g_cube[x_pos][y_pos])):
if g_cube[x_pos][y_pos][z_pos]!=0:
cur_farbe=g_cube[x_pos][y_pos][z_pos]%len(farben)
print("Voxel by in {} for ({}>x>={}//{}>y>={}//{}>z>={}) )".format(farben[cur_farbe],x_pos,x_pos+1,y_pos,y_pos+1,z_pos,z_pos+1))
list_of_cubes.append({"cube":(x > x_pos) & (x <= (x_pos+1) ) & (y > y_pos) & (y <= (y_pos+1) ) & (z > z_pos) & (z <= (z_pos+1) ),"farbe":farben[cur_farbe]})
color_counter=(color_counter + 1) % len (farben)
voxels=list_of_cubes[0]["cube"]
colors = np.empty(voxels.shape, dtype=object)
for x in list_of_cubes:
voxels=voxels | x["cube"]
colors[x["cube"]]=x["farbe"]
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.voxels(voxels, facecolors=colors, edgecolor='k')
plt.show()
# draw cuboids in the top left and bottom right corners, and a link between them
#
# cube1 = (x < 3) & (y < 3) & (z < 3)
# cube2 = (x >= 5) & (y >= 5) & (z >= 5)
# link = abs(x - y) + abs(y - z) + abs(z - x) <= 2
#
# # combine the objects into a single boolean array
# voxels = cube1 | cube2 | link
#
# # set the colors of each object
# colors = np.empty(voxels.shape, dtype=object)
# colors[link] = 'red'
# colors[cube1] = 'blue'
# colors[cube2] = 'green'
#
# # and plot everything
# fig = plt.figure()
# ax = fig.gca(projection='3d')
# ax.voxels(voxels, facecolors=colors, edgecolor='k')
#
# plt.show()