Skip to content

Instantly share code, notes, and snippets.

@birkir
Created December 19, 2014 16:32
Show Gist options
  • Save birkir/a5ae49aeb860c15f140d to your computer and use it in GitHub Desktop.
Save birkir/a5ae49aeb860c15f140d to your computer and use it in GitHub Desktop.
Jóladagatal 16/24 - Lotukerfið
// HTTP 200 www.foo.is /ord
// paste in console
// parameters
var singles = 'hbcnofpskvyiwu'.split(''),
doubles = 'helibenenamgalsiclarcascticrmnfeconicuzngageassebrkrrbsrzrnbmotcrurhpdagcdinsnsbtexecsbalaceprndpmsmeugdtbdyhoertmybluhftareosirptauhgtlpbbipoatrnfrraacthpanppuamcmbkcfesfmmdnolrrfdbsgbhhsmt'.match(/.{1,2}/g),
elements = singles.concat(doubles),
words = document.querySelector('pre').innerHTML.split('\n');
// sort words by length
words.sort(function (a, b) { return (a.length < b.length) ? 1 : -1; });
// process first 10.000 words alpha-filtered
words = words.slice(0, 10000).filter(function (w) { return (w.match(/^[a-zA-Z]+$/) !== null); });
// Find longest word by number of iterations
function findByIterations(itrs) {
for (var w in words) {
for (var r = 0; r < itrs || 1; r++) {
var word = words[w];
for (var e in elements) {
if ((pos = word.indexOf(elements[e])) >= 0) {
word = word.substr(0, pos) + word.substr(pos + elements[e].length);
}
if (word.length === 0) {
return words[w];
}
}
// shuffle elements array for iteration
for (var j, x, i = elements.length; i; j = Math.floor(Math.random() * i), x = elements[--i], elements[i] = elements[j], elements[j] = x);
}
}
return null;
}
findByIterations(1000); // "starfsgreinasamningurinn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment