diff --git a/src/lib/core/cube.ts b/src/lib/core/cube.ts new file mode 100644 index 0000000..a116492 --- /dev/null +++ b/src/lib/core/cube.ts @@ -0,0 +1,11 @@ +import { CubeIndex } from "./cubeindex"; +import { CubeOp } from "./cubeop"; + +export type FaceletNumber = number; + +export interface Cube { + readonly dimension: number; + readonly positionOf: (FaceletNumber: number) => CubeIndex; + readonly occupantOf: (position: CubeIndex) => number; + readonly apply: (op: CubeOp) => Cube; +} diff --git a/src/lib/core/cubeindex.ts b/src/lib/core/cubeindex.ts new file mode 100644 index 0000000..d998b4f --- /dev/null +++ b/src/lib/core/cubeindex.ts @@ -0,0 +1,36 @@ +export type SlotNumber = number; + +export interface CubeIndex { + readonly face: number; + readonly row: number; + readonly column: number; +} + +export class IndexConverter { + readonly dimension: number; + + static forDimension(dimension: number): IndexConverter { + return new IndexConverter(dimension); + } + + fromSlotNumber(slotNumber: SlotNumber): CubeIndex { + const dimSquared = this.dimension ** 2; + return { + face: Math.trunc(slotNumber / dimSquared), + row: Math.trunc((slotNumber % dimSquared) / this.dimension), + column: slotNumber % this.dimension, + }; + } + + toSlotNumber(position: CubeIndex): SlotNumber { + return ( + this.dimension ** 2 * position.face + + this.dimension * position.row + + position.column + ); + } + + private constructor(dimension: number) { + this.dimension = dimension; + } +} diff --git a/src/lib/core/cubeop.ts b/src/lib/core/cubeop.ts new file mode 100644 index 0000000..49a720e --- /dev/null +++ b/src/lib/core/cubeop.ts @@ -0,0 +1,6 @@ +import { CubeIndex } from "./cubeindex"; + +export interface CubeOp { + readonly imageOf: (cubeIndex: CubeIndex, dimension: number) => CubeIndex; + readonly inverse: () => CubeOp; +}