# Diffing "DarknessEnd2" and "TwilightStart2"

 Title: DarknessEnd2 TwilightStart2 Author: Alan Chalker Alan Chalker Submitted: 2011-04-14 14:17:41 UTC 2011-04-14 21:43:29 UTC Status: Passed Passed Score: 20273.8 20279.9 Result: 8100389 (cyc: 32, node: 871) 8103239 (cyc: 31, node: 831) CPU Time: 7.985 22.057 Code: ```function board = solver(words, weights, n, penalty) %% solver 1, weight per char, every other row % setup vars col = 1; row = 1; spaceavail=1; item=1; weightsum=0; % pre populate board board=zeros(n); %construct length array, in sorted order for x=1:size(words,2) len(x)=size(words{x},2); end % find weight per char [vals,locs]=sort(weights./len,2,'descend'); len=len(locs); while spaceavail % insert word into board at current location if (col+len(item)-1) <= n board(row,col:(col+len(item)-1))=words{locs(item)}; col = col+len(item)+1; if col >=n col=1; row=row+2; end weightsum=weightsum+weights(locs(item)); len(item)=[]; locs(item)=[]; item=1; else item=item+1; if item > numel(len) item=1; col=1; row=row+2; end end if row > n spaceavail=0; end end %% solver 2, weight per char, stagger step rows % setup vars col = 1; row = 1; spaceavail=1; item=1; weightsum2=0; alt=0; skipnextrow=0; % pre populate board board2=zeros(n); %construct length array, in sorted order for x=1:size(words,2) len(x)=size(words{x},2); end % find weight per char [vals,locs]=sort(weights./len,2,'descend'); len=len(locs); curlen=len(1); while spaceavail % insert word into board at current location if (col+len(item)-1) <= n board2(row+alt,col:(col+len(item)-1))=words{locs(item)}; col = col+len(item); if (curlen ~= len(item)) && (row > 1) && (row < n) if nnz(board2(row-1,(col-len(item)):end)) > 0 board2(row+1:row+2,:)=board2(row:row+1,:); board2(row,:)=zeros(1,n); row=row+1; end curlen = len(item); skipnextrow=1; end if alt alt=0; else alt=1; end if row == n alt=0; col=col+len(item); end if col >=n col=1; row=row+2+skipnextrow; skipnextrow=0; alt=0; end weightsum2=weightsum2+weights(locs(item)); len(item)=[]; locs(item)=[]; item=1; else item=item+1; if item > numel(len) item=1; col=1; row=row+2+skipnextrow; skipnextrow=0; alt=0; end end if row > n spaceavail=0; end end if weightsum2>weightsum board=board2; weightsum=weightsum2; end %% solver 3, length per char, stagger step rows % %% solver 3, length per char, stagger step rows % % setup vars % col = 1; % row = 1; % spaceavail=1; % item=1; % weightsum2=0; % alt=0; % skipnextrow=0; % % % pre populate board % board2=zeros(n); % % %construct length array, in sorted order % for x=1:size(words,2) % len(x)=size(words{x},2); % end % % % find weight per char % [vals,locs]=sort(len,2,'ascend'); % len=len(locs); % curlen=len(1); % % while spaceavail % % insert word into board at current location % if (col+len(item)-1) <= n % board2(row+alt,col:(col+len(item)-1))=words{locs(item)}; % col = col+len(item); % if (curlen ~= len(item)) && (row > 1) && (row < n) % board2(row+1:row+2,:)=board2(row:row+1,:); % board2(row,:)=zeros(1,n); % row=row+1; % curlen = len(item); % skipnextrow=1; % end % if alt % alt=0; % else % alt=1; % end % if row == n % alt=0; % col=col+len(item); % end % if col >=n % col=1; % row=row+2+skipnextrow; % skipnextrow=0; % alt=0; % end % weightsum2=weightsum2+weights(locs(item)); % len(item)=[]; % locs(item)=[]; % item=1; % else % item=item+1; % if item > numel(len) % item=1; % col=1; % row=row+2+skipnextrow; % skipnextrow=0; % alt=0; % end % end % % if row > n % spaceavail=0; % end % % end % % if weightsum2>weightsum % board=board2; % weightsum=weightsum2; % end %% solver 4, weight per char, every other row, fill in end of row % setup vars col = 1; row = 1; spaceavail=1; item=1; weightsum2=0; alt=0; skipnextrow=0; % pre populate board board2=zeros(n); %construct length array, in sorted order for x=1:size(words,2) len(x)=size(words{x},2); end minlen = min(len); % find weight per char [vals,locs]=sort(len,2,'ascend'); len=len(locs); curlen=len(1); % find weight per char [vals,locs]=sort(weights./len,2,'descend'); len=len(locs); maxlen = n; insertany = 0; while spaceavail % insert word into board at current location if (col+len(item)-1) <= n board2(row+alt,col:(col+len(item)-1))=words{locs(item)}; col = col+len(item); if (curlen ~= len(item)) && (row > 1) && (row < n) board2(row+1:row+2,:)=board2(row:row+1,:); board2(row,:)=zeros(1,n); row=row+1; curlen = len(item); skipnextrow=1; if len(item) <= maxlen newmaxlen = maxlen - len(item) - 1; newlen = len; newlen(item) =[]; newminlen = min(newlen); if (newmaxlen == -1) || (newminlen < newmaxlen)|| insertany board2(row,col:(col+len(item)-1))=words{locs(item)}; col = col+len(item)+1; weightsum2=weightsum2+weights(locs(item)); len=newlen; locs(item)=[]; item=1; maxlen = newmaxlen; minlen=newminlen; insertany=0; if col >=n col=1; row=row+2; maxlen=n; end if alt alt=0; else alt=1; item=item+1; if item > numel(len) item=1; insertany=1; end end if row == n alt=0; col=col+len(item); end if col >=n col=1; row=row+2+skipnextrow; skipnextrow=0; alt=0; end weightsum2=weightsum2+weights(locs(item)); len(item)=[]; locs(item)=[]; item=1; else item=item+1; if item > numel(len) item=1; col=1; row=row+2+skipnextrow; skipnextrow=0; alt=0; end end if row > n spaceavail=0; end end else item=item+1; if item > numel(len) item=1; col=1; row=row+2; end end if row > n spaceavail=0; end end if weightsum2>weightsum board=board2; weightsum=weightsum2; end end```