Skip to content

Instantly share code, notes, and snippets.

@safareli
Last active December 23, 2015 03:29
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 safareli/6574073 to your computer and use it in GitHub Desktop.
Save safareli/6574073 to your computer and use it in GitHub Desktop.
Array.prototype.clone = function() {
var arr = this.slice(0);
for( var i = 0; i < this.length; i++ ) {
if( this[i].clone ) {
//recursion
arr[i] = this[i].clone();
}
}
return arr;
}
var H = {
DEBUG :true,
log:function(t){
if (this.DEBUG)
console.log(t);
},
logMatrix:function(_array){
var array = _array.clone();
for (var i = 0; i < array.length; i++) {
array[i] = array[i].join(", ");
}
this.log("[\n\t["+array.join("],\n\t[")+"]\n]");
}
};
function Solver () {
this.isDoing = false;
}
Solver.prototype.setInput = function(_matrix) {
this.matrix = _matrix.clone();
this.columnMax = this.matrix[0].length-2;
this.rowMax = this.matrix.length-1;
this.pilot = {
value : null,
column : 0,
row : 0,
direction : 1
};
};
Solver.prototype.solv = function() {
while(this.setNextPilot()){
H.logMatrix(this.matrix);
H.log("pilot="+this.pilotToString());
var i = this.pilot.row + this.pilot.direction;
while(true){
H.log("i="+i);
if ((this.pilot.direction==1 && i >= this.matrix.length) || (this.pilot.direction==-1 && i < 0) )
break;
var target = this.matrix[i][this.pilot.column];
var k = target/this.pilot.value * -1;
H.log("i="+i+"; "+"target="+target+"; "+"k="+k);
for (var j = this.pilot.column; j < this.matrix[i].length; j++) {
H.log("j="+j);
this.matrix[i][j] += this.matrix[this.pilot.row][j] * k ;
}
i+= this.pilot.direction;
}
if(this.pilot.direction==-1 && (this.pilot.column == 0 || this.pilot.row == 0)) {
return this.matrix;
}
}
return this.matrix;
};
Solver.prototype.setNextPilot = function() {
var p = this.pilot;
var _row = p.row;
var _column = p.column;
if (p.value == null) {
p.value = this.matrix[p.row][p.column];
while(p.value == 0 && p.column < this.columnMax){
p.row++;
if (p.row > this.rowMax){
p.row = 0;
p.column++;
}
p.value = this.matrix[p.row][p.column];
}
if (p.column == this.columnMax && p.value ==0) {
return false;
}
this.matrix.splice(0, 0, this.matrix.splice(p.row, 1)[0]);
p.row = 0;
return true;
};
p.row += p.direction;
if (p.row > this.rowMax) {
p.direction = -1;
p.row-=2;
}
if (p.direction == 1) {
do{
p.column++;
}while(p.column < this.columnMax && this.matrix[p.row][p.column] == 0)
if (p.column == this.columnMax && this.matrix[p.row][p.column] ==0) {
if (this.matrix[p.row][p.column+1] != 0) {
return false;
}else if (p.row == this.rowMax) {
p.direction = -1;
return this.setNextPilot();
}else{
p.column = _column;
return this.setNextPilot();
}
}
if (this.matrix[p.row][p.column] == 0) {
p.direction = -1;
p.row = _row;
p.column = _column;
return true;
}
if (p.column == this.columnMax) {
p.direction = -1;
}
}else{
p.column = 0;
while(p.column < this.columnMax && this.matrix[p.row][p.column] == 0){
p.column++;
}
if (p.column == this.columnMax && this.matrix[p.row][p.column] ==0) {
return this.setNextPilot();
}
}
p.value = this.matrix[p.row][p.column];
return true;
};
Solver.prototype.pilotToString = function() {
var str = "{";
for(key in this.pilot){
str += "\n\t"+key+" : "+this.pilot[key]
}
return str+"\n}"
};
// var input =[
// [1,-2,1,0],
// [0,2,-8,8],
// [-4,5,9,-9]
// ];
// var input =[
// [1,6,2,-5,-2,-4],
// [0,0,2,-8,-1,3],
// [0,0,0,0,1,7]
// ];
// var input =[
// [2,4,-4],
// [5,7,11]
// ];
// var input =[
// [1,5,7],
// [-2,-7,-5]
// ];
// var input =[
// [1,5,7],
// [-2,-7,-5]
// ];
// var input =[
// [1,-5,1],
// [3,-7,5]
// ];
// var input =[
// [1,-4,5,0,7],
// [0,1,-3,0,6],
// [0,0,1,0,2],
// [0,0,0,1,-5],
// ];
// var input = [
// [1 ,-6,4,0,-1],
// [0,2,-7,0,64],
// [0,0,1,2,-3],
// [0,0,3,1,6]
// ];
// var input = [
// [1 ,7,3,0,-4],
// [0,1,-1,0,3],
// [0,0,0,0,1],
// [0,0,1,1,-2]
// ];
// var input = [
// [1,-4,9,0],
// [0,1,7,0],
// [0,0,2,0]
// ];
// var input = [
// [1,-1,0,0,-4],
// [0,1,-3,0,-7],
// [0,0,1,-3,-2],
// [0,0,0,2,4]
// ];
// var input = [
// [1,-2,0,3,-2],
// [0,1,0,-4,7],
// [0,0,1,0,6],
// [0,0,0,1,-3]
// ];
// var input = [
// [1,3,5,-2],
// [0,1,4,-5],
// [3,7,7,6]
// ];
// var input = [
// [1,-3,4,-4],
// [3,-7,7,-8],
// [-4,6,-1,7],
// ];
// var input = [
// [1,0,-3,8],
// [2,2,9,7],
// [0,1,5,-2]
// ];
// var input = [
// [1,-3,0,5],
// [-1,1,5,2],
// [0,1,1,0]
// ];
// var input = [
// [1,0,3,0,2],
// [0,1,0,-1,3],
// [3,-2,3,2,1],
// [1,0,0,7,-5]
// ];
var input = [
[1,0,0,-2,-2],
[0,2,2,0,0],
[0,0,0,0,0],
[0,0,1,3,1],
[-2,3,3,1,5],
];
// var input = [
// [0],
// [0],
// ];
// var input = [
// [0],
// [0],
// ];
// var input = [
// [0],
// [0],
// ];
var s = new Solver();
s.setInput(input);
H.logMatrix(s.solv());
H.logMatrix(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment