Skip to content

Instantly share code, notes, and snippets.

@zjiekai
Last active September 21, 2016 11:43
Show Gist options
  • Save zjiekai/e6f49413a04c4c3f30b689012974f4c2 to your computer and use it in GitHub Desktop.
Save zjiekai/e6f49413a04c4c3f30b689012974f4c2 to your computer and use it in GitHub Desktop.
<body><script>
var B = 'B', Y = 'Y', R = 'R';
var init_state = [[B], [R], [R], [Y], [Y], [Y], [R], [R], [B]];
function equalCol(c0, c1) {
if (c0.length === c1.length) {
for (var i = 0; i < c0.length; ++i) {
if (c0[i] !== c1[i]) return false;
}
return true;
} else {
return false;
}
}
function equalState(s0, s1) {
if (s0.length === s1.length) {
for (var i = 0; i < s0.length; ++i) {
if (!equalCol(s0[i], s1[i])) return false;
}
return true;
} else {
return false;
}
}
var AM = {
'Y2B': function(s0) {
var s1 = [];
for (var i = 0; i < s0.length; ++i) {
var col = s0[i];
var new_col = [];
for (var j = 0; j < col.length; ++j) {
new_col.push((col[j] === Y) ? B : col[j]);
}
s1.push(new_col);
}
return s1;
},
'Y2YB': function(s0) {
var s1 = [];
for (var i = 0; i < s0.length; ++i) {
var col = s0[i];
var new_col = [];
for (var j = 0; j < col.length; ++j) {
if (col[j] === Y) {
new_col.push(Y);
new_col.push(B);
} else {
new_col.push(col[j]);
}
}
s1.push(new_col);
}
return s1;
},
'B2RR': function(s0) {
var s1 = [];
for (var i = 0; i < s0.length; ++i) {
var col = s0[i];
var new_col = [];
for (var j = 0; j < col.length; ++j) {
if (col[j] === B) {
new_col.push(R);
new_col.push(R);
} else {
new_col.push(col[j]);
}
}
s1.push(new_col);
}
return s1;
},
'rejectR': function(s0) {
var s1 = [];
for (var i = 0; i < s0.length; ++i) {
var col = s0[i];
var new_col = [];
for (var j = 0; j < col.length; ++j) {
if (col[j] !== R) {
new_col.push(col[j]);
}
}
if (new_col.length > 0) s1.push(new_col);
}
return s1;
},
'stackY': function(s0) {
var s1 = [];
for (var i = 0; i < s0.length; ++i) {
var col = s0[i];
var new_col = [Y];
for (var j = 0; j < col.length; ++j) {
new_col.push(col[j]);
}
s1.push(new_col);
}
return s1;
},
'stackEqual': function(s0) {
var s1 = [];
var cur = null;
var new_col = [];
for (var i = 0; i < s0.length; ++i) {
if (cur === null) {
cur = s0[i];
new_col = [].concat(cur);
} else {
if (equalCol(cur, s0[i])) {
new_col = new_col.concat(cur);
} else {
cur = s0[i];
s1.push(new_col);
new_col = [].concat(cur);
}
}
}
if (new_col.length > 0) {
s1.push(new_col);
}
return s1;
}
};
var actions = [
AM['Y2B'], AM['Y2YB'], AM['B2RR'],
AM['rejectR'], AM['stackY'], AM['stackEqual']
];
function show(s) {
for (var i = 0; i < s.length; ++i) {
console.log(i, s[i].join(''));
}
}
console.log(equalState(actions[0](init_state), [[B],[R],[R],[B],[B],[B],[R],[R],[B]]));
console.log(equalState(actions[1](init_state), [[B],[R],[R],[Y,B],[Y,B],[Y,B],[R],[R],[B]]));
console.log(equalState(actions[2](init_state), [[R,R],[R],[R],[Y],[Y],[Y],[R],[R],[R,R]]));
console.log(equalState(actions[3](init_state), [[B],[Y],[Y],[Y],[B]]));
console.log(equalState(actions[4](init_state),
[[Y,B],[Y, R],[Y,R],[Y,Y],[Y,Y],[Y,Y],[Y,R],[Y,R],[Y,B]]));
console.log(equalState(actions[5](init_state), [[B], [R,R], [Y,Y,Y], [R,R], [B]]));
//var dst = [[B], [R,R], [B,B,B], [R,R], [B]];
var dst = [[B,B], [B], [B,B,B,B], [B], [B,B]];
function search(used, ans, cur) {
if (equalState(dst, cur)) {
console.log(ans);
}
for (var i = 0; i < 6; ++i) {
if (!used[i]) {
used[i] = true;
var s = actions[i](cur);
search(used, ans.concat([i]), s);
used[i] = false;
}
}
}
search({}, [], init_state);
</script></body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment