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

shapeshifter 3

by Oli

Status: Passed
Results: 7321721 (cyc: 8, node: 536)
CPU Time: 1.158
Score: 18304.8
Submitted at: 2011-04-14 19:31:09 UTC
Scored at: 2011-04-14 19:39:02 UTC

Current Rank: 1232nd (Highest: 8th )

Comments
Oli
14 Apr 2011
another small sign mistake
Please login or create a profile.
Code
function board = solver(words, weights, n, penalty)


    

    
    b{1} = bylines(words, weights, n, penalty,1) ;
    b{2} = bylines(words, weights, n, penalty,2) ;
    b{3} = bycol(words, weights, n, penalty) ;
    
    
    c(1) = boguscount(b{1}.board)*penalty - b{1}.score ;
    c(2) = boguscount(b{2}.board)*penalty - b{2}.score ;
    c(3) = boguscount(b{3}.board)*penalty - b{3}.score ;
    
  
    
    [temp,imin] = min(c) ;

    board=b{imin}.board;
    
end


function out=boguscount(board)
    count=0  ;
    col=1 ;
    lin=1 ;
    n=length(board);

    while col<=n
        if board(lin,col)>0 & (lin==1 || board(lin-1,col)==0) & (lin<n && board(lin+1,col)>0)
            count=count+1 ;
        end
        lin=mod(lin+1-1,n)+1 ;
        
        if lin==1
            col=col+1 ;
        end
    end
out=count;
end


function out=bycol(words, weights, n, penalty)
    L=cellfun('length',words);
    weightperL=weights ./ L ;
    
    [a,order]=sort(L) ;
    board=zeros(n) ;
    
    orderw=weights(order);
    
    lin=1 ;
    col=ones(n,1) ;
    counter=1 ;
    finish=0;
    
    while finish==0;

        w=words{order(counter)} ;
        if col(lin)+length(w)-1<=n
            board(lin,col(lin):col(lin)+length(w)-1) = w ;
            col(lin)=col(lin)+length(w)+1 ;
        else
            finish=1 ;
        end
        lin=lin+1;
        if lin==n+1 
            lin=1 ;
            col=max(col)*ones(n,1);
        end
        counter=counter+1 ;
    end
    out.board=board;
    out.score=sum(orderw(1:counter));
end
  

function out=bylines(words, weights, n, penalty,skipline)
    L=cellfun('length',words);
     weightperL=weights ./ (L+1) ;    
    [a,order]=sort(-weightperL) ;
    board=zeros(n) ;    
    orderw=weights(order);
    lin=1;
    col=1;
    counter=1; 
    while lin<=n
        w=words{order(counter)};
        if col+length(w)-1<=n
            board(lin,col:col+length(w)-1) = w ;
            counter=counter+1;
            col = col + length(w) + 1;
        else
            lin=lin+skipline;
            col=1;
        end
    end
    
    out.board=board;
    out.score = sum(orderw(1:counter));
end