Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
8.3 kB
3
Indexable
function twoPluses(grid) {
    let objI;
    let objJ;
    let max = -Infinity;
    let count;
    let countUp;
    let countDown;
    let arr = [];
    function compareValue(a, b) {
      if (a.value < b.value) {
      return 1;
    }
      if (a.value > b.value) {
      return -1;
    }
      return 0;
    }
    
    for(let i = 0; i< grid.length; i++){
        grid[i] = grid[i].split('');
        
    }
    
    for(let i = 0; i < grid.length; i++){
        count = 0;
        
        
        for(let j = 0; j<grid[i].length; j++){
            if(grid[i][j] === 'G'){
                count++;
                if(count === 1 ){
                   let obj = {};
                    obj.value = count;
                    obj.coordinates = i.toString()+'-'+j.toString();
                    arr.push(obj);
                }else if(count%2 === 1){
                    countUp = 0;
                    countDown = 0;
                    let a = j - Math.floor(count/2);
                    for(let k = i-1 ; k >= 0; k--){
                        if(grid[k][a]=== 'G'){
                            countUp++;
                        }else{
                            break;
                        }
                    }
                    for(let l = i+1; l<grid.length; l++){
                        if(grid [l][a]=== 'G'){
                            countDown++;
                        }
                        else{
                            break;
                        }
                    }
                    let b = Math.min(countUp,countDown);
                    if((count-1)/2 < b){
                        let obj2 = {};
                        obj2.value = 4 * ((count-1)/2) +1;
                        obj2.coordinates = i.toString()+'-'+ a.toString()
                        arr.push(obj2);
                    }else{
                        let obj2 = {};
                        obj2.value = 4* b + 1;
                        obj2.coordinates = i.toString()+'-'+ a.toString();
                        arr.push(obj2);
                    }
                }
                else{
                    countUp = 0;
                    countDown = 0;
                    let c = count -1;
                    let a = j - Math.floor(c/2);
                    for(let k = i-1 ; k >= 0; k--){
                        if(grid[k][a]=== 'G'){
                            countUp++;
                        }else{
                            break;
                        }
                    }
                    for(let l = i+1; l<grid.length; l++){
                        if(grid [l][a]=== 'G'){
                            countDown++;
                        }
                        else{
                            break;
                        }
                    }
                    let b = Math.min(countUp,countDown);
                    if((c-1)/2 < b){
                        let obj2 = {};
                        obj2.value = 4 * ((c-1))/2 +1;
                        obj2.coordinates = i.toString()+'-'+ a.toString()
                        arr.push(obj2);
                    }else{
                        let obj2 = {};
                        obj2.value = 4* b + 1;
                        obj2.coordinates = i.toString()+'-'+ a.toString();
                        arr.push(obj2);
                    }
                    
                }
            }else{
                count = 0;
            }
        }
        count = 0;
        for(let j = grid[i].length; j>=0; j--){
            if(grid[i][j] === 'G'){
                count++;
                if(count === 1 ){
                   let obj = {};
                    obj.value = count;
                    obj.coordinates = i.toString()+'-'+j.toString();
                    arr.push(obj);
                }else if(count%2 === 1){
                    countUp = 0;
                    countDown = 0;
                    let a = j + Math.floor(count/2);
                    for(let k = i-1 ; k >= 0; k--){
                        if(grid[k][a]=== 'G'){
                            countUp++;
                        }else{
                            break;
                        }
                    }
                    for(let l = i+1; l<grid.length; l++){
                        if(grid [l][a]=== 'G'){
                            countDown++;
                        }
                        else{
                            break;
                        }
                    }
                    let b = Math.min(countUp,countDown);
                    if((count-1)/2 > b){
                        let obj2 = {};
                        obj2.value = 4 * b +1;
                        obj2.coordinates = i.toString()+'-'+ a.toString()
                        arr.push(obj2);
                    }else{
                        let obj2 = {};
                        obj2.value = 4* ((count-1)/2) + 1;
                        obj2.coordinates = i.toString()+'-'+ a.toString();
                        arr.push(obj2);
                    }
                }
                else{
                    countUp = 0;
                    countDown = 0;
                    let c = count -1;
                    let a = j + Math.floor(c/2);
                    for(let k = i-1 ; k >= 0; k--){
                        if(grid[k][a]=== 'G'){
                            countUp++;
                        }else{
                            break;
                        }
                    }
                    for(let l = i+1; l<grid.length; l++){
                        if(grid [l][a]=== 'G'){
                            countDown++;
                        }
                        else{
                            break;
                        }
                    }
                    let b = Math.min(countUp,countDown);
                    if((c-1)/2 > b){
                        let obj2 = {};
                        obj2.value = 4 * b +1;
                        obj2.coordinates = i.toString()+'-'+ a.toString()
                        arr.push(obj2);
                    }else{
                        let obj2 = {};
                        obj2.value = 4* ((c-1)/2) + 1;
                        obj2.coordinates = i.toString()+'-'+ a.toString();
                        arr.push(obj2);
                    }
                    
                }
            }else{
                count = 0;
            }
        }
        
    }
    
    arr.sort(compareValue);
    
    for(let i = 0; i < arr.length; i++){
        arr[i].points = [];
        if(arr[i].value === 1){
            arr[i].points.push(arr[i].coordinates);
        }
        else{
           
            for(let j = 1; j < (arr[i].value - 1)/4; j++){
                let newNum = Number(arr[i].coordinates.split('-')[0])+ j;
                let newNumDown = Number(arr[i].coordinates.split('-')[0])- j;
                arr[i].points.push(newNum.toString()+'-'+arr[i].coordinates.split('-')[1]);
                arr[i].points.push(newNumDown.toString()+'-'+arr[i].coordinates.split('-')[1]);
                
            }
            
            for(let n = 1; n < (arr[i].value - 1)/4; n++){
                let newNum = Number(arr[i].coordinates.split('-')[1]) + j;
                let newNumDown = Number(arr[i].coordinates.split('-')[1])- j;
                arr[i].points.push(arr[i].coordinates.split('-')[0] + '-' + newNum.toString());
                arr[i].points.push(arr[i].coordinates.split('-')[0] + '-' + newNumDown.toString());
                
            }
            arr[i].points.push(arr[i].coordinates);
        }
        
    }
    
    for(let i = 0; i< arr.length-1; i++){
        for(let j = i+1 ; j < arr.length; j++){
            let countResult = 0;
            for(let k = 0; k < arr[i].points.length; k++){
                if(arr[j].points.includes(arr[i].points[k])){
                    break;
                }else {
                    countResult++;
                }
            }
            if(countResult === arr[i].points.length){
               
                if(arr[i].value * arr[j].value > max){
                    max = arr[i].value* arr[j].value;
                     objI = arr[i];
                     objJ = arr[j];
                }
            }
        }
    }
    
    return max;
}