2011-04-20 12:00:00 UTC

# KNodes21-rev

Status: Passed
Results: 7187785 (cyc: 12, node: 888)
CPU Time: 5.88
Score: 17972.3
Submitted at: 2011-04-19 15:38:09 UTC
Scored at: 2011-04-19 15:59:49 UTC

Current Rank: 1115th (Highest: 725th )
Based on: KNodes21 (diff)

Comments
Please login or create a profile.
Code
```function board = solver(words, weights, n, penalty)
words = words(end:-1:1);
weights = weights(end:-1:1);
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);
[board1, result1] = solver_columns(words, weights, n, sumw,lengths,bboard);
if result1 < result
board = board1;
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=zeros(2); % 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```