Skip to content

Instantly share code, notes, and snippets.

@awjuliani
Created April 5, 2016 22:28
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 awjuliani/52fb14bab6b7b62b191d5760d30145ae to your computer and use it in GitHub Desktop.
Save awjuliani/52fb14bab6b7b62b191d5760d30145ae to your computer and use it in GitHub Desktop.
Matlab script to solve basic cryptarithmetic problems.
clear ; close all; clc
%Get input for three terms
prompt = 'First three letter term? ';
x1 = input(prompt, 's');
prompt = 'Second three letter term? ';
x2 = input(prompt, 's');
prompt = 'Four letter answer? ';
y = input(prompt, 's');
x1 = char(x1);
x2 = char(x2);
y = char(y);
%Get unique characters from input
all = strcat(x1,x2,y);
uniqueVals = unique(all);
for i = 1:length(all)
for ix=1:length(uniqueVals)
if all(i)== uniqueVals(ix)
allIndex(i) = ix;
end
end
end
%%Establish all constraints%%
%Establishes AllDif constraint
searchSpace = perms(0:9);
searchSpace = searchSpace(:,1:length(uniqueVals));
searchSpace = unique(searchSpace,'rows');
%Establishing no leading 0 constraint
%Additionally, the problem requires the first variable in the solution term
%to always be 1, as it cannot be 0 by definition, and no set of the numbers
%<999 can be summed to result in a number >=2000.
searchSpace(searchSpace(:,allIndex(7))~=1,:) = [];
searchSpace(searchSpace(:,allIndex(1))==0,:) = [];
searchSpace(searchSpace(:,allIndex(4))==0,:) = [];
%Establish additive constraints by digits
searchSpace((searchSpace(:,allIndex(4)) + searchSpace(:,allIndex(1)))<9,:) = [];
searchSpace(((searchSpace(:,allIndex(4)) + searchSpace(:,allIndex(1)) - 10) ~= searchSpace(:,allIndex(8)) & ((searchSpace(:,allIndex(4)) + searchSpace(:,allIndex(1)) - 9) ~= searchSpace(:,allIndex(8)))),:) = [];
searchSpace(((searchSpace(:,allIndex(3)) + searchSpace(:,allIndex(6)) - 10) ~= searchSpace(:,allIndex(10)) & ((searchSpace(:,allIndex(3)) + searchSpace(:,allIndex(6))) ~= searchSpace(:,allIndex(10)))),:) = [];
searchSpace(((searchSpace(:,allIndex(2)) + searchSpace(:,allIndex(5)) - 10) ~= searchSpace(:,allIndex(9)) & ((searchSpace(:,allIndex(2)) + searchSpace(:,allIndex(5))) ~= searchSpace(:,allIndex(9))) & ((searchSpace(:,allIndex(2)) + searchSpace(:,allIndex(5))) + 1 ~= searchSpace(:,allIndex(9))) & ((searchSpace(:,allIndex(2)) + searchSpace(:,allIndex(5))) -9 ~= searchSpace(:,allIndex(9)))),:) = [];
%Search for possible solutions that meet overall additive constraint
numSolutions = 0;
for i=1:size(searchSpace,1)
v=searchSpace(i,:);
exp1=v(allIndex(1))*100 + v(allIndex(2))*10 + v(allIndex(3));
exp2=v(allIndex(4))*100 + v(allIndex(5))*10 + v(allIndex(6));
exp3=v(allIndex(7))*1000 + v(allIndex(8))*100 + v(allIndex(9))*10 + v(allIndex(10));
if exp1+exp2==exp3
numSolutions=numSolutions+1;
report(numSolutions,:)=v(1:length(uniqueVals));
end
end
%Output search results
if numSolutions==0
prompt = 'No Solution :-('
end
if numSolutions>0
report = unique(report,'rows');
display(length(report),'Number of valid solutions found')
display(uniqueVals,'Unique characters')
display(report,'Character assignments')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment