Skip to content

Commit

Permalink
Fixed spelling of dominoes
Browse files Browse the repository at this point in the history
  • Loading branch information
RGBz committed Jul 31, 2021
1 parent f2983f9 commit 0a673ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
46 changes: 23 additions & 23 deletions src/Graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,54 @@ import { Graph } from './Graph';
describe('Graph', () => {
describe('when graphing 1 domino', () => {
test('should respond with a single graph containing that domino', () => {
const dominos = ['1x2'].map(Domino.from);
const graphs = Graph.generateAll(dominos);
const dominoes = ['1x2'].map(Domino.from);
const graphs = Graph.generateAll(dominoes);
expect(graphs.length).toEqual(1);
expect(graphs[0].sides).toStrictEqual(new Set([DominoSide.ONE, DominoSide.TWO]));
expect(graphs[0].dominos).toStrictEqual(dominos);
expect(graphs[0].dominoes).toStrictEqual(dominoes);
});
});

describe('when graphing 2 dominos that can be paired', () => {
test('should respond with a single graph containing both dominos', () => {
const dominos = ['1x2', '2x3'].map(Domino.from);
const graphs = Graph.generateAll(dominos);
describe('when graphing 2 dominoes that can be paired', () => {
test('should respond with a single graph containing both dominoes', () => {
const dominoes = ['1x2', '2x3'].map(Domino.from);
const graphs = Graph.generateAll(dominoes);
expect(graphs.length).toEqual(1);
expect(graphs[0].sides).toStrictEqual(new Set([DominoSide.ONE, DominoSide.TWO, DominoSide.THREE]));
expect(graphs[0].dominos).toStrictEqual(dominos);
expect(graphs[0].dominoes).toStrictEqual(dominoes);
});
});

describe('when graphing 2 dominos that cannot be paired', () => {
describe('when graphing 2 dominoes that cannot be paired', () => {
test('should respond with a two graphs containing one domino each', () => {
const dominos = ['1x2', '3x4'].map(Domino.from);
const graphs = Graph.generateAll(dominos);
const dominoes = ['1x2', '3x4'].map(Domino.from);
const graphs = Graph.generateAll(dominoes);
expect(graphs.length).toEqual(2);
expect(graphs[0].sides).toStrictEqual(new Set([DominoSide.ONE, DominoSide.TWO]));
expect(graphs[1].sides).toStrictEqual(new Set([DominoSide.THREE, DominoSide.FOUR]));
expect(graphs[0].dominos).toStrictEqual([dominos[0]]);
expect(graphs[1].dominos).toStrictEqual([dominos[1]]);
expect(graphs[0].dominoes).toStrictEqual([dominoes[0]]);
expect(graphs[1].dominoes).toStrictEqual([dominoes[1]]);
});
});

describe('when graphing several dominos', () => {
describe('when graphing several dominoes', () => {
test('should respond with the expected graphs', () => {
const dominos = ['1x2', '7x8', '3x4', '5x1', '3x1', '6x7'].map(Domino.from);
const graphs = Graph.generateAll(dominos);
const dominoes = ['1x2', '7x8', '3x4', '5x1', '3x1', '6x7'].map(Domino.from);
const graphs = Graph.generateAll(dominoes);
expect(graphs.length).toEqual(2);
expect(graphs[0].sides).toStrictEqual(
new Set([DominoSide.ONE, DominoSide.TWO, DominoSide.THREE, DominoSide.FOUR, DominoSide.FIVE]),
);
expect(graphs[1].sides).toStrictEqual(new Set([DominoSide.SIX, DominoSide.SEVEN, DominoSide.EIGHT]));
expect(new Set(graphs[0].dominos)).toStrictEqual(new Set([dominos[0], dominos[2], dominos[3], dominos[4]]));
expect(new Set(graphs[1].dominos)).toStrictEqual(new Set([dominos[1], dominos[5]]));
expect(new Set(graphs[0].dominoes)).toStrictEqual(new Set([dominoes[0], dominoes[2], dominoes[3], dominoes[4]]));
expect(new Set(graphs[1].dominoes)).toStrictEqual(new Set([dominoes[1], dominoes[5]]));
});
});

describe('findLongestPath', () => {
test('should respond with the longest path for a set of dominos', () => {
const dominos = ['1x2', '7x8', '3x4', '5x1', '3x1', '6x7'].map(Domino.from);
const path = Graph.findLongestPath(dominos);
test('should respond with the longest path for a set of dominoes', () => {
const dominoes = ['1x2', '7x8', '3x4', '5x1', '3x1', '6x7'].map(Domino.from);
const path = Graph.findLongestPath(dominoes);
expect(new Set(path)).toStrictEqual(
new Set([
new Domino([DominoSide.THREE, DominoSide.FOUR]),
Expand All @@ -62,8 +62,8 @@ describe('Graph', () => {
);
});
test('should respond with an empty list if the starting side is not found', () => {
const dominos = ['1x2', '7x8', '3x4', '5x1', '3x1', '6x7'].map(Domino.from);
const path = Graph.findLongestPath(dominos, DominoSide.TWELVE);
const dominoes = ['1x2', '7x8', '3x4', '5x1', '3x1', '6x7'].map(Domino.from);
const path = Graph.findLongestPath(dominoes, DominoSide.TWELVE);
expect(new Set(path)).toStrictEqual(new Set([]));
});
});
Expand Down
20 changes: 10 additions & 10 deletions src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { longest } from './longest';
type Cursor = LinkedListCursor<Graph>;

export class Graph {
dominos: Domino[] = [];
dominoes: Domino[] = [];
nodes: Map<DominoSide, Set<Domino>> = new Map();

static findLongestPath(dominos: Domino[], startingSide?: DominoSide): Domino[] {
return longest(Graph.generateAll(dominos).map((graph) => graph.findLongestPath(startingSide)));
static findLongestPath(dominoes: Domino[], startingSide?: DominoSide): Domino[] {
return longest(Graph.generateAll(dominoes).map((graph) => graph.findLongestPath(startingSide)));
}

static generateAll(dominos: Domino[]): Graph[] {
static generateAll(dominoes: Domino[]): Graph[] {
const root = new LinkedListNode(new Graph());
for (const domino of dominos) {
for (const domino of dominoes) {
let didAdd = false;
let cursor: Cursor = root;
let prev: LinkedListNode<Graph> = cursor;
Expand Down Expand Up @@ -50,14 +50,14 @@ export class Graph {
}

add(domino: Domino): void {
this.dominos.push(domino);
this.dominoes.push(domino);
for (const side of domino.sides) {
this.nodes.set(side, (this.nodes.get(side) || new Set()).add(domino));
}
}

merge(other: Graph): void {
for (const domino of other.dominos) {
for (const domino of other.dominoes) {
this.add(domino);
}
}
Expand All @@ -66,12 +66,12 @@ export class Graph {
if (!sideToConnectTo) {
return longest([...this.sides].map((side) => this.findLongestPath(side)));
}
const dominos = [...(this.nodes.get(sideToConnectTo) ?? [])].filter((domino) => !path.includes(domino));
if (dominos.length === 0) {
const dominoes = [...(this.nodes.get(sideToConnectTo) ?? [])].filter((domino) => !path.includes(domino));
if (dominoes.length === 0) {
return path;
}
return longest(
dominos.map((domino) => this.findLongestPath(domino.getOtherSide(sideToConnectTo), path.concat(domino))),
dominoes.map((domino) => this.findLongestPath(domino.getOtherSide(sideToConnectTo), path.concat(domino))),
);
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Domino } from './Domino';
import { Graph } from './Graph';

if (process.argv.length !== 4) {
throw new Error('Must specify a comma separated list of dominos and the side to start with. e.g.: 1x2,3x4,2x3 3');
throw new Error('Must specify a comma separated list of dominoes and the side to start with. e.g.: 1x2,3x4,2x3 3');
}

console.log(
Expand Down

0 comments on commit 0a673ba

Please sign in to comment.