• + 0 comments

    C++

    // stores the sums, in order
    std::set<int> hourglass_sums;
     
    /*
    A core is the middle point of the glasshour
    a b c 
      d 
    e f g
     in the above, the core is d */
    
    // finds the sum of the three neighbors above the core
    int sumOfUpperNeighbors(int core_y, int core_x, vector<vector<int>> arr) { 
        int left_most_cordinate = core_x - 1; 
        int right_most_cordinate = core_x + 1; 
        int upper_coordinate = core_y - 1; 
        
        int sum =0; 
        sum += arr[upper_coordinate][left_most_cordinate];
        sum += arr[upper_coordinate][core_x];
        sum += arr[upper_coordinate][right_most_cordinate];
        return sum;
    }
    
    // finds the sum of the three neighbors above the core
    int sumOfLowerNeighbors(int core_y, int core_x, vector<vector<int>> arr) { 
        int left_most_cordinate = core_x - 1; 
        int right_most_cordinate = core_x + 1; 
        int lower_coordinate = core_y + 1; 
        
        int sum =0; 
        sum += arr[lower_coordinate][left_most_cordinate];
        sum += arr[lower_coordinate][core_x];
        sum += arr[lower_coordinate][right_most_cordinate];
        return sum;
    }
    
    int hourglassSum(vector<vector<int>> arr) {    
        int total_sum = 0;
        for(int i = 1; i < 5; i++){
            for(int j = 1; j < 5; j++){
                total_sum += arr[i][j]; 
                total_sum += sumOfUpperNeighbors(i,j,arr);
                total_sum += sumOfLowerNeighbors(i,j,arr);
                hourglass_sums.insert(total_sum);
                total_sum = 0;
    
        }
    }
    return *hourglass_sums.rbegin();
    

    }