Skip to content

Instantly share code, notes, and snippets.

@ThomasTheGerman
Created December 20, 2020 17:43
Show Gist options
  • Save ThomasTheGerman/5cb6da4c4832e4217e6e23bd2a782814 to your computer and use it in GitHub Desktop.
Save ThomasTheGerman/5cb6da4c4832e4217e6e23bd2a782814 to your computer and use it in GitHub Desktop.
data = getData("input.txt");
IDs = cell2mat(data.keys);
IDs_cpy = IDs;
image = zeros(sqrt(length(IDs)));
ID1 = IDs(1);
tile = data(ID1);
x = 6;
y = 6;
image(x,y) = ID1;
newtiles = ID1;
finished = [];
while ~isempty(IDs_cpy)
newnewtiles = [];
for i = 1:length(newtiles)
ID1 = newtiles(i);
tile = data(ID1);
IDs_cpy(ismember(IDs_cpy,finished)) = [];
IDs_cpy(ismember(IDs_cpy,newnewtiles)) = [];
% disp(length(IDs_cpy));
for j = 1:length(IDs_cpy)
[x,y] = find(image == ID1);
ID2 = IDs_cpy(j);
if ID1 == ID2
continue;
end
[edge1,edge2] = getMatch(ID1,ID2,data);
if isempty(edge2)
continue;
end
if edge2 == -1 || edge2 == -3
data(ID2) = flipud(data(ID2));
edge2 = -edge2;
elseif edge2 == -2 || edge2 == -4
data(ID2) = fliplr(data(ID2));
edge2 = -edge2;
end
switch num2str([edge1,edge2])
case {'1 1','3 3'}
data(ID2) = flipud(rot90(data(ID2),2));
case {'2 2','4 4'}
data(ID2) = fliplr(rot90(data(ID2),2));
case {'1 3','2 4','3 1','4 2'}
case {'1 2','3 4'}
data(ID2) = flipud(rot90(data(ID2),1));
case {'4 3','2 1'}
data(ID2) = fliplr(rot90(data(ID2),-1));
case {'1 4','3 2'}
data(ID2) = rot90(data(ID2),-1);
case {'2 3','4 1'}
data(ID2) = rot90(data(ID2),1);
otherwise
error("oops" + num2str([edge1,edge2]));
end
switch edge1
case 1
if y == length(image)
image_cpy = zeros(sqrt(length(IDs)));
image_cpy(:,1:end-1) = image(:,2:end);
image = image_cpy;
y = y-1;
end
image(x,y+1) = ID2
case 2
if x == 1
image_cpy = zeros(sqrt(length(IDs)));
image_cpy(2:end,:) = image(1:end-1,:);
image = image_cpy;
x = 2;
end
image(x-1,y) = ID2
case 3
if y == 1
image_cpy = zeros(sqrt(length(IDs)));
image_cpy(:,2:end) = image(:,1:end-1);
image = image_cpy;
y = 2;
end
image(x,y-1) = ID2
case 4
if x == length(image)
image_cpy = zeros(sqrt(length(IDs)));
image_cpy(1:end-1,:) = image(2:end,:);
image = image_cpy;
x = x-1;
end
image(x+1,y) = ID2
end
newnewtiles = union(newnewtiles,ID2);
end
finished = union(finished,newtiles);
end
newtiles = setdiff(newnewtiles,finished);
end
part1 = uint64(image(1,1)*image(1,end)*image(end,1)*image(end,end))
bw = [];
for i = 1:numel(image)
tile = data(image(i));
tile = tile(2:end-1,2:end-1);
[x,y] = ind2sub(size(image),i);
x = x*8-7;
y = y*8-7;
bw(x:x+7,y:y+7) = tile;
end
nessie = logical([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0;
1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1;
0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0]);
part2 = 0;
turns = 0;
while turns <8
turns = turns+1;
for i = 1:size(bw,1)-2
for j = 1:size(bw,2)-19
if all((bw(i:i+2,j:j+19) & nessie) == nessie,'all')
bw(i:i+2,j:j+19) = xor(bw(i:i+2,j:j+19),nessie);
end
end
end
if turns == 4
bw = flip(bw);
end
bw = rot90(bw);
end
part2 = sum(bw,'all')
function [i,comp] = getMatch(ID1,ID2, data)
comp = [];
i = [];
tile1 = data(ID1);
up = tile1(1,:)';
down = tile1(end,:)';
left = tile1(:,1);
right = tile1(:,end);
edges1 = [right,up,left,down];
tile2 = data(ID2);
up = tile2(1,:)';
down = tile2(end,:)';
left = tile2(:,1);
right = tile2(:,end);
edges2 = [right,up,left,down];
for i = 1:4
comp = find(all(edges1(:,i) == edges2));
if comp
return;
end
end
edges2 = flipud(edges2);
for i = 1:4
comp = find(all(edges1(:,i) == edges2));
if comp
comp = -comp;
return;
end
end
end
function data = getData(inputfile)
inp = string(strsplit(fileread(inputfile),"\n"))';
inp = strip(inp(1:end-1));
data = containers.Map('KeyType','double','ValueType','any');
for i = 1:11:length(inp)
tile = inp(i+1:i+10);
tile = strrep(tile,'#','a');
tile = ~cell2mat(isstrprop(tile,'punct'));
data(str2double(extractBetween(inp(i),' ',':'))) = tile;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment