Version 0 of Brute force with velvet gloves

Updated 2004-02-24 18:43:02

This page was spun off from Solving cryptarithms, numeric puzzles in which digits have been replaced by letters


TFW (Feb, 23 2004) - OK, Here is another one, we must solve the equation below where ABCDE*4=EDCBA, nothing fancy but the brute force methods solved it before your finger leaves the keyboard.

 #----------------------------------------------------------------------------
 # Solve the problem
 # ABCDE
 #    X4
 #------
 # EDCBA
 #----------------------------------------------------------------------------
 proc ABCDE {args} {
    set nums {1 2 3 4 5 6 7 8 9}
    set counter 0
    foreach a $nums {
       foreach b $nums {
          foreach c $nums {
             foreach d $nums {
                foreach e $nums {
                   set n1 [expr {"$a$b$c$d$e"*4}]
                   set n2 "$e$d$c$b$a"
                   incr counter
                   if {$n1==$n2} {
                      puts "We solved it! $a$b$c$d$e * 4 = $n2 at $counter tries"
                      return
                   }
                }
             }
          }
       }
    }
    puts "Not Solved"
 }

Here is another one, we have $1 in coins but only one can be a nickel. So we know that 19 coins must add to 95 cents using only pennies, dimes, quarters and half-dollars. Again, the simple brute force method yields an answer before your finger leaves the keyboard

 #----------------------------------------------------------------------------
 # Solve the problem
 # 20 coins = $1.00, only one is a nickel
 # so we have 19 coins =95 cents
 #----------------------------------------------------------------------------
 proc solve2 {args} {
    set nums {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
    set counter 0
    foreach penny $nums {
       foreach dime $nums {
          foreach quarter $nums {
             foreach halfdollar $nums {
                if {($penny+$dime+$quarter+$halfdollar)==19} {
                   set value [expr {$penny*1+$dime*10+$quarter*25+$halfdollar*50}]
                   incr counter
                   if {$value==95} {
                      puts "We solved it! penny=$penny dime=$dime quarter=$quarter halfdollar=$halfdollar at $counter tries"
                      return
                   }
                }
             }
          }
       }
    }
    puts "Not Solved"
 }

RS experiments with the following "General Problem Solver" (for small values of General), which, with heavy metaprogramming, builds up a nest of foreachs suiting the problem, quick kills (with continue) to force unique values for the variables, and returns the first solution found, or else an empty string:

 proc solve {problem {domain0 {0 1 2 3 4 5 6 7 8 9}}} {
    set vars [lsort -u [split [regsub -all {[^A-Z]} $problem ""] ""]]
    set map {= ==}
    set outers {}
    set initials [regexp -all -inline {[^A-Z]([A-Z])} /$problem]
    set pos [lsearch $domain0 0]
    set domain1 [lreplace $domain0 $pos $pos]
    foreach var $vars {
        append body "foreach $var \$domain[expr [lsearch $initials $var]>=0] \{\n"
        lappend map $var $$var
        foreach outer $outers {
            append body "if {$$var eq $$outer} continue\n"
        }
        lappend outers $var
        append epilog \}
    }
    set test [string map $map $problem]
    append body "if {\[expr $test\]} {return \[subst $test\]}" $epilog
    if 1 $body
 }

This passes the tests from earlier in this page:

 % solve SEND+MORE=MONEY
 9567+1085==10652
 % solve SAVE+MORE=MONEY
 9386+1076==10462
 % solve YELLOW+YELLOW+RED=ORANGE
 143329+143329+846==287504

So this routine is not blindingly fast, but can process a number of problems from earlier in this page, without other configuration than specifying the problem.

Another kind of cryptarithm I found in Martin Gardner's Mathematical Circus:

 EVE/DID=.TALKTALKTALK...

requires epsilon comparison with a periodic fraction... Any takers? - Took it myself, by replacing == equality with abs(delta)<epsilon:


 proc solve {problem {domain0 {0 1 2 3 4 5 6 7 8 9}}} {
    set vars [lsort -u [split [regsub -all {[^A-Z]} $problem ""] ""]]
    set map {= )-( ... ""}
    set outers {}
    set initials [regexp -all -inline {[^A-Z]([A-Z])} /$problem]
    set pos [lsearch $domain0 0]
    set domain1 [lreplace $domain0 $pos $pos]
    foreach var $vars {
        append body "foreach $var \$domain[expr [lsearch $initials $var]>=0] \{\n"
        lappend map $var $$var
        foreach outer $outers {
            append body "if {$$var eq $$outer} continue\n"
        }
        lappend outers $var
        append epilog \}
    }
    set test abs(([string map $map $problem]))<=.00000001
    append body "if {\[expr $test\]} {return \[subst $test\]}" $epilog
    if 1 $body
 }

The other tests still pass, but the output is a bit harder to read:

 % solve EVE/DID.=.TALKTALK...
 abs((212/606.)-(.34983498))<=.00000001
 % solve SEND+MORE=MONEY
 abs((9567+1085)-(10652))<=.00000001
 % solve ABCDE*4=EDCBA
 abs((21978*4)-(87912))<=.00000001
 % solve 7AB*CD=EFGHJ
 abs((713*59)-(42067))<=.00000001