Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict';
const initState = {
'A': {
white: true,
graph: {
'C': ['J', 'I'],
'D': ['F', 'G'],
'I': ['J'],
'G': ['F']
}
},
'B': {
white: true,
graph: {
'E': ['J', 'F'],
'D': ['I', 'H'],
'F': ['J'],
'H': ['I']
}
},
'C': {
white: true,
graph: {
'A': ['I', 'J'],
'E': ['H', 'G'],
'G': ['H'],
'J': ['I']
}
},
'D': {
white: true,
graph: {
'A': ['F', 'G'],
'B': ['H', 'I'],
'I': ['H'],
'F': ['G']
}
},
'E': {
white: true,
graph: {
'B': ['J', 'F'],
'C': ['G', 'H'],
'J': ['F'],
'H': ['G']
}
},
'F': {
white: false,
graph: {
'B': ['J'],
'D': ['G'],
}
},
'G': {
white: false,
graph: {
'A': ['F'],
'C': ['H'],
}
},
'H': {
white: false,
graph: {
'B': ['I'],
'E': ['G'],
}
},
'I': {
white: false,
graph: {
'A': ['J'],
'D': ['H'],
}
},
'J': {
white: false,
graph: {
'C': ['I'],
'E': ['F'],
}
}
};
class Star {
constructor() {
const pointNames = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
this.points = pointNames.map(p => new Point(p, initState[p]));
this.pointsMap = {};
this.points.forEach(p => {
this.pointsMap[p.name] = p;
});
}
toStateString() {
return this.points.map(p => p.toStateString()).join('');
}
turn(name) {
const point = this.pointsMap[name];
point.white = !point.white;
for (let gn in point.graph) {
//console.log(gn);
if (this.pointsMap[gn].white !== point.white) {
continue;
}
const betweenPoints = point.graph[gn];
//console.log(betweenPoints);
betweenPoints
.map(bp => this.pointsMap[bp])
.filter(bp => bp.white !== point.white)
.forEach(bp => bp.white = !bp.white);
}
}
}
class Point {
constructor(name, state) {
this.name = name;
this.white = state.white;
this.graph = state.graph;
}
toStateString() {
return this.white ? 'W': 'R';
}
}
const assert = require('assert');
function test(input, expected) {
const star = new Star();
for (let i = 0; i < input.length; i++) {
star.turn(input[i]);
}
assert.equal(star.toStateString(), expected);
console.log(star.toStateString());
}
test("A", "RWWWWRRRRR");
test("F", "WWWWWWWRRW");
test("J", "WWWWWWRRWW");
test("AA", "WWWWWWWRWW");
test("IC", "WWRWWRRRWW");
test("FC", "WWRWWWWRRW");
test("AE", "RWWWRRRRRR");
test("GJ", "WWWWWWWWWW");
test("CCB", "WRWWWRWWWR");
test("BEF", "WRWWRWWRRR");
test("JGD", "WWWRWWWWWW");
test("IHCC", "WWWWWRWWWW");
test("AIDD", "RWWWWRRWWR");
test("IJFA", "RWWWWWWWWW");
test("ABCDE", "RRRRRRRRRR");
test("ICEBA", "RRRWRRRRRR");
test("DAHHD", "RWWWWRWWWR");
test("GJIJC", "WWRWWWWWRR");
test("FGHIJ", "WWWWWWWWRR");
test("HJICGA", "RWRWWRRRRR");
test("IBCIGC", "WRWWWWWWWW");
test("BIJJJB", "WWWWWWRWWW");
test("DCBCHGD", "WRWWWWWRRW");
test("JEABDHD", "RRWWRRRWRR");
test("JHFADHE", "RWWRRRRRWW");
test("HDGGDBIB", "WWWWWWWWWW");
test("IIDIHCCG", "WWWRWRRWWW");
test("BBFBICIE", "WRRWRRRWWW");
test("HJHCFBJGG", "WRRWWWWRRW");
test("AJJIEAAII", "RWWWRWWWWR");
test("AIDHJFGAE", "WWWRRWWWWW");
test("FGBGHCBHJJ", "WWRWWWWRRW");
test("EFIGIGGHHJ", "WWWWRRRWWR");
test("HGAFDIFFFF", "RWWRWRRRRW");
test("AABBCCDDEE", "WWWWWWWWWW");
test("ABCDEFGHIJ", "RRRRRWWWWW");
test("FGHIJABCDE", "RRRRRRRRRR");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment