SEND + MORE = MONEY: Difference between revisions
m
→{{header|Free Pascal}}: 41 Words
m (→{{header|Free Pascal}}: 41 Words) |
|||
Line 292:
const
nmax = 9;
maxLen = 5;
maxIDx = 41;
type
tFreeDgt = array[0..nmax+1] of Int32;
tSymbWord = String[
tDgtWord =
DW_DgtsIdx: array[1..maxLen] of UInt8;
DW_maxIdx: Uint8;
end;
const
// cSumWords : array[0..
//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..
SymbInUseCount,gblCount : 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);
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
begin
ch := Words[i][j];
if SymbToIdx[ch] = 255 then
begin
Line 350 ⟶ 377:
end;
For i := 0 to
begin
for j := 1 to maxlen do
DgtWords[i].DW_DgtsIdx[j]:= SymbToIdx[Words[i][j]];
end;
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 :=
begin
if symbolIdx = 0 then
write(' ')
else
write(DigitSample[symbolIdx]);
end;
end;
function AddWords(const DS:tFreeDgt):boolean;
var
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 col := 1 to maxLen do
Begin
sum :=
carrysum := carrysum DIV 10;
carry :=0;
For
Begin
sum := sum+DS[DgtWords[
if sum > 9 then
carry := sum DIV 10;
end;
end;
//digit of sum
if sum <> DS[DgtWords[maxIDx].DW_DgtsIdx[col]] then
EXIT(false);
end;
if
EXIT(false);
For
OneSol(
EXIT(true);
end;
Line 413 ⟶ 447:
if fullStop then
EXIT;
IF row <=
begin
NextPermute(Row+1,DS);
For i := row+1 to
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;
fullStop := false;
NextPermute(
t1:= time;
writeln;
WriteLn(gblCount,' checks ',FormatDateTime(' NN:SS.ZZZ',T1-t0),' secs');
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Line 456 ⟶ 494:
10652 MONEY
2704147 checks 00:00.090 secs
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}}==
|