usa + ussr ------ peaceThe goal is to find the digits that can be substituted for the letters to produce a correct addition statement. The answer for this puzzle is given below. Wherever an 'a' appears in the word form, the digit 2 is substituted in the number form. Wherever an 's' appears in the word form, the digit 3 is used in the number form. The complete mapping of characters to digits appears to the right.
932 usa a c e p r s u + 9338 + ussr 2 7 0 1 8 3 9 ------ ------ 10270 peaceHow about the much simpler example below?
i + bb ---- illHere are a bunch of guesses -
b i l 1 b i l 1 b i l 9 b i l 0 b i l 1 0 1 2 + 00 0 1 9 + 00 1 9 0 + 11 9 0 1 + 99 9 1 0 + 99 ---- ---- ---- ---- ---- 122 199 900 011 100The last guess above is the answer.
How about identifying all possible guesses to this problem, and then testing each guess in turn, until the answer is found?
So – is the number of all possible guesses finite? Is it knowable? Answer
There are 10 digits to choose from (0 through 9), and there are 3 letters in this puzzle ('b', 'i', 'l') that need to be assigned a number. How could the problem be stated in order to lead us to a formula that gives the total number of guesses possible? Answer
If we were to enumerate all combinations of 10 digits taken 3 at a time, we would find there are 120. If we were to then list all possible orderings (i.e. permutations) of a 3 digit number, we would find there are 6. The total possible guesses is 120 * 6 = 720 (all permutations of all combinations).
012 021 102 120 201 210 013 031 103 130 301 310 014 041 104 140 401 410 015 051 105 150 501 510 016 061 106 160 601 610 017 071 107 170 701 710 018 081 108 180 801 810 019 091 109 190 901 910 023 032 203 230 302 320 024 042 204 240 402 420 025 052 205 250 502 520 026 062 206 260 602 620 027 072 207 270 702 720 028 082 208 280 802 820 029 092 209 290 902 920 034 043 304 340 403 430 035 053 305 350 503 530 036 063 306 360 603 630 037 073 307 370 703 730 038 083 308 380 803 830 039 093 309 390 903 930 045 054 405 450 504 540 046 064 406 460 604 640 047 074 407 470 704 740 048 084 408 480 804 840 049 094 409 490 904 940 056 065 506 560 605 650 057 075 507 570 705 750 058 085 508 580 805 850 059 095 509 590 905 950 067 076 607 670 706 760 068 086 608 680 806 860 069 096 609 690 906 960 078 087 708 780 807 870 079 097 709 790 907 970 089 098 809 890 908 980 123 132 213 231 312 321 124 142 214 241 412 421 125 152 215 251 512 521 126 162 216 261 612 621 | 127 172 217 271 712 721 128 182 218 281 812 821 129 192 219 291 912 921 134 143 314 341 413 431 135 153 315 351 513 531 136 163 316 361 613 631 137 173 317 371 713 731 138 183 318 381 813 831 139 193 319 391 913 931 145 154 415 451 514 541 146 164 416 461 614 641 147 174 417 471 714 741 148 184 418 481 814 841 149 194 419 491 914 941 156 165 516 561 615 651 157 175 517 571 715 751 158 185 518 581 815 851 159 195 519 591 915 951 167 176 617 671 716 761 168 186 618 681 816 861 169 196 619 691 916 961 178 187 718 781 817 871 179 197 719 791 917 971 189 198 819 891 918 981 234 243 324 342 423 432 235 253 325 352 523 532 236 263 326 362 623 632 237 273 327 372 723 732 238 283 328 382 823 832 239 293 329 392 923 932 245 254 425 452 524 542 246 264 426 462 624 642 247 274 427 472 724 742 248 284 428 482 824 842 249 294 429 492 924 942 256 265 526 562 625 652 257 275 527 572 725 752 258 285 528 582 825 852 259 295 529 592 925 952 267 276 627 672 726 762 | 268 286 628 682 826 862 269 296 629 692 926 962 278 287 728 782 827 872 279 297 729 792 927 972 289 298 829 892 928 982 345 354 435 453 534 543 346 364 436 463 634 643 347 374 437 473 734 743 348 384 438 483 834 843 349 394 439 493 934 943 356 365 536 563 635 653 357 375 537 573 735 753 358 385 538 583 835 853 359 395 539 593 935 953 367 376 637 673 736 763 368 386 638 683 836 863 369 396 639 693 936 963 378 387 738 783 837 873 379 397 739 793 937 973 389 398 839 893 938 983 456 465 546 564 645 654 457 475 547 574 745 754 458 485 548 584 845 854 459 495 549 594 945 954 467 476 647 674 746 764 468 486 648 684 846 864 469 496 649 694 946 964 478 487 748 784 847 874 479 497 749 794 947 974 489 498 849 894 948 984 567 576 657 675 756 765 568 586 658 685 856 865 569 596 659 695 956 965 578 587 758 785 857 875 579 597 759 795 957 975 589 598 859 895 958 985 678 687 768 786 867 876 679 697 769 796 967 976 689 698 869 896 968 986 789 798 879 897 978 987 |
public class Alphametics { public static void main( String args[] ) { int[] guess = new int[3]; int count = 0; // All combinations of 10 digits taken 3 at a time Combinations combos = new Combinations( 10, 3 ); while (combos.next( guess )) { Permutations permus = new Permutations( guess ); while (permus.next( guess )) { count++; System.out.print( "" + guess[0] + guess[1] + guess[2] + " " ); } System.out.println(); } System.out.println( "\ncount is " + count ); } }The source for the
Combinations
and Permutations
classes can be found here.Now that we have a framework for generating all possible guesses, how do you want to proceed?
..... [Final Jeopardy music goes here] .....
Somehow, we need to take each guess, and evaluate it against the statement
"i + bb = ill
". Let's decide on the convention that
guess[0] represents the digit to be assigned to the character 'b' guess[1] represents the digit to be assigned to the character 'i' guess[2] represents the digit to be assigned to the character 'l'To convert the word "
ill
" into its corresponding
3-digit number, we need a formula like -
guess[1] * 100 + guess[2] * 10 + guess[2]To convert "
bb
", we need the formula
guess[0] * 10 + guess[0]We can now test each possible guess with something like -
if (guess[1]) + (guess[0]*10 + guess[0]) = (guess[1]*100 + guess[2]*10 + guess[2]) then BINGO!Adapting the previous program might produce something like -
while (combos.next( guess )) { Permutations permus = new Permutations( guess ); while (permus.next( guess )) { count++; if (guess[1] + guess[0]*10+guess[0] == guess[1]*100+guess[2]*10+guess[2]) System.out.println( guess[1] + " + " + guess[0] + guess[0] + " = " + guess[1] + guess[2] + guess[2] ); } } System.out.println( "count is " + count );And the output is -
1 + 99 = 100 count is 720
base + ball ------ gamesWhat is the first step? Answer
There are 7 unique characters that need to be decoded -
character a b e g l m s position 0 1 2 3 4 5 6What needs to be changed in the program to solve the new puzzle? Answer
Using the previous strategy, the three words of this puzzle can be represented as -
base guess[1]*1000 + guess[0]*100 + guess[6]*10 + guess[2] ball guess[1]*1000 + guess[0]*100 + guess[4]*10 + guess[4] games guess[3]*10000 + guess[0]*1000 + guess[5]*100 + guess[2]*10 + guess[6]The new program looks remarkably similar -
int[] guess = new int[7]; Combinations combos = new Combinations( 10, 7 ); while (combos.next( guess )) { Permutations permus = new Permutations( guess ); while (permus.next( guess )) { count++; if ((guess[1]*1000 + guess[0]*100 + guess[6]*10 + guess[2]) + (guess[1]*1000 + guess[0]*100 + guess[4]*10 + guess[4]) == (guess[3]*10000 + guess[0]*1000 + guess[5]*100 + guess[2]*10 + guess[6])) { System.out.println( "\n base " + guess[1] + guess[0] + guess[6] + guess[2] ); System.out.println( " ball " + guess[1] + guess[0] + guess[4] + guess[4] ); System.out.println( "games " +guess[3] +guess[0] +guess[5] +guess[2] +guess[6] ); } } } System.out.println( "\ncount is " + count );And the output is -
base 2461 ball 2455 games 04916 base 2483 ball 2455 games 04938 base 7483 ball 7455 games 14938 count is 604800The first two solutions are less than desirable.   The program could be upgraded to filter out these "false hits".   Or - the program's User Guide could simply describe this as a feature.
> java Alphametics usa ussr peace a c e p r s u 2 7 0 1 8 3 9 usa 932 ussr 9338 peace 10270 count is 604800 > java Alphametics base ball games a b e g l m s 4 7 3 1 5 9 8 base 7483 ball 7455 games 14938 count is 604800 > java Alphametics one nine twenty fifty eighty e f g h i n o t w y 4 7 0 1 5 8 9 3 6 2 one 984 nine 8584 twenty 364832 fifty 75732 eighty 450132 count is 3628800 > java Alphametics this isa great time waster a e g h i m r s t w 0 4 9 6 2 3 7 8 5 1 this 5628 isa 280 great 97405 time 5234 waster 108547 count is 3628800How would you go about creating this program that can program itself?
What steps are necessary and sufficient?
How about these steps?