Skip to content

Instantly share code, notes, and snippets.

@ThomasTheGerman
Created December 24, 2020 12:19
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/d6dd3db504c6c032ab43944dc51210db to your computer and use it in GitHub Desktop.
Save ThomasTheGerman/d6dd3db504c6c032ab43944dc51210db to your computer and use it in GitHub Desktop.
tiles = getData('input.txt');
un = unique(tiles);
tiles = un(groupcounts(tiles) ==1);
part1 = length(tiles)
for k = 1:100
tiles = generation(tiles);
end
part2 = length(tiles)
function nexttiles = generation(tiles)
directions = [2;-2;1-1i;-1-1i;-1+1i;1+1i];
nexttiles = [];
for i = 1:length(tiles)
n = 0;
tile = tiles(i);
neighbors = tile+directions;
for j = 1:6
if ismember(neighbors(j),tiles)
n = n+1;
else
m = 0;
neighborsneighbors = neighbors(j)+directions;
for k = 1:6
if ismember(neighborsneighbors(k),tiles)
m = m+1;
end
end
if m == 2
nexttiles = union(neighbors(j),nexttiles);
end
end
end
if n > 0 && n <= 2
nexttiles = union(tile,nexttiles);
end
end
end
function data = getData(inputfile)
inp = string(strsplit(fileread(inputfile),"\n"))';
inp = strip(inp(1:end-1));
data = zeros(length(inp),1);
dirs = ["se","sw","nw","ne","w","e"];
for i = 1:length(inp)
for j = 1:length(dirs)
c = count(inp{i},dirs{j});
inp{i} = erase(inp{i},dirs{j});
switch dirs{j}
case "e"
data(i) = data(i) + c*2;
case "w"
data(i) = data(i) - c*2;
case "se"
data(i) = data(i) + c*(1-1i);
case "sw"
data(i) = data(i) + c*(-1-1i);
case "nw"
data(i) = data(i) + c*(-1+1i);
case "ne"
data(i) = data(i) + c*(1+1i);
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment