2011-04-20 12:00:00 UTC

# focus on result (kN3)

by Albert

Status: Passed
Results: 7189149 (cyc: 12, node: 999)
CPU Time: 7.707
Score: 17975.9
Submitted at: 2011-04-19 15:46:20 UTC
Scored at: 2011-04-19 16:13:30 UTC

Current Rank: 1117th (Highest: 730th )

Code
```function board = solver(words, weights, n, penalty)

words = words(weights>0);
weights = weights(weights>0);
lengths                 = cellfun('length',words);
[~,idx]                 = sortrows([weights'./lengths'  weights'],[-1 -2]);
words                   = words(idx);
weights                 = weights(idx);
lengths                 = lengths(idx);
bboard = zeros(n);
sumw = sum(weights);
[board,  result] = solver_press(words, weights, n, penalty, sumw,lengths,bboard);
[board2, result2] = yot(bboard,words, weights, n, lengths,sumw);
[board1, result1] = solver_columns(words, weights, n, sumw,lengths,bboard);
if result1 < result
board = board1;
elseif result2 < result
board = board2;
end

end

function [board, result] = solver_press(words, weights, n, penalty, result,wordlengths,board)
[B,index] = sortrows([wordlengths' weights'],[1 -2]); % index is wordsFromBIndex
last2letterword = find(B(:,1)>2,1)-1;
w2l = index(1:last2letterword)'; % should be row vector for for
isUnplayed = true(size(words));
hopeless = false(size(words));
[C,alphaix]=sortrows(cell2mat(words(w2l)')); % so char(C) = char(words{w2l(alphaix)}) % alphaix is WfromC
c=1;
r=1;
while r+1 <= n,
[sq, isUnplayed, hopeless] = press2(words, isUnplayed, hopeless, w2l, C, alphaix);
if ~sq,
break; % no more 2x2s
end
board(r+(0:1),c+(0:1)) = sq;
c=c+3;
if c > n-1,
r =r+3;
c=1;
end
end
result = result - sum(weights(~isUnplayed));
[B,index] = sortrows([wordlengths(isUnplayed)' weights(isUnplayed)'],[1 -2]);
words = words(isUnplayed);
nwords=numel(words);
k=1;
stop_index = min(nwords, n-c+1); % check that we haven't run out of words
start_index = 1;
while (r+B(stop_index,1)-1) <= n, % if the new row fits
words_left = stop_index - start_index + 1; % num words to play in this row
current_row_points = sum(B(start_index:stop_index,2)) - penalty*B(stop_index-1,1); % use length of next to last word to get number of penalty cols
if current_row_points
for cc = c:min(n,words_left+c-1),
wi = start_index-c+cc; % word index
board(r:(r+B(wi,1)-1),cc) = words{index(wi)};
end
end
c=1;
result = result - current_row_points;
r=r+B(stop_index,1)+1; % increment row location
k=k+1; % increment row number
start_index=stop_index+1;
stop_index = min(nwords, stop_index+n);
end
end

function [sq, isUnplayed, hopeless] = press2(words, isUnplayed, hopeless, w2l, C, alphaix)
sq = zeros(2);
for ii = w2l, % w2l must be a row vector here
if ~isUnplayed(ii) || hopeless(ii),
continue;
end
sq(1,:) = words{ii};
isUnplayed(ii) = false;
a1=find(C(:,1) == sq(1,1),1); a2=find(C(:,1) == sq(1,1),1,'last');
for jj = w2l(sort(alphaix(a1:a2))),
if isUnplayed(jj) && ~hopeless(jj), % eval order? also checks could be elim
sq(2,1) = words{jj}(2);
isUnplayed(jj) = false;
b1=find(C(:,1) == sq(1,2),1);
b2=find(C(:,1) == sq(1,2),1,'last');
c1=find(C(:,1) == sq(2,1),1);
c2=find(C(:,1) == sq(2,1),1,'last');
for kk = w2l(sort(alphaix(b1:b2))),
if isUnplayed(kk),
sq(2,2) = words{kk}(2);
isUnplayed(kk) = false;
for ll =  w2l(sort(alphaix(c1:c2))),
if isUnplayed(ll) && all(sq(2,:) == words{ll}),
isUnplayed(ll) = false;
return; % press completed!
end
end
isUnplayed(kk) = true;
end
end
isUnplayed(jj) = true;
end
end
isUnplayed(ii) = true;
hopeless(ii) = true; % hopeless in positions 1 or 2
end
sq=false; % no result
end

function [board, result] = solver_columns(words, weights, n, result,wordlengths,board)
wpl = weights./(wordlengths); % weight per letter, maybe should also consider blanks; +1 for blank at end of word acutally lowered result?
[~,ix] = sort(wpl,'descend'); % so now s_wpl = wpl(ix) % possible sign tricks?
w = 1;
for col = 1:2:min(n,length(words)),
r = 1;
while r + wordlengths(ix(w))-1 <= n
y=ix(w);
board(r:(r-1+wordlengths(y)),col) = words{y};
result = result - weights(y);
r = r + 1 + wordlengths(y);
w = w + 1;
end
end
end

function [board2, sumw] = yot(board2, words, weights, N, wlen, sumw)
open     = true(1,length(words));
wlen_m_N = wlen <= N;
for n = 1:2:N
l = 0;
k = find(open & wlen_m_N,1);
while ~isempty(k)
open(k)                = false;
word                   = words{k};
L_word                 = numel(word);
board2(n,l+1:l+L_word) = word;
l                      = l+L_word+1;
sumw                   = sumw - weights(k);
k                      = find(open & (wlen <= N-l),1);
end
end
end
```