Untitled

H
mail@pastecode.io avatar
unknown
javascript
a year ago
8.4 kB
6
Indexable
Never
function twoPluses(grid) {
    
    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);
    console.log(arr)
    for(let i = 0; i < arr.length; i++){
        arr[i].points = [];
        if(arr[i].value === 1){
            arr[i].points.push(arr[i].coordinates);
        }
        else{
            let num = 1;
            let numDown = -1;
            for(let j = 0; j < (arr[i].value - 1)/4; j++){
                let newNum = Number(arr[i].coordinates.split('-')[0])+ num;
                let newNumDown = Number(arr[i].coordinates.split('-')[0])+ numDown;
                arr[i].points.push(newNum.toString()+'-'+arr[i].coordinates.split('-')[1]);
                arr[i].points.push(newNumDown.toString()+'-'+arr[i].coordinates.split('-')[1]);
                num++;
                numDown--;
            }
            num= 1;
            numDown = -1;
            for(let n = 0; n < (arr[i].value - 1)/4; n++){
                let newNum = Number(arr[i].coordinates.split('-')[1]) + num;
                let newNumDown = Number(arr[i].coordinates.split('-')[1])+ numDown;
                arr[i].points.push(arr[i].coordinates.split('-')[0] + '-' + newNum.toString());
                arr[i].points.push(arr[i].coordinates.split('-')[0] + '-' + newNumDown.toString());
                num++;
                numDown--;
            }
            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){
               
                max = Math.max(max,arr[i].value * arr[j].value);
                     
                }
            }
        }
    }
    
    return max;
}