Grid Computing


今回は縦横計算なので、組みやすいけれど、どこまで短縮できるかが鍵ということでしょうか?
自分の脳みそをダンプすると以下のようになりますが、縦横同じアルゴリズムで計算させた方が短くなりそうですね。(当然か?)

#! /usr/bin/gawk -f
# grid_computing.awk - problem from code golf
# http://codegolf.com/grid-computing
#    input:  arrayed text file from stdin
#    output: maximum value found by summing all the rows and columns
#            of a grid of numbers

{
    # initialize
    row_sum = 0;

    # calc summation of row and stack column
    for (i = 1; i <= NF; i++) {
        row_sum += $i;
        col_sum[i] += $i;
    }

    # max calc
    if (row_max < row_sum) {
        row_max = row_sum;
    }
}

END {

    # max calc
    for (i in col_sum) {
        if (max < col_sum[i]) {
            max = col_sum[i];
        }
    }

    # max calc
    if (max < row_max) {
        max = row_max;
    }

    print max;
}