Skip to content

Instantly share code, notes, and snippets.

@ErBlack
Created March 5, 2014 19:41
Show Gist options
  • Save ErBlack/9374941 to your computer and use it in GitHub Desktop.
Save ErBlack/9374941 to your computer and use it in GitHub Desktop.
Might And Magick columns solution (brutal)
var rules = {
l: [0,-1,1,1],
r: [1,-1,-1,0],
b: [-1,1,0,1]
},
go = function(start, direction) {
return start.map(function(value, index) {
var val = value + rules[direction][index];
if (val < 0) val = 4 + val;
return val == 4 ? 0 : val;
});
},
walk = function(path, start) {
console.info('walking path:', path);
console.info('start:', start);
Array.prototype.forEach.call(path, function(step) {
console.info('step:', step, start = go(start, step));
});
}
route = [{
path: '',
position: [3,0,1,2]
}],
finish = false,
makeStep = function() {
var steps = [];
route.forEach(function(step) {
Object.keys(rules).forEach(function(direction) {
var path = step.path + direction,
position = go(step.position, direction);
if (position.join('') === '0000') {
console.info('path', path, position.join(''));
finish = true;
}
steps.push({
path: path,
position: position
});
})
});
route = steps;
}
var i = 0;
while (!finish) {
makeStep()
console.info('step', i++)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment