|
| 1 | +unit Alphametics; |
| 2 | + |
| 3 | +{$mode ObjFPC}{$H+} |
| 4 | + |
| 5 | +interface |
| 6 | + |
| 7 | +function solve(const puzzle : string) : string; |
| 8 | + |
| 9 | +implementation |
| 10 | + |
| 11 | +uses SysUtils; |
| 12 | + |
| 13 | +type |
| 14 | + ESolved = class(Exception); |
| 15 | + |
| 16 | +function solve(const puzzle : string) : string; |
| 17 | +type |
| 18 | + TLetter = record |
| 19 | + letter : char; |
| 20 | + rank : integer; { lowest column where letter appears } |
| 21 | + weight : array of integer; |
| 22 | + leading : boolean; { cannot be assigned 0 } |
| 23 | + digit : integer; |
| 24 | + end; |
| 25 | + TLetters = array of TLetter; |
| 26 | +var |
| 27 | + letters : TLetters; |
| 28 | + nColumns : integer; |
| 29 | + |
| 30 | + function ParsePuzzle : TLetters; |
| 31 | + var |
| 32 | + table : array[0..25] of TLetter; |
| 33 | + |
| 34 | + procedure Scan; |
| 35 | + var |
| 36 | + i, c, sign, place, prev : integer; |
| 37 | + begin |
| 38 | + for i := 0 to 25 do begin |
| 39 | + table[i].letter := chr(ord('A') + i); |
| 40 | + table[i].rank := High(integer); |
| 41 | + table[i].weight := nil; |
| 42 | + table[i].leading := false; |
| 43 | + end; |
| 44 | + |
| 45 | + sign := -1; |
| 46 | + place := 0; |
| 47 | + prev := 0; |
| 48 | + for i := length(puzzle) downto 1 do begin |
| 49 | + c := ord(puzzle[i]) - ord('A'); |
| 50 | + if (c >= 0) and (c < 26) then |
| 51 | + begin |
| 52 | + if place < table[c].rank then |
| 53 | + table[c].rank := place; |
| 54 | + |
| 55 | + if length(table[c].weight) <= place then |
| 56 | + SetLength(table[c].weight, place + 1); |
| 57 | + |
| 58 | + inc(table[c].weight[place], sign); |
| 59 | + prev := c; |
| 60 | + inc(place); |
| 61 | + end |
| 62 | + else |
| 63 | + begin |
| 64 | + if puzzle[i] = '=' then |
| 65 | + sign := 1; |
| 66 | + |
| 67 | + if place > 0 then |
| 68 | + table[prev].leading := true; |
| 69 | + |
| 70 | + place := 0; |
| 71 | + end; |
| 72 | + end; |
| 73 | + if place > 0 then |
| 74 | + table[prev].leading := true; |
| 75 | + end; |
| 76 | + |
| 77 | + function Compact : TLetters; |
| 78 | + var |
| 79 | + i, j : integer; |
| 80 | + begin |
| 81 | + nColumns := 0; |
| 82 | + result := nil; |
| 83 | + for i := 0 to 25 do begin |
| 84 | + if table[i].rank = High(integer) then |
| 85 | + continue; |
| 86 | + |
| 87 | + if length(table[i].weight) > nColumns then |
| 88 | + nColumns := length(table[i].weight); |
| 89 | + |
| 90 | + j := length(result); |
| 91 | + SetLength(result, j + 1); |
| 92 | + while (j > 0) and (result[j - 1].rank > table[i].rank) do |
| 93 | + begin |
| 94 | + result[j] := result[j - 1]; |
| 95 | + dec(j); |
| 96 | + end; |
| 97 | + result[j] := table[i]; |
| 98 | + end; |
| 99 | + end; |
| 100 | + |
| 101 | + begin |
| 102 | + Scan; |
| 103 | + result := Compact; |
| 104 | + end; |
| 105 | + |
| 106 | + procedure Search(index, claimed, col, carry : integer); |
| 107 | + var |
| 108 | + digit, k, colSum : integer; |
| 109 | + begin |
| 110 | + if (index = length(letters)) or (letters[index].rank > col) then |
| 111 | + begin |
| 112 | + colSum := carry; |
| 113 | + for k := 0 to index - 1 do |
| 114 | + if col < length(letters[k].weight) then |
| 115 | + inc(colSum, letters[k].weight[col] * letters[k].digit); |
| 116 | + |
| 117 | + if colSum mod 10 = 0 then |
| 118 | + begin |
| 119 | + if col + 1 < nColumns then |
| 120 | + Search(index, claimed, col + 1, colSum div 10) |
| 121 | + else if colSum div 10 = 0 then |
| 122 | + raise ESolved.Create(''); |
| 123 | + end; |
| 124 | + end |
| 125 | + else |
| 126 | + for digit := ord(letters[index].leading) to 9 do |
| 127 | + begin |
| 128 | + if claimed and (1 shl digit) <> 0 then |
| 129 | + continue; |
| 130 | + |
| 131 | + letters[index].digit := digit; |
| 132 | + Search(index + 1, claimed or (1 shl digit), col, carry); |
| 133 | + end; |
| 134 | + end; |
| 135 | + |
| 136 | + function BuildResult : string; |
| 137 | + var |
| 138 | + i : integer; |
| 139 | + table : array['A'..'Z'] of char; |
| 140 | + ch : char; |
| 141 | + begin |
| 142 | + for i := 0 to length(letters) - 1 do |
| 143 | + table[letters[i].letter] := chr(ord('0') + letters[i].digit); |
| 144 | + |
| 145 | + result := ''; |
| 146 | + for ch in puzzle do |
| 147 | + if (ch >= 'A') and (ch <= 'Z') then |
| 148 | + result += table[ch] |
| 149 | + else |
| 150 | + result += ch; |
| 151 | + end; |
| 152 | + |
| 153 | +begin |
| 154 | + letters := ParsePuzzle; |
| 155 | + try |
| 156 | + Search(0, 0, 0, 0); |
| 157 | + raise Exception.Create('no solution'); |
| 158 | + except |
| 159 | + on ESolved do result := BuildResult; |
| 160 | + end; |
| 161 | +end; |
| 162 | + |
| 163 | +end. |
0 commit comments