Skip to content

Instantly share code, notes, and snippets.

@lctseng
Created May 1, 2019 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lctseng/04fdbee30e5fe428ade853ac59013f30 to your computer and use it in GitHub Desktop.
Save lctseng/04fdbee30e5fe428ade853ac59013f30 to your computer and use it in GitHub Desktop.
function deepCompare() {
var i, l, leftChain, rightChain;
function compare2Objects(x, y) {
var p;
// remember that NaN === NaN returns false
// and isNaN(undefined) returns true
if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
return true;
}
// Compare primitives and functions.
// Check if both arguments link to the same object.
// Especially useful on the step where we compare prototypes
if (x === y) {
return true;
}
// Works in case when functions are created in constructor.
// Comparing dates is a common scenario. Another built-ins?
// We can even handle functions passed across iframes
if ((typeof x === 'function' && typeof y === 'function') ||
(x instanceof Date && y instanceof Date) ||
(x instanceof RegExp && y instanceof RegExp) ||
(x instanceof String && y instanceof String) ||
(x instanceof Number && y instanceof Number)) {
return x.toString() === y.toString();
}
// At last checking prototypes as good as we can
if (!(x instanceof Object && y instanceof Object)) {
return false;
}
if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
return false;
}
if (x.constructor !== y.constructor) {
return false;
}
if (x.prototype !== y.prototype) {
return false;
}
// Check for infinitive linking loops
if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
return false;
}
// Quick checking of one object being a subset of another.
// todo: cache the structure of arguments[0] for performance
for (p in y) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
}
}
for (p in x) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
}
switch (typeof (x[p])) {
case 'object':
case 'function':
leftChain.push(x);
rightChain.push(y);
if (!compare2Objects(x[p], y[p])) {
return false;
}
leftChain.pop();
rightChain.pop();
break;
default:
if (x[p] !== y[p]) {
return false;
}
break;
}
}
return true;
}
if (arguments.length < 1) {
return true; //Die silently? Don't know how to handle such case, please help...
// throw "Need two or more arguments to compare";
}
for (i = 1, l = arguments.length; i < l; i++) {
leftChain = []; //Todo: this can be cached
rightChain = [];
if (!compare2Objects(arguments[0], arguments[i])) {
return false;
}
}
return true;
}
var _findPuzzle = function (img) {
var selector = "img[src='" + img + "']"
return $(selector)
}
var _toAnagram = function (word) {
var anagram = {}
for (var i = 0; i < word.length; i++) {
if (!anagram[word[i]]) {
anagram[word[i]] = 0
}
anagram[word[i]] += 1;
}
return anagram
}
var _buildAnagram = function () {
if (document._anagram) {
return
}
var targets = [];
targets.push(...["OR", "IN", "AS", "SUM", "AND", "AVG", "MAX", "MIN"]);
targets.push(...["FROM", "LIKE", "TRIM", "WHERE", "COUNT", "CONCAT", "SELECT", "HAVING", "SUBSTR", "LENGTH"]);
targets.push(...["BETWEEN", "REPLACE", "ORDER BY", "DISTINCT", "GROUP BY", "CREATE VIEW", "ALTER TABLE", "CREATE TABLE", "CREATE INDEX"]);
targets.push(...["autonomous", "adwc simple", "oracle adwc", "adwc easy"]);
// build anagram
document._anagram = [];
for (var i = 0; i < targets.length; i++) {
var word = targets[i].toUpperCase();
var anagram = _toAnagram(word);
var ent = [anagram, word]
document._anagram.push(ent)
}
console.log("Anagram built")
console.log(document._anagram)
};
var _search = function (word) {
for (var i = 0; i < document._anagram.length; i++) {
if (deepCompare(document._anagram[i][0], _toAnagram(word))) {
return document._anagram[i][1];
}
}
return "NOT_FOUND"
}
_buildAnagram();
// searching
var _solveKey = function (key) {
var puzzle = _findPuzzle(key);
if (puzzle.length > 1) {
// solve this puzzle
var parents = puzzle.parent()
var word = []
for (var j = 1; j < puzzle.length; j++) {
var t = parents[j].children[1].innerText;
if (t.length == 0) {
t = ' '
}
word.push(t);
}
var ans = _search(word)
console.log("solving: " + word + " --> " + ans)
$("#hiddeninput").val(ans)
}
}
var _solve = function () {
var keys = ["img/dest/game-triblue.png", "img/dest/game-triblack.png", "img/dest/game-trio.png", "img/dest/game-trir.png"];
for (var i = 0; i < keys.length; i++) {
setTimeout((key) => {
_solveKey(key)
}, i * 100, keys[i])
}
}
var _autoSolve = function () {
document._autoInterval = setInterval(_solve, 1000)
}
var _stopSolve = function () {
clearInterval(document._autoInterval)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment