Skip to content

Instantly share code, notes, and snippets.

@ThomasTheGerman
Created December 23, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasTheGerman/a0f857e714cca97da42043fb31773910 to your computer and use it in GitHub Desktop.
Save ThomasTheGerman/a0f857e714cca97da42043fb31773910 to your computer and use it in GitHub Desktop.
currset = [7 8 9 4 6 5 1 2 3];%,10:1000000];
% currset = [3 8 9 1 2 5 4 6 7];%,10:1000000];
N = length(currset);
smartlist = zeros(N,0);
for i = 1:N
smartlist(currset(i)) = currset(wrapN(i+1,N));
end
currcup = currset(1);
smartlist = iterate(smartlist,currcup,100);
part1 = zeros(8,1);
curr = 1;
for i = 1:8
part1(i) = smartlist(curr);
curr = smartlist(curr);
end
disp("Part1: " + strjoin(string(part1),''));
%part2
currset = [7 8 9 4 6 5 1 2 3,10:1000000];
% currset = [3 8 9 1 2 5 4 6 7,10:1000000];
N = length(currset);
smartlist = zeros(N,0);
for i = 1:N
smartlist(currset(i)) = currset(wrapN(i+1,N));
end
currcup = currset(1);
smartlist = iterate(smartlist,currcup,1e7);
part2 = uint64(smartlist(1)*smartlist(smartlist(1)))
function smartlist = iterate(smartlist,currcup,rounds)
N = length(smartlist);
for i = 1:rounds
pickedup = [smartlist(currcup),smartlist(smartlist(currcup)),smartlist(smartlist(smartlist(currcup)))];
smartlist(currcup) = smartlist(pickedup(end));
dest = wrapN(currcup -1,N);
while any(pickedup == dest)
dest = wrapN(dest-1,N);
end
buff = smartlist(dest);
smartlist(dest) = pickedup(1);
smartlist(pickedup(end)) = buff;
currcup = smartlist(currcup);
end
end
function r = wrapN(x,N)
r = (1 + mod(x-1, N));
end
% for i = 1:100
% if ~mod(i,10000)
% disp(i);
% end
% startindex = wrapN(find(currset == currcup)+1,N);
% range = wrapN(startindex:startindex+2,N);
% pickedup = currset(range);
% currset(range) = [];
% dest = wrapN(currcup-1,N);
% while any(pickedup == dest)
% dest = wrapN(dest -1,N);
% end
% % disp(dest);
% currset = [currset(1:find(currset==dest)),pickedup,currset(find(currset==dest)+1:end)];
% disp("result: " + strjoin(string(currset)));
% currcup = currset(wrapN(find(currset == currcup)+1,N));
% end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment