From 4895ffd4bb5d8ec2cd29541706c94cf8400e8c52 Mon Sep 17 00:00:00 2001 From: Michael Qian Date: Fri, 6 Mar 2020 17:05:53 -0800 Subject: [PATCH 1/3] Basic cube representation types --- src/lib/core/cube.tsx | 9 +++++++++ src/lib/core/cubeindex.tsx | 20 ++++++++++++++++++++ src/lib/core/cubeop.tsx | 6 ++++++ 3 files changed, 35 insertions(+) create mode 100644 src/lib/core/cube.tsx create mode 100644 src/lib/core/cubeindex.tsx create mode 100644 src/lib/core/cubeop.tsx diff --git a/src/lib/core/cube.tsx b/src/lib/core/cube.tsx new file mode 100644 index 0000000..fd3836c --- /dev/null +++ b/src/lib/core/cube.tsx @@ -0,0 +1,9 @@ +import { CubeIndex } from "./cubeindex" +import { CubeOp } from "./cubeop" + +export interface Cube { + readonly dimension: number; + readonly positionOf: (facelet: number) => CubeIndex; + readonly occupantOf: (position: CubeIndex) => number; + readonly apply: (op: CubeOp) => Cube; +} diff --git a/src/lib/core/cubeindex.tsx b/src/lib/core/cubeindex.tsx new file mode 100644 index 0000000..1291fa7 --- /dev/null +++ b/src/lib/core/cubeindex.tsx @@ -0,0 +1,20 @@ +export interface CubeIndex { + readonly face: number; + readonly row: number; + readonly column: number; +} + +export namespace CubeIndex { + export function fromSlotNumber(slotNumber: number, dimension: number): CubeIndex { + const dimSquared = dimension ** 2; + return { + face: Math.trunc(slotNumber / dimSquared), + row: Math.trunc((slotNumber % dimSquared) / dimension), + column: slotNumber % dimension + } + } + + export function toSlotNumber(position: CubeIndex, dimension: number): number { + return ((dimension ** 2) * position.face) + (dimension * position.row) + position.column; + } +} diff --git a/src/lib/core/cubeop.tsx b/src/lib/core/cubeop.tsx new file mode 100644 index 0000000..93ed356 --- /dev/null +++ b/src/lib/core/cubeop.tsx @@ -0,0 +1,6 @@ +import { CubeIndex } from "./cubeindex" + +export interface CubeOp { + readonly imageOf: (cubeIndex: CubeIndex, dimension: number) => CubeIndex; + readonly inverse: () => CubeOp; +} From 65b5b95397ec7810227252a1bfa19467d8bf580e Mon Sep 17 00:00:00 2001 From: Michael Qian Date: Thu, 12 Mar 2020 21:52:09 -0700 Subject: [PATCH 2/3] Renaming non-react files to .ts from .tsx and upgrades static factory methods for index conversion to a factory object. --- src/lib/core/{cube.tsx => cube.ts} | 4 +-- src/lib/core/cubeindex.ts | 34 ++++++++++++++++++++++++++ src/lib/core/cubeindex.tsx | 20 --------------- src/lib/core/{cubeop.tsx => cubeop.ts} | 2 +- 4 files changed, 37 insertions(+), 23 deletions(-) rename src/lib/core/{cube.tsx => cube.ts} (73%) create mode 100644 src/lib/core/cubeindex.ts delete mode 100644 src/lib/core/cubeindex.tsx rename src/lib/core/{cubeop.tsx => cubeop.ts} (77%) diff --git a/src/lib/core/cube.tsx b/src/lib/core/cube.ts similarity index 73% rename from src/lib/core/cube.tsx rename to src/lib/core/cube.ts index fd3836c..840fc7d 100644 --- a/src/lib/core/cube.tsx +++ b/src/lib/core/cube.ts @@ -1,5 +1,5 @@ -import { CubeIndex } from "./cubeindex" -import { CubeOp } from "./cubeop" +import { CubeIndex } from "./cubeindex"; +import { CubeOp } from "./cubeop"; export interface Cube { readonly dimension: number; diff --git a/src/lib/core/cubeindex.ts b/src/lib/core/cubeindex.ts new file mode 100644 index 0000000..ea4eddd --- /dev/null +++ b/src/lib/core/cubeindex.ts @@ -0,0 +1,34 @@ +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: number): 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): number { + 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/cubeindex.tsx b/src/lib/core/cubeindex.tsx deleted file mode 100644 index 1291fa7..0000000 --- a/src/lib/core/cubeindex.tsx +++ /dev/null @@ -1,20 +0,0 @@ -export interface CubeIndex { - readonly face: number; - readonly row: number; - readonly column: number; -} - -export namespace CubeIndex { - export function fromSlotNumber(slotNumber: number, dimension: number): CubeIndex { - const dimSquared = dimension ** 2; - return { - face: Math.trunc(slotNumber / dimSquared), - row: Math.trunc((slotNumber % dimSquared) / dimension), - column: slotNumber % dimension - } - } - - export function toSlotNumber(position: CubeIndex, dimension: number): number { - return ((dimension ** 2) * position.face) + (dimension * position.row) + position.column; - } -} diff --git a/src/lib/core/cubeop.tsx b/src/lib/core/cubeop.ts similarity index 77% rename from src/lib/core/cubeop.tsx rename to src/lib/core/cubeop.ts index 93ed356..49a720e 100644 --- a/src/lib/core/cubeop.tsx +++ b/src/lib/core/cubeop.ts @@ -1,4 +1,4 @@ -import { CubeIndex } from "./cubeindex" +import { CubeIndex } from "./cubeindex"; export interface CubeOp { readonly imageOf: (cubeIndex: CubeIndex, dimension: number) => CubeIndex; From c686698c2337850b8b4113fe9f0eac5c13cfd13e Mon Sep 17 00:00:00 2001 From: Michael Qian Date: Thu, 12 Mar 2020 21:57:38 -0700 Subject: [PATCH 3/3] Adds SlotNumber and FaceletNumber type aliases --- src/lib/core/cube.ts | 4 +++- src/lib/core/cubeindex.ts | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib/core/cube.ts b/src/lib/core/cube.ts index 840fc7d..a116492 100644 --- a/src/lib/core/cube.ts +++ b/src/lib/core/cube.ts @@ -1,9 +1,11 @@ import { CubeIndex } from "./cubeindex"; import { CubeOp } from "./cubeop"; +export type FaceletNumber = number; + export interface Cube { readonly dimension: number; - readonly positionOf: (facelet: number) => CubeIndex; + 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 index ea4eddd..d998b4f 100644 --- a/src/lib/core/cubeindex.ts +++ b/src/lib/core/cubeindex.ts @@ -1,3 +1,5 @@ +export type SlotNumber = number; + export interface CubeIndex { readonly face: number; readonly row: number; @@ -11,7 +13,7 @@ export class IndexConverter { return new IndexConverter(dimension); } - fromSlotNumber(slotNumber: number): CubeIndex { + fromSlotNumber(slotNumber: SlotNumber): CubeIndex { const dimSquared = this.dimension ** 2; return { face: Math.trunc(slotNumber / dimSquared), @@ -20,7 +22,7 @@ export class IndexConverter { }; } - toSlotNumber(position: CubeIndex): number { + toSlotNumber(position: CubeIndex): SlotNumber { return ( this.dimension ** 2 * position.face + this.dimension * position.row +