Created
July 21, 2015 17:27
-
-
Save rhettl/585c6ce15de692e8ce1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by rhett on 7/7/15. | |
*/ | |
var permutation = function (col){ | |
if (col.length <= 1) { | |
return col; | |
} else if (col.length === 2) { | |
return [ | |
[col[0], col[1]], | |
[col[1], col[0]] | |
] | |
} else { | |
//console.log(col); | |
return col.reduce(function(last, item, index){ | |
//console.log(item, index); | |
var subset = col.concat([]); | |
subset.splice(index, 1); | |
//console.log(subset) | |
return last.concat( | |
permutation(subset).map(function(sub){ | |
return [item].concat(sub); | |
}) | |
); | |
}, []); | |
} | |
}; | |
var decodeUbchiNums = function(str, order){ | |
var segs = []; | |
var step = str.length / order.length; | |
for (var j = 0; j < step; j ++) { | |
segs.push([]); | |
} | |
for(var i = 0; i < str.length; i += step) { | |
var slice = str.slice(i, i + step).split(''); | |
for (j = 0; j < step; j ++) { | |
segs[j].push(slice.shift()); | |
} | |
} | |
var out = ''; | |
for (i = 0; i < order.length; i ++) { | |
for(j = 0; j < segs.length; j ++) { | |
out += segs[j][ order[i] - 1 ]; | |
} | |
} | |
return out | |
}; | |
//var str = 'OOEUSLPLOCTXITRZ BW'.split(' ')[0].split(''); | |
var str = 'OOEUSLPLOCTXITRZ BW'.replace(/\s/g, ''); | |
var numRows = []; | |
for(var i = 2, l = str.length; i < l; i ++) { | |
var multiple = str.length / i; | |
if (multiple != parseInt(multiple)) continue; | |
// This is a factor | |
// Make array of 1-n, ex for 3 => [1,2,3] | |
var nums = []; | |
for (var j = 1; j <= i; j++) { | |
nums.push(j); | |
} | |
// make permutation set | |
// for 3 => [1,2,3] => [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] | |
var perms = permutation(nums); | |
//check against sets | |
var res = perms.map(function(order){ | |
return [ | |
decodeUbchiNums(decodeUbchiNums(str, order), order), | |
order | |
]; | |
}).forEach(function(s){ | |
if(s[0].match(/^pillbox/i)) { | |
console.log(s) | |
} | |
}); | |
//console.log(res) | |
} | |
//console.log(numRows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment