Sunday, March 21, 2010

Maybe not the last kakuro post...

I discovered last night that my solver didn't work for puzzles like this one by Vegard Hanssen. So it took rather more thinking, and a little more coding (which Python makes easy) to get that one solved.

I've put the new one in place of the old one -- all visible at http://cpark.users.sonic.net/kakuro-python/. Besides the prettier HTML, which I got from Vegard Hanssen's site (see the above puzzle too), the new "k2.py" program has the following changes vs "k2-Mar_19_2255.py".

  1. Trivially, the old code had "HTML=True" near the top and the printout routine would output HTML if the variable was true. Yup...
  2. Almost as trivially (I don't think it made any difference), handle the process of setting a cell's determined value vs adjusting the content of its "possibles" list uniformly. There was one place we weren't doing that.
  3. Once we start dealing with permutations (the 2nd bullet in this earlier posting), find undecided cells that have only two possible values. For each such cell, try out first one value, then the other, to see if it leads to a contradiction. If it does lead to an impossibility, remove that value from the list of "possibles" and thereby decide the value of that cell. For example, in the aforementioned very hard puzzle #345943, doing the elementary steps brought us to this point:
      A B C D E F G H
    0          
     
    17
     
    22
     
    1      
     
    6
    10
    39
         
    2    
    19
    38
           
     
    3
    3  
    11
     
         
    4
     
    3 1
    4  
    20
    16
      3  
    6
    23
    4 2
    5
    15
     
       
    18
     
           
    6
    12
     
       
    21
    4
           
    7  
    24
     
               
    8  
    8
     
               
    As you can see, the solver didn't get too far with this one; filled in 5 but left 27 cells blank. The new logic does this for example with cell 7D:
    • 7D can be either 1 or 3 (see http://cpark.users.sonic.net/kakuro-python/veryhard.html for a complete list). But if 7D=1 then
      • 8D=3 because of 6Ddown, and
      • 8C=5 due to 8Bacross.
      • Of the combinations allowed by 2Cdown, there's only one allowing 8C=5: (6,9,8,3,7,5), which dictates for example that 7C=7.
      • This is a problem because 7Bacross doesn't have any permutations starting (7,1,...).
    • Hence 7D cannot be 1.
    • Therefore 7D=3, 8D=1, 8C=7, etc.
    This same logic, combined with the permutation-checking code described earlier, handles this puzzle as well as puzzle #18999, which takes about 7 CPU-seconds (feels like forever) on this 1GHz Athlon3000+ box (bogomips 2009) running 32-bit 2.6.28-18-generic (Ubuntu 9.04) and Python 2.6.2
So unless I find any more kakuros that this solver can't handle, this will really be the last kakuro post.

No comments: