Last active
August 29, 2015 14:15
-
-
Save kadamwhite/8ae525609e48e29de8f0 to your computer and use it in GitHub Desktop.
Solve the "Hands of Time" temporal anomaly puzzles in Final Fantasy XIII-2
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
function Numbers( arr ) { | |
this.numbers = arr; | |
this.length = arr.length; | |
} | |
Numbers.prototype.wrapIdx = function( idx ) { | |
if ( 0 <= idx && idx < this.length ) { | |
return idx; | |
} | |
if ( idx >= this.length ) { | |
return idx % this.length; | |
} | |
if ( idx < 0 ) { | |
return idx + this.length; | |
} | |
} | |
Numbers.prototype.at = function( idx ) { | |
return this.numbers[ this.wrapIdx( idx ) ]; | |
} | |
Numbers.prototype.next = function( idx ) { | |
var currentNum = this.at( idx ); | |
this.numbers[ idx ] = null; | |
return [ | |
this.wrapIdx( idx + currentNum ), | |
this.wrapIdx( idx - currentNum ) | |
]; | |
}; | |
Numbers.prototype.dupe = function() { | |
return new Numbers( [].concat( this.numbers ) ); | |
}; | |
function test( arr ) { | |
var targetLength = arr.length; | |
var origNumbers = arr; | |
function check( numbers, idx, idxSoFar ) { | |
// console.log( numbers.numbers ); | |
var currentVal = numbers.at( idx ); | |
if ( currentVal === null ) { | |
return; | |
} | |
var indices = [].concat( idxSoFar, idx ); | |
if ( indices.length === targetLength ) { | |
console.log( 'did it! (' + indices.join(', ') + ')' ); | |
return; | |
} | |
var next = numbers.next( idx ); | |
return next.forEach(function( nextIdx ) { | |
check( numbers.dupe(), nextIdx, indices ); | |
}); | |
} | |
return origNumbers.map(function( val, idx ) { | |
return { | |
start: idx + ' (val == ' + val + ')', | |
order: check( new Numbers( origNumbers ).dupe(), idx, [] ) | |
}; | |
}); | |
} | |
var results = test([ | |
6, | |
5, | |
6, | |
4, | |
3, | |
5, | |
3, | |
4, | |
6, | |
4, | |
3, | |
1 | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment