2011-11-09 12:00:00 UTC

# Vines before the twilight

by Victoria

Status: Failed
Results: Failed (timeout)

Code
```function [moves, vine] = solver(board, limit)

% Victoria's solver

moves = [];
N = size(board);
M = N(1)*N(2);
Q = floor(M/5.5);
vineVariants = cell(Q,1);
R = zeros(Q,1);
[~,I]=sort(board(:),'descend');
for i=1:Q
vine = I(i);
Continue = true;
while Continue
Check = setdiff(neighbours(vine(1)),vine);
Check = Check(board(Check)<=board(vine(1)));
if ~isempty(Check)
[~,PreviousInCheck] = max(board(Check));
vine = [Check(PreviousInCheck) vine];
else
Continue = false;
end
end
vineVariants{i} = vine;
R(i) = result(board,board,vineVariants{i});
end
[~,S] = min(R);
vine = vineVariants{S};

function LN = neighbours(L)
if L == 1
LN = [2 N(1)+1];
elseif L == N(1)
LN = [2*N(1) N(1)-1];
elseif L == M - N(1) + 1
LN = [M-N(1)+2 M-2*N(1)+1];
elseif L == N(1)*N(2)
LN = [M-1 M-N(1)];
elseif L >= 2 && L <= N(1)
LN = [L-1 L+N(1) L+1];
elseif L >= M - N(1) && L <= M - 1
LN = [L-1 L-N(1) L+1];
elseif mod(L,N(1)) == 1
LN = [L-N(1) L+1 L+N(1)];
elseif mod(L,N(1)) == 0
LN = [L-N(1) L-1 L+N(1)];
else
LN = [L-1 L+N(1) L+1 L-N(1)];
end
end
function R = result(board,modified_board,vine)
R = sum(board(:)) - sum(modified_board(vine));
end
end

```