SEND + MORE = MONEY: Difference between revisions
m
→{{header|Free Pascal}}: no more unneeded carry calc
m (→{{header|Free Pascal}}: 41 Words) |
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 =
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
{
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
DigitSampleSolution : tFreeDgt;
SymbInUse : array[0..10] of char;
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;
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
begin
ch := Words[i][j];
Line 376 ⟶ 407:
end;
end;
dec(SymbInUseCount);
For i := 1 to SymbInUseCount do
write(SymbInUse[i]);
writeln(SymbInUseCount
//get index for every symbol in word
For i := 0 to maxIdx do
with DgtWords[i] do
for j := 1 to High(tSymbWord) do
DW_DgtsIdx[j]:= SymbToIdx[Words[i][j]];
//find all first symbols
FrontSymbols := [];
For i := 0
with DgtWords[i] do
j := 1;
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
begin
//
with DgtFrontWords do
begin
if DS[DW_DgtsIdx[col]] = 0 then
EXIT(false);
end;
carry := 0;
For col := 1 to maxLen do
Begin
sum :=
For row := maxIdx-1 downto 0 do
sum := sum+DS[DgtWords[row].DW_DgtsIdx[col]];
if sum
begin
sum :=
//digit of sum
if sum <> DS[DgtWords[maxIDx].DW_DgtsIdx[col]] then
EXIT(false);
end;
DigitSampleSolution := DS;
EXIT(true);
end;
Line 445 ⟶ 474:
i,Col : nativeInt;
begin
if fullStop then 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>
{{out|@TIO.RUN}}
<pre>
DEYNROSM 8 symbols
10652 MONEY
D=7 E=5 Y=2 N=6 R=8 O=0 S=9 M=1
2704147 checks 00:00.043 secs
//shorthened 'SO','MANY','MORE','MEN','SEEM','TO','SAY',
OYENMTSRAH 10 symbols
O=1 Y=4 E=0 N=6 M=2 T=9 S=3 R=8 A=7 H=5
496179 checks 00:00.013 secs</pre>
=={{header|Phix}}==
|