SEND + MORE = MONEY: Difference between revisions

m
Line 292:
const
nmax = 9;
maxLen = 5;
maxIDx = 41;
type
tFreeDgt = array[0..nmax+1] of Int32;
tSymbWord = String[5maxLen];
tDgtWord = array[0..4] of byte;record
DW_DgtsIdx: array[1..maxLen] of UInt8;
DW_maxIdx: Uint8;
end;
const
// cSumWords : array[0..2maxIDx] of tSymbWord =('SEND','MORE','MONEY');
 
//MANYOTHERS=M2A7N6Y4O1T9H5E0R8S3
//{
cSumWords : array[0..maxIDx] of tSymbWord =(
'SO','MANY','MORE','MEN','SEEM','TO','SAY','THAT',
'THEY','MAY','SOON','TRY','TO','STAY','AT','HOME',
'SO','AS','TO','SEE','OR','HEAR','THE','SAME','ONE',
'MAN','TRY','TO','MEET','THE','TEAM','ON','THE',
'MOON','AS','HE','HAS','AT','THE','OTHER','TEN',
'TESTS');
//}
var
{$ALIGN 32}
DigitSample : tFreeDgt;
SymbInUse : array[0..910] of char;
SymbToIdxDgtToSymb : array['A'0..'Z'10] of byteUInt8;
DgtToSymbWords : array[0..9maxIDx] of bytetSymbWord;
WordsDgtWords : array[0..2maxIDx] of tSymbWordtDgtWord;
SymbInUseCount,gblCount : Uint32;
DgtWords : array[0..2] of tDgtWord;
T1,T0: TDateTime;
i,j,SymbInUseCount,gblCount,DgtCnt : Uint32;
fullStop: boolean;
ch : char;
Line 325 ⟶ 339:
 
procedure GetSymbols;
var
//CHR(ORD('A')-1) = '@' is placeholder for no Symbol
SymbToIdx : array['@'..'Z'] of byte;
i,j : Int32;
Begin
fillchar(SymbToIdx,SizeOf(SymbToIdx),#255);
For iSymbToIdx['@'] := 0 to 2 do;
SymbInUseCount := 1;//['@'] is always zero
 
For i := 0 to maxIDx do
begin
Words[i] := cSumWords[i];
j := length(Words[i]);
//position of highest symbol
DgtWords[i].DW_maxIdx := j;
while j < maxLen do
begin
inc(j);
Words[i] := Low(SymbToIdx)+Words[i];
end;
RevString(Words[i]);
end;
 
 
for j := 1 to High(tSymbWord) do
Begin
For i := 0 to 2maxIDx do
begin
if length(Words[i])<j then
continue;
ch := Words[i][j];
 
if SymbToIdx[ch] = 255 then
begin
Line 350 ⟶ 377:
end;
 
For i := 0 to 2maxIDx do
begin
for j := 0 to Length(Words[i])-1 do
for j := 1 to maxlen do
DgtWords[i][j]:= SymbToIdx[Words[i][j+1]];
DgtWords[i].DW_DgtsIdx[j]:= SymbToIdx[Words[i][j]];
 
end;
writeln(SymbInUseCount,' symbols');
For i := 1 to SymbInUseCount do
write(SymbInUse[i]);
writeln(SymbInUseCount-1:4,' symbols');
end;
 
procedure OneSol(idx:int32);
var
i,symbolIdx : Int32;
begin
For i := High(tSymbWord);maxlen downto 1 do
while i>length(Words[idx]) do
Begin
write(' ');
dec(i);
end;
dec(i);
while i >= 0 do
begin
write(DigitSample[symbolIdx := DgtWords[idx].DW_DgtsIdx[i]]);
if symbolIdx = 0 then
dec(i);
write(' ')
else
write(DigitSample[symbolIdx]);
end;
RevStringwriteln(WordscSumWords[idx]:maxLen+2);
writeln(words[idx]:7);
end;
 
function AddWords(const DS:tFreeDgt):boolean;
var
dgtPoscol,irow,
sum,carrySum,carry : NativeUInt;
begin
// No zero as first symbol
For row := maxIdx downto 0 do
with DgtWords[row] do
if DS[DW_DgtsIdx[DW_maxidx]] = 0 then
EXIT(false);
 
carry := 0;
For dgtPoscarrySum := 0 to 3 do;
For col := 1 to maxLen do
Begin
sum := carrycarrysum MOD 10;
carrysum := carrysum DIV 10;
carry :=0;
For irow := 0 to High(DgtWords)maxIdx-1 downto 0 do
Begin
sum := sum+DS[DgtWords[irow].DW_DgtsIdx[dgtPoscol]];
if sum > 9 then
end;
if sum > 9 thenbegin
carry := sum DIV 10;
begin
carry sum := sum DIV- 10 * carry;
sum := sum - 10 * carryinc(CarrySum,Carry);
end;
end;
//digit of sum
if sum <> DS[DgtWords[High(DgtWords)][dgtPos]] then
if sum <> DS[DgtWords[maxIDx].DW_DgtsIdx[col]] then
EXIT(false);
end;
if carry=carrysum <>0 then EXIT(false);
if DS[DgtWords[High(DgtWords)][Length(Words[High(DgtWords)])-1]]<>carry then
EXIT(false);
For irow := 0 to High(DgtWords)do
OneSol(irow);
EXIT(true);
end;
Line 413 ⟶ 447:
if fullStop then
EXIT;
IF row <= 910 then
begin
NextPermute(Row+1,DS);
For i := row+1 to 910 do
begin
//swap
Line 430 ⟶ 464:
else
begin
fullStop := AddWords(DS);
inc(gblCount);
end
end;
var
T1,T0: TDateTime;
i : Uint32;
 
begin
DigitSample[0] := 0;
For i := 0 to nmax do
DigitSample[i+1] := i;
GetSymbols;
 
t0 := time;
gblCount := 0;
DgtCnt := 9;
fullStop := false;
NextPermute(01,DigitSample);
t1:= time;
writeln;
WriteLn(gblCount,' checks ',FormatDateTime(' NN:SS.ZZZ',T1-t0),' secs');
end.
end.</syntaxhighlight>
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Line 456 ⟶ 494:
10652 MONEY
 
2704147 checks 00:00.090 secs</pre>
OYENMTSRAH
10 symbols
31 SO
2764 MANY
2180 MORE
206 MEN
3002 SEEM
91 TO
374 SAY
9579 THAT
9504 THEY
274 MAY
3116 SOON
984 TRY
91 TO
3974 STAY
79 AT
5120 HOME
31 SO
73 AS
91 TO
300 SEE
18 OR
5078 HEAR
950 THE
3720 SAME
160 ONE
276 MAN
984 TRY
91 TO
2009 MEET
950 THE
9072 TEAM
16 ON
950 THE
2116 MOON
73 AS
50 HE
573 HAS
79 AT
950 THE
19508 OTHER
906 TEN
90393 TESTS
 
496179 checks 00:00.061 secs
</pre>
 
=={{header|Phix}}==
132

edits