Cube Summation

  • + 0 comments
    def cubeSum(n, operations):
        matrix = {}
        
        block_sums = []
        for operation in operations:
            split_result = operation.split(' ')
            op = split_result[0]
    
            if op == 'UPDATE':
                x, y, z, w = split_result[1:]
                x, y, z, w = map(int, [x, y, z, w])
                matrix[(x, y, z)] = w
            else:
                x1, y1, z1, x2, y2, z2 = split_result[1:]
                x1, y1, z1, x2, y2, z2 = map(int, [x1, y1, z1, x2, y2, z2])
                block_sum = 0
                for (x, y, z), w in matrix.items():
                    if x1 <= x <= x2 and y1 <= y <= y2 and z1 <= z <= z2:
                        block_sum += w
                block_sums.append(block_sum)
        return block_sums