Finish 2011-04-20 12:00:00 UTC

columns2_submit

by James White

Status: Passed
Results: 7433330 (cyc: 5, node: 204)
CPU Time: 0.954
Score: 18583.4
Submitted at: 2011-04-14 14:37:58 UTC
Scored at: 2011-04-14 14:52:43 UTC

Current Rank: 1276th (Highest: 6th )
Basis for: three's company (diff)

Comments
Please login or create a profile.
Code
function board = solver(words, weights, n, penalty)

board = zeros(n);

for k = 1:length(words),
    wordlengths(k)=length(words{k});
end

nwords= numel(words);

[B,index] = sortrows([wordlengths' weights'],[1 -2]);

k=1;
c=1;

current_stop_index = min(nwords, n*k); % check that we haven't run out of words

while (c+B(current_stop_index,1)-1) <= n,

    % maybe be off if the last word in a column is longer thatn all the
    % others
    
    current_col_points = sum(B(((k-1)*n)+1:current_stop_index,2)) - penalty*B(k*n,1);
    if current_col_points > 0,
        r=1;
        words_left = current_stop_index - n*(k-1);
        for r = 1:min(n,words_left),
            board(c:(c+B(r+(k-1)*n,1)-1),r) = words{index(r+(k-1)*n)};
        end
    end
    c=c+B(current_stop_index,1)+1;
    

    k=k+1;
    current_stop_index = min(nwords, n*k);
end


end