Jump to content

SEND + MORE = MONEY: Difference between revisions

m
→‎{{header|Free Pascal}}: no more unneeded carry calc
m (→‎{{header|Free Pascal}}: no more unneeded carry calc)
Line 288:
<syntaxhighlight lang="pascal">
program SymbolToDigit;
{$IFDEF FPC}{$MODE DELPHI}{$Optimization ON,All}{$ENDIF}
{$IFDEF Windows}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;// TDatetime
const
nmax = 9;
maxLen = 57;
 
maxIDx = 41;
type
tFreeDgt = array[0..nmax+1] of Int32;
tSymbWord = String[maxLen];
tDgtWord = record
DW_DgtsIdx: array[1..maxLen] of UInt8;
DW_maxIdx: Uint8;
end;
tDgtFront = record
DW_DgtsIdx: array[1..nmax+1] of UInt8;
DW_maxIdx: Uint8;
end;
tInUse = set of 0..nmax+1;
const
{
// cSumWords : array[0..maxIDx] of tSymbWord =('SEND','MORE','MONEY');
maxIDx = 2;
cSumWords : array[0..maxIDx] of tSymbWord =('SEND','MORE','MONEY');
}
{
maxIDx = 4;
cSumWords : array[0..maxIDx] of tSymbWord =('ABRA','CADABRA','ABRA','CADABRA','HOUDINI');
}
 
//MANYOTHERS=M2A7N6Y4O1T9H5E0R8S3
maxIDx = 41;
//{
cSumWords : array[0..maxIDx] of tSymbWord =(
'SO','MANY','MORE','MEN','SEEM','TO','SAY','THAT',
Line 313 ⟶ 329:
'MOON','AS','HE','HAS','AT','THE','OTHER','TEN',
'TESTS');
 
//}
var
{$ALIGN 32}
DigitSample : tFreeDgt;,
DigitSampleSolution : tFreeDgt;
SymbInUse : array[0..10] of char;
DgtToSymb : array[0..10] of UInt8;
Words :array[0..maxIDx] of tSymbWord;
DgtWords : array[0..maxIDx] of tDgtWord;
DgtFrontWords :tDgtFront;
SymbInUseCount,gblCount : Uint32;
fullStop: boolean;
ch : char;
 
procedure OneSol(idx:int32;const DS:tFreeDgt);
var
i,symbolIdx : Int32;
begin
For i := maxlen downto 1 do
begin
symbolIdx := DgtWords[idx].DW_DgtsIdx[i];
if symbolIdx = 0 then
write(' ')
else
write(DS[symbolIdx]);
end;
writeln(cSumWords[idx]:maxLen+2);
end;
 
procedure RevString(var s:tSymbWord);
Line 342 ⟶ 374:
//CHR(ORD('A')-1) = '@' is placeholder for no Symbol
SymbToIdx : array['@'..'Z'] of byte;
FrontSymbols :tInUse;
i,j : Int32;
Begin
Line 354 ⟶ 387:
//position of highest symbol
DgtWords[i].DW_maxIdx := j;
while// jextend <by maxLen'@' doaka zero
begin
inc(j);
Words[i] := Low(SymbToIdx)+Words[i];
end;
RevString(Words[i]);
setlength(Words[i],maxlen);
For j := j+1 to maxLen do
Words[i][j] := Low(SymbToIdx);
end;
// find all symbols
 
 
for j := 1 to High(tSymbWord) do
Begin
For i := 0 to maxIDxmaxIdx do
begin
ch := Words[i][j];
Line 376 ⟶ 407:
end;
end;
dec(SymbInUseCount);
 
For i := 0 to maxIDx do
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;
 
//get index for every symbol in word
procedure OneSol(idx:int32);
For i := 0 to maxIdx do
var
with DgtWords[i] do
i,symbolIdx : Int32;
for j := 1 to High(tSymbWord) do
begin
DW_DgtsIdx[j]:= SymbToIdx[Words[i][j]];
For i := maxlen downto 1 do
begin
//find all first symbols
symbolIdx := DgtWords[idx].DW_DgtsIdx[i];
FrontSymbols := [];
if symbolIdx = 0 then
For i := 0 write('to ')maxIDx do
with DgtWords[i] do
else
writeinclude(DigitSampleFrontSymbols,DW_DgtsIdx[symbolIdxDW_maxIdx]);
 
end;
j := 1;
writeln(cSumWords[idx]:maxLen+2);
For i := 0 to nmax+1 do
if i in FrontSymbols then
Begin
DgtFrontWords.DW_DgtsIdx[j] := i;
inc(j);
end;
DgtFrontWords.DW_maxIdx := j-1;
end;
 
Line 405 ⟶ 437:
var
col,row,
sum,carrySum,carry : NativeUInt;
begin
// Nocheck for zero asin first symbolsymbols of words
with DgtFrontWords do
For row := maxIdx downto 0 do
withFor DgtWords[row]col := DW_maxIdx downto 1 do
begin
if DS[DW_DgtsIdx[DW_maxidx]] = 0 then
if DS[DW_DgtsIdx[col]] = 0 then
EXIT(false);
end;
 
carry := 0;
carrySum := 0;
For col := 1 to maxLen do
Begin
sum := carrysum MOD 10carry;
carrysumcarry := carrysum DIV 100;
carry// :=0;add one column
For row := maxIdx-1 downto 0 do
Begin
sum := sum+DS[DgtWords[row].DW_DgtsIdx[col]];
if sum > 9 then
if sum begin> 9 then
begin
carry := sum DIV 10;
sumcarry := sum -DIV 10 * carry;
sum := inc(CarrySum,Carry)sum - 10 * carry;
end;
end;
//digit of sum
if sum <> DS[DgtWords[maxIDx].DW_DgtsIdx[col]] then
EXIT(false);
end;
ifIf carrysumCarry = <>0 then
DigitSampleSolution := DS;
EXIT(false);
For row := 0 to High(DgtWords)do
OneSol(row);
EXIT(true);
end;
Line 445 ⟶ 474:
i,Col : nativeInt;
begin
if fullStop then EXIT;
EXIT;
IF row <= 10 then
begin
Line 468 ⟶ 496:
end
end;
 
var
T1,T0: TDateTime;
i,j : Uint32;
 
begin
Line 483 ⟶ 512:
NextPermute(1,DigitSample);
t1:= time;
IF maxIDx < 10 then
For i := 0 to High(DgtWords)do
OneSol(i,DigitSampleSolution);
writeln;
For i := 1 to SymbInUseCount do
begin
j := DigitSampleSolution[i];
write(SymbInUse[i],'=',j,' ');
end;
writeln;
WriteLn(gblCount,' checks ',FormatDateTime(' NN:SS.ZZZ',T1-t0),' secs');
end.</syntaxhighlight>
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
DEYNROSM 8 symbols
9567 SEND
1085 MORE
10652 MONEY
 
D=7 E=5 Y=2 N=6 R=8 O=0 S=9 M=1
2704147 checks 00:00.090 secs
2704147 checks 00:00.043 secs
OYENMTSRAH
//shorthened 'SO','MANY','MORE','MEN','SEEM','TO','SAY',
10 symbols
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
 
O=1 Y=4 E=0 N=6 M=2 T=9 S=3 R=8 A=7 H=5
496179 checks 00:00.061 secs
496179 checks 00:00.013 secs</pre>
</pre>
 
=={{header|Phix}}==
132

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.