Created
August 17, 2010 08:09
-
-
Save fuba/528907 to your computer and use it in GitHub Desktop.
This file contains 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
// ==UserScript== | |
// @name shiritori helper | |
// @namespace http://fuba.moaningnerds.org/ | |
// @include http://gdd-2010-quiz-japan.appspot.com/problems?problem=shiritori* | |
// @require https://gist.github.com/184276.txt | |
// ==/UserScript== | |
String.prototype.head = function () { | |
return this.charAt(0); | |
}; | |
String.prototype.tail = function () { | |
return this.charAt(this.length - 1); | |
}; | |
String.prototype.x = function (number) { | |
var result = ''; | |
for (var i = 0; i< number; i++) { | |
result += this; | |
} | |
return result; | |
}; | |
Array.prototype.diff = function (diffTargetObject, log) { | |
var loghash = {}; | |
for (var i = 0; i < log.length; i++) { | |
loghash[log[i]] = 1; | |
} | |
var newArray = []; | |
this.forEach(function (word) { | |
if (!(diffTargetObject[word] || loghash[word])) { | |
newArray.push(word); | |
} | |
}); | |
return newArray; | |
}; | |
Array.prototype.getHeads = function (head) { | |
var heads = {}; | |
var regexp = new RegExp('^'+head); | |
this.forEach(function (word) { | |
if (regexp.test(word)) { | |
heads[word] = word.tail(); | |
} | |
}); | |
return heads; | |
}; | |
Array.prototype.hasLoop = function () { | |
var is_mine = false; | |
var log_me = {}; | |
var log_him = {}; | |
for (var i = 0; i < this.length; i++) { | |
var head = this[i].head(); | |
if (is_mine) { | |
if (log_me[head]) return 1; | |
log_me[head] = 1; | |
is_mine = false; | |
} | |
else { | |
if (log_him[head]) return 2; | |
log_him[head] = 1; | |
is_mine = true; | |
} | |
} | |
return 0; | |
}; | |
var maxDepth = 4; | |
function doShiritori (list, history, log, targettext, depth) { | |
var result = ''; | |
if (depth > maxDepth) return result; | |
var nexts = list.diff(history, log).getHeads(targettext.tail()); | |
for (var word in nexts) { | |
var newLog = log.concat(word); | |
var hasLoop = newLog.hasLoop(); | |
if (hasLoop == 0) { | |
if (depth == maxDepth) { | |
//result += newLog.join(' > ') + "\n"; | |
} | |
result += doShiritori( | |
list.diff(history, log), | |
history, | |
log.concat(word), | |
word, | |
depth + 1 | |
); | |
} | |
else if (hasLoop == 1) { | |
result += '[DANGER]' + newLog.join(' > ') + "\n"; | |
} | |
else { | |
result += '[ SAFE ]' + newLog.join(' > ') + "\n"; | |
} | |
} | |
return result; | |
} | |
function main () { | |
var defaultList = []; | |
$X('//ul[1]/li/text()').forEach(function(node){ | |
defaultList.push(node.textContent); | |
}); | |
var defaultHistory = {}; | |
$X('//ul[2]/li/text()').forEach( | |
function(node){ | |
defaultHistory[node.textContent] = 1; | |
} | |
); | |
var targets = $X('//input[@name="answer0"]'). | |
map(function(node){return node.value}); | |
var pre = document.createElement('pre'); | |
for (var i = 0; i < targets.length; i++) { | |
if (targets[i] != 'reset') { | |
pre.innerHTML = doShiritori( | |
defaultList.diff(defaultHistory, []), | |
defaultHistory, | |
[], | |
targets[i], | |
1 | |
); | |
} | |
} | |
$X('id("main")')[0].appendChild(pre); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment