cool
This commit is contained in:
99
src/solver/SomaSolution.ts
Normal file
99
src/solver/SomaSolution.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import type VoxelSpace from "./VoxelSpace";
|
||||
|
||||
export default class SomaSolution {
|
||||
private solutionSpaces: VoxelSpace[];
|
||||
private dim: number;
|
||||
constructor(dim: number) {
|
||||
if (dim < 0 || dim % 1 !== 0) {
|
||||
throw new Error("Dimension must be a whole positive integer!");
|
||||
}
|
||||
this.dim = dim;
|
||||
this.solutionSpaces = [];
|
||||
}
|
||||
|
||||
static filterUnique(solutions: SomaSolution[]): SomaSolution[] {
|
||||
if (solutions.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const uniqueSolns = [solutions[0]];
|
||||
for (const solution of solutions) {
|
||||
let foundMatch = false;
|
||||
for (const rotation of solution.getUniqueRotations()) {
|
||||
let end = uniqueSolns.length;
|
||||
for (let i = 0; i < end; i++) {
|
||||
if (rotation.matches(uniqueSolns[i])) {
|
||||
foundMatch = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
uniqueSolns.push(solution);
|
||||
}
|
||||
}
|
||||
return uniqueSolns;
|
||||
}
|
||||
|
||||
getUniqueRotations(): SomaSolution[] {
|
||||
if (this.solutionSpaces.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const result: SomaSolution[] = [];
|
||||
const allRots = this.solutionSpaces.map(space => space.getAllRotations());
|
||||
for (let i = 0; i < allRots[0].length; i++) {
|
||||
const solnRot = new SomaSolution(this.dim);
|
||||
allRots.forEach(rotGroup => solnRot.addSpace(rotGroup[i]));
|
||||
result.push(solnRot);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
matches(solution: SomaSolution) {
|
||||
for (let i = 0; i < this.solutionSpaces.length; i++) {
|
||||
if (!this.solutionSpaces[i].matches(solution.solutionSpaces[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
addSpace(space: VoxelSpace) {
|
||||
this.solutionSpaces.push(space);
|
||||
}
|
||||
|
||||
print() {
|
||||
let accum = "";
|
||||
console.log("---");
|
||||
for (let x = 0; x < this.dim; x++) {
|
||||
for (let y = 0; y < this.dim; y++) {
|
||||
for (let z = 0; z < this.dim; z++) {
|
||||
for (const space of this.solutionSpaces) {
|
||||
if (space.at(x, y, z)) {
|
||||
accum += space.getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (x !== this.dim - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
}
|
||||
|
||||
at(x: number, y: number, z: number) {
|
||||
for (const space of this.solutionSpaces) {
|
||||
if (space.at(x, y, z)) {
|
||||
return space.getId();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
clone() {
|
||||
const clone = new SomaSolution(this.dim);
|
||||
clone.solutionSpaces = this.solutionSpaces.slice();
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
49
src/solver/SomaSolver.ts
Normal file
49
src/solver/SomaSolver.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import VoxelSpace from "./VoxelSpace";
|
||||
import SomaSolution from "./SomaSolution";
|
||||
|
||||
export default class SomaSolver {
|
||||
private solutionCube: VoxelSpace;
|
||||
private dim: number;
|
||||
private solutions: SomaSolution[] = [];
|
||||
private iterations: number = 0;
|
||||
constructor(dimension: number) {
|
||||
if (dimension % 1 !== 0 || dimension < 0) {
|
||||
throw new Error("The argument 'dimension' must be a positive whole number");
|
||||
}
|
||||
this.dim = dimension;
|
||||
this.solutionCube = new VoxelSpace(0, [dimension, dimension, dimension], Array(dimension**3).fill(0));
|
||||
}
|
||||
|
||||
solve(polycubes: VoxelSpace[]) {
|
||||
if (polycubes.length === 0) {
|
||||
throw new Error("You must pass at least one polycube to solve the puzzle.");
|
||||
}
|
||||
let cumulativeSize = polycubes.reduce((prev, curr) => prev + curr.size(), 0);
|
||||
if (cumulativeSize !== this.dim**3) {
|
||||
throw new Error(`The polycubes passed do not add up to exactly enough units to form a cube of dimension ${this.dim}! Got: ${cumulativeSize}, need: ${this.dim**3}`);
|
||||
}
|
||||
const combosWithRots = polycubes.slice(1).map(polycube => polycube.getUniqueRotations().map(rot => rot.getAllPositionsInCube(this.dim)).flat());
|
||||
const combos = [polycubes[0].getAllPositionsInCube(this.dim), ...combosWithRots];
|
||||
this.backtrackSolve(this.solutionCube, combos, new SomaSolution(this.dim));
|
||||
this.solutions = SomaSolution.filterUnique(this.solutions);
|
||||
this.solutions.forEach(sol => sol.print());
|
||||
}
|
||||
|
||||
private backtrackSolve(workingSolution: VoxelSpace, polycubes: VoxelSpace[][], currentSoln: SomaSolution, depth = 0) {
|
||||
const nextCubeGroup = polycubes[0];
|
||||
for (let i = 0; i < nextCubeGroup.length; i++) {
|
||||
const fusionAttempt = workingSolution.plus(nextCubeGroup[i]);
|
||||
if (fusionAttempt) {
|
||||
const nextSoln = currentSoln.clone();
|
||||
nextSoln.addSpace(nextCubeGroup[i]);
|
||||
if (polycubes.length === 1) {
|
||||
this.solutions.push(nextSoln);
|
||||
currentSoln = new SomaSolution(this.dim);
|
||||
return;
|
||||
} else {
|
||||
this.backtrackSolve(fusionAttempt, polycubes.slice(1), nextSoln, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
297
src/solver/VoxelSpace.ts
Normal file
297
src/solver/VoxelSpace.ts
Normal file
@@ -0,0 +1,297 @@
|
||||
export type DimensionDef = [number, number, number];
|
||||
|
||||
export default class VoxelSpace {
|
||||
private dims: DimensionDef;
|
||||
private length: number;
|
||||
private space: bigint;
|
||||
private id: number;
|
||||
constructor(id: number, dims: DimensionDef, space?: boolean[] | bigint, cullEmpty?: boolean) {
|
||||
if (!space) {
|
||||
space = 0n;
|
||||
} else if (Array.isArray(space)) {
|
||||
if (space.length !== dims[0] * dims[1] * dims[2]) {
|
||||
throw new Error("Vals don't fit in given dimensions.");
|
||||
}
|
||||
space = VoxelSpace.boolArrayToBigInt(space)
|
||||
}
|
||||
this.id = id;
|
||||
this.length = dims[0] * dims[1] * dims[2];
|
||||
this.dims = dims;
|
||||
this.space = space;
|
||||
if (cullEmpty) {
|
||||
this.cullEmptySpace();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolArrayToBigInt(boolArray: boolean[]): bigint {
|
||||
let result = 0n;
|
||||
for (let i = 0; i < boolArray.length; i++) {
|
||||
if (boolArray[i]) {
|
||||
result |= BigInt(1 << i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
binaryRep() {
|
||||
return this.space.toString(2);
|
||||
}
|
||||
|
||||
private cullEmptySpace() {
|
||||
const extrema = {
|
||||
xMax: -Infinity,
|
||||
xMin: Infinity,
|
||||
yMax: -Infinity,
|
||||
yMin: Infinity,
|
||||
zMax: -Infinity,
|
||||
zMin: Infinity,
|
||||
};
|
||||
let newSpace = 0n;
|
||||
this.forEachCell((val, x, y, z) => {
|
||||
if (val) {
|
||||
extrema.xMax = Math.max(extrema.xMax, x);
|
||||
extrema.xMin = Math.min(extrema.xMin, x);
|
||||
extrema.yMax = Math.max(extrema.yMax, y);
|
||||
extrema.yMin = Math.min(extrema.yMin, y);
|
||||
extrema.zMax = Math.max(extrema.zMax, z);
|
||||
extrema.zMin = Math.min(extrema.zMin, z);
|
||||
}
|
||||
});
|
||||
let index = 0n;
|
||||
for (let x = extrema.xMin; x <= extrema.xMax; x++) {
|
||||
for (let y = extrema.yMin; y <= extrema.yMax; y++) {
|
||||
for (let z = extrema.zMin; z <= extrema.zMax; z++) {
|
||||
if (this.at(x, y, z)) {
|
||||
newSpace |= 1n << index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.dims[0] = extrema.xMax - extrema.xMin + 1;
|
||||
this.dims[1] = extrema.yMax - extrema.yMin + 1;
|
||||
this.dims[2] = extrema.zMax - extrema.zMin + 1;
|
||||
this.space = newSpace;
|
||||
}
|
||||
|
||||
forEachCell(cb: (val: boolean, x: number, y: number, z: number) => any) {
|
||||
loopStart: for (let x = 0; x < this.dims[0]; x++) {
|
||||
for (let y = 0; y < this.dims[1]; y++) {
|
||||
for (let z = 0; z < this.dims[2]; z++) {
|
||||
if (cb(this.at(x, y, z), x, y, z) === 0) {
|
||||
break loopStart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
print() {
|
||||
let accum = "";
|
||||
console.log("---");
|
||||
for (let i = 0; i < this.dims[0]; i++) {
|
||||
for (let j = 0; j < this.dims[1]; j++) {
|
||||
for (let k = 0; k < this.dims[2]; k++) {
|
||||
accum += this.at(i, j, k) ? '#' : 'O';
|
||||
}
|
||||
console.log(accum);
|
||||
accum = "";
|
||||
}
|
||||
if (i !== this.dims[0] - 1) {
|
||||
console.log("-");
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
}
|
||||
|
||||
getUniqueRotations() {
|
||||
const rotations: VoxelSpace[] = [];
|
||||
const refSpace = this.clone();
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
refSpace.rot90('z');
|
||||
VoxelSpace.pushNewUniqueSpaces(rotations, refSpace.getAxisSpins('x'));
|
||||
return rotations;
|
||||
}
|
||||
|
||||
getAllRotations() {
|
||||
const rotations: VoxelSpace[] = [];
|
||||
const refSpace = this.clone();
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('y');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
refSpace.rot90('z');
|
||||
refSpace.rot90('z');
|
||||
rotations.push(...refSpace.getAxisSpins('x'));
|
||||
return rotations;
|
||||
}
|
||||
|
||||
protected static pushNewUniqueSpaces(existingSpaces: VoxelSpace[], newSpaces: VoxelSpace[]) {
|
||||
for (const newSpace of newSpaces) {
|
||||
let matchFound = false;
|
||||
for (const existingSpace of existingSpaces) {
|
||||
if (newSpace.matches(existingSpace)) {
|
||||
matchFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matchFound) {
|
||||
existingSpaces.push(newSpace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getAllPositionsInCube(cubeDim: number): VoxelSpace[] {
|
||||
if ((cubeDim > 0) && (cubeDim % 1 === 0)) {
|
||||
const cubePositions: VoxelSpace[] = [];
|
||||
for (let x = 0; x < cubeDim - this.dims[0] + 1; x++) {
|
||||
for (let y = 0; y < cubeDim - this.dims[1] + 1; y++) {
|
||||
for (let z = 0; z < cubeDim - this.dims[2] + 1; z++) {
|
||||
const cubePos = new VoxelSpace(this.id, [cubeDim, cubeDim, cubeDim]);
|
||||
this.forEachCell((val, rotX, rotY, rotZ) => {
|
||||
cubePos.set(x + rotX, y + rotY, z + rotZ, val);
|
||||
});
|
||||
cubePositions.push(cubePos);
|
||||
}
|
||||
}
|
||||
}
|
||||
return cubePositions;
|
||||
} else {
|
||||
throw new Error("cubeDim must be a positive integer.");
|
||||
}
|
||||
}
|
||||
|
||||
matches(space: VoxelSpace) {
|
||||
const otherDims = space.getDims();
|
||||
for (let i = 0; i < this.dims.length; i++) {
|
||||
if (otherDims[i] !== this.dims[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return this.space === space.getRaw();
|
||||
}
|
||||
|
||||
clone() {
|
||||
return new VoxelSpace(this.id, this.getDims(), this.getRaw());
|
||||
}
|
||||
|
||||
private getAxisSpins(axis: 'x' | 'y' | 'z'): VoxelSpace[] {
|
||||
const rotations = [this.clone()];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
rotations.push(rotations[i].rotated90(axis));
|
||||
}
|
||||
return rotations;
|
||||
}
|
||||
|
||||
getDims(): DimensionDef {
|
||||
return this.dims.slice() as DimensionDef;
|
||||
}
|
||||
|
||||
getRaw() {
|
||||
return this.space;
|
||||
}
|
||||
|
||||
// [1, 0, 0] [x] [ x]
|
||||
// [0, 0, -1] * [y] = [-z]
|
||||
// [0, 1, 0] [z] [ y]
|
||||
private newIndexRotX(x: number, y: number, z: number) {
|
||||
return this.dims[2] * this.dims[1] * x + this.dims[1] * (this.dims[2] - 1 - z) + y;
|
||||
}
|
||||
|
||||
// [ 0, 0, 1] [x] [ z]
|
||||
// [ 0, 1, 0] * [y] = [ y]
|
||||
// [-1, 0, 0] [z] [-x]
|
||||
private newIndexRotY(x: number, y: number, z: number) {
|
||||
return this.dims[1] * this.dims[0] * z + this.dims[0] * y + (this.dims[0] - 1 - x);
|
||||
}
|
||||
|
||||
// [0, -1, 0] [x] [-y]
|
||||
// [1, 0, 0] * [y] = [ x]
|
||||
// [0, 0, 1] [z] [ z]
|
||||
private newIndexRotZ(x: number, y: number, z: number) {
|
||||
return this.dims[0] * this.dims[2] * (this.dims[1] - 1 - y) + this.dims[2] * x + z;
|
||||
}
|
||||
|
||||
at(x: number, y: number, z: number) {
|
||||
const mask = 1n << BigInt(this.dims[1] * this.dims[2] * x + this.dims[2] * y + z);
|
||||
return (this.space & mask) !== 0n;
|
||||
}
|
||||
|
||||
toggle(x: number, y: number, z: number) {
|
||||
const mask = BigInt(1 << this.dims[1] * this.dims[2] * x + this.dims[2] * y + z);
|
||||
this.space ^= mask;
|
||||
}
|
||||
|
||||
set(x: number, y: number, z: number, val: boolean) {
|
||||
const mask = BigInt(1 << this.dims[1] * this.dims[2] * x + this.dims[2] * y + z);
|
||||
if (val) {
|
||||
this.space |= mask;
|
||||
} else {
|
||||
this.space &= ~mask;
|
||||
}
|
||||
}
|
||||
|
||||
rotated90(dim: 'x' | 'y' | 'z') {
|
||||
let newSpace = 0n;
|
||||
let newDims: DimensionDef;
|
||||
let rotIndex: (i: number, j: number, k: number) => number;
|
||||
if (dim === 'x') {
|
||||
newDims = [this.dims[0], this.dims[2], this.dims[1]];
|
||||
rotIndex = this.newIndexRotX.bind(this);
|
||||
} else if (dim === 'y') {
|
||||
newDims = [this.dims[2], this.dims[1], this.dims[0]];
|
||||
rotIndex = this.newIndexRotY.bind(this);
|
||||
} else {
|
||||
newDims = [this.dims[1], this.dims[0], this.dims[2]];
|
||||
rotIndex = this.newIndexRotZ.bind(this);
|
||||
}
|
||||
this.forEachCell((val, i, j, k) => {
|
||||
if (val) {
|
||||
newSpace |= BigInt(1 << rotIndex(i, j, k));
|
||||
}
|
||||
})
|
||||
return new VoxelSpace(this.id, newDims, newSpace);
|
||||
}
|
||||
|
||||
rot90(dim: 'x' | 'y' | 'z') {
|
||||
const rot = this.rotated90(dim);
|
||||
this.space = rot.getRaw();
|
||||
this.dims = rot.getDims();
|
||||
}
|
||||
|
||||
plus(space: VoxelSpace): VoxelSpace | null {
|
||||
const otherSpace = space.getRaw();
|
||||
if ((this.space | otherSpace) === (this.space ^ otherSpace)) {
|
||||
return new VoxelSpace(this.id, this.dims, otherSpace | this.space);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
size() {
|
||||
let size = 0;
|
||||
this.forEachCell((val) => {
|
||||
if (val) {
|
||||
size++;
|
||||
}
|
||||
});
|
||||
return size;
|
||||
}
|
||||
}
|
||||
10
src/solver/main.js
Normal file
10
src/solver/main.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import App from './App.svelte';
|
||||
|
||||
const app = new App({
|
||||
target: document.body,
|
||||
props: {
|
||||
name: 'world'
|
||||
}
|
||||
});
|
||||
|
||||
export default app;
|
||||
109
src/solver/main.ts
Normal file
109
src/solver/main.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import SomaSolver from "./SomaSolver";
|
||||
import VoxelSpace from "./VoxelSpace";
|
||||
|
||||
const tetromino1 = new VoxelSpace(1, [3, 3, 3], [
|
||||
true, true, true,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino2 = new VoxelSpace(2, [3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino3 = new VoxelSpace(3, [3, 3, 3], [
|
||||
true, false, false,
|
||||
true, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino4 = new VoxelSpace(4, [3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
true, false, false,
|
||||
true, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino5 = new VoxelSpace(5, [3, 3, 3], [
|
||||
true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
false, false, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const tetromino6 = new VoxelSpace(6, [3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, true,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
const triomino1 = new VoxelSpace(7, [3, 3, 3], [
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, true, false,
|
||||
false, true, false,
|
||||
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
], true);
|
||||
|
||||
|
||||
|
||||
// const cube = new VoxelSpace([3, 3, 3], Array(3**3).fill(0));
|
||||
// cube.plus(triomino1)?.plus(tetromino2, {x: 1, y: 0, z: 1})?.print();
|
||||
|
||||
const solver = new SomaSolver(3);
|
||||
console.log("solving");
|
||||
solver.solve([triomino1, tetromino2, tetromino3, tetromino1, tetromino4, tetromino5, tetromino6]);
|
||||
Reference in New Issue
Block a user