SEND + MORE = MONEY: Difference between revisions
Content added Content deleted
(Added Go) |
(→{{header|Julia}}: append Free Pascal Verion with brute force.) |
||
Line 283: | Line 283: | ||
end |
end |
||
</syntaxhighlight>{{out}} 9567 + 1085 == 10652 |
</syntaxhighlight>{{out}} 9567 + 1085 == 10652 |
||
=={{header|Pascal}}== |
|||
==={{header|Free Pascal}}=== |
|||
simple brute force. Permutation stolen by nQueens. |
|||
<syntaxhighlight lang="pascal"> |
|||
program SymbolToDigit; |
|||
uses |
|||
sysutils;// TDatetime |
|||
const |
|||
nmax = 9; |
|||
type |
|||
tFreeDgt = array[0..nmax] of Int32; |
|||
tSymbWord = String[5]; |
|||
tDgtWord = array[0..4] of byte; |
|||
const |
|||
cSumWords : array[0..2] of tSymbWord =('SEND','MORE','MONEY'); |
|||
var |
|||
{$ALIGN 32} |
|||
DigitSample : tFreeDgt; |
|||
SymbInUse : array[0..9] of char; |
|||
SymbToIdx : array['A'..'Z'] of byte; |
|||
DgtToSymb : array[0..9] of byte; |
|||
Words :array[0..2] of tSymbWord; |
|||
DgtWords : array[0..2] of tDgtWord; |
|||
T1,T0: TDateTime; |
|||
i,j,SymbInUseCount,gblCount,DgtCnt : Uint32; |
|||
fullStop: boolean; |
|||
ch : char; |
|||
procedure RevString(var s:tSymbWord); |
|||
var |
|||
i,j: NativeInt; |
|||
begin |
|||
i := 1; |
|||
j := Length(s); |
|||
while j>i do |
|||
begin |
|||
ch:= s[i];s[i]:= s[j];s[j] := ch; |
|||
inc(i);dec(j); |
|||
end; |
|||
end; |
|||
procedure GetSymbols; |
|||
Begin |
|||
fillchar(SymbToIdx,SizeOf(SymbToIdx),#255); |
|||
For i := 0 to 2 do |
|||
begin |
|||
Words[i] := cSumWords[i]; |
|||
RevString(Words[i]); |
|||
end; |
|||
for j := 1 to High(tSymbWord) do |
|||
Begin |
|||
For i := 0 to 2 do |
|||
begin |
|||
if length(Words[i])<j then |
|||
continue; |
|||
ch := Words[i][j]; |
|||
if SymbToIdx[ch] = 255 then |
|||
begin |
|||
SymbToIdx[ch] := SymbInUseCount; |
|||
SymbInUse[SymbInUseCount] := ch; |
|||
inc(SymbInUseCount); |
|||
end; |
|||
end; |
|||
end; |
|||
For i := 0 to 2 do |
|||
for j := 0 to Length(Words[i])-1 do |
|||
DgtWords[i][j]:= SymbToIdx[Words[i][j+1]]; |
|||
writeln(SymbInUseCount,' symbols'); |
|||
end; |
|||
procedure OneSol(idx:int32); |
|||
var |
|||
i : Int32; |
|||
begin |
|||
i := High(tSymbWord); |
|||
while i>length(Words[idx]) do |
|||
Begin |
|||
write(' '); |
|||
dec(i); |
|||
end; |
|||
dec(i); |
|||
while i >= 0 do |
|||
begin |
|||
write(DigitSample[DgtWords[idx][i]]); |
|||
dec(i); |
|||
end; |
|||
RevString(Words[idx]); |
|||
writeln(words[idx]:7); |
|||
end; |
|||
function AddWords(const DS:tFreeDgt):boolean; |
|||
var |
|||
dgtPos,i, |
|||
sum,carry : NativeUInt; |
|||
begin |
|||
carry := 0; |
|||
For dgtPos := 0 to 3 do |
|||
Begin |
|||
sum := carry; |
|||
carry :=0; |
|||
For i := 0 to High(DgtWords)-1 do |
|||
Begin |
|||
sum := sum+DS[DgtWords[i][dgtPos]]; |
|||
end; |
|||
if sum > 9 then |
|||
begin |
|||
carry := sum DIV 10; |
|||
sum := sum - 10 * carry; |
|||
end; |
|||
if sum <> DS[DgtWords[High(DgtWords)][dgtPos]] then |
|||
EXIT(false); |
|||
end; |
|||
if carry= 0 then EXIT(false); |
|||
if DS[DgtWords[High(DgtWords)][Length(Words[High(DgtWords)])-1]]<>carry then |
|||
EXIT(false); |
|||
For i := 0 to High(DgtWords)do |
|||
OneSol(i); |
|||
EXIT(true); |
|||
end; |
|||
procedure NextPermute(Row:nativeInt;var DS:tFreeDgt); |
|||
var |
|||
i,Col : nativeInt; |
|||
begin |
|||
if fullStop then |
|||
EXIT; |
|||
IF row <= 9 then |
|||
begin |
|||
NextPermute(Row+1,DS); |
|||
For i := row+1 to 9 do |
|||
begin |
|||
//swap |
|||
Col := DS[i]; |
|||
DS[i] := DS[Row]; |
|||
DS[Row] := Col; |
|||
NextPermute(Row+1,DS); |
|||
//Undo swap |
|||
DS[Row] := DS[i]; |
|||
DS[i] := Col; |
|||
end |
|||
end |
|||
else |
|||
begin |
|||
fullStop := AddWords(DS); |
|||
inc(gblCount); |
|||
end |
|||
end; |
|||
begin |
|||
For i := 0 to nmax do |
|||
DigitSample[i] := i; |
|||
GetSymbols; |
|||
t0 := time; |
|||
gblCount := 0; |
|||
DgtCnt := 9; |
|||
fullStop := false; |
|||
NextPermute(0,DigitSample); |
|||
t1:= time; |
|||
writeln; |
|||
WriteLn(gblCount,' checks ',FormatDateTime(' NN:SS.ZZZ',T1-t0),' secs'); |
|||
end.</syntaxhighlight> |
|||
{{out|@TIO.RUN}} |
|||
<pre> |
|||
8 symbols |
|||
9567 SEND |
|||
1085 MORE |
|||
10652 MONEY |
|||
2704147 checks 00:00.090 secs</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |