Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Last active August 29, 2015 14:15
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 kadamwhite/8ae525609e48e29de8f0 to your computer and use it in GitHub Desktop.
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
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