Skip to content
Browse files

read me info

  • Loading branch information...
1 parent 90d9b49 commit 19a60bdb93cf9a7997a71a97406d88c08d1d0ec4 @szeryf committed Jun 30, 2010
Showing with 84 additions and 0 deletions.
  1. +28 −0 README
  2. +56 −0 kakuro.hs
View
28 README
@@ -0,0 +1,28 @@
+= Simple Kakuro helper script
+
+This is a script I wrote for myself to help me in solving Kakuro puzzles. It finds all the digit combinations with given sum, for example:
+
+ $ ruby kakuro.rb 37 6
+ Combinations:
+ [2, 5, 6, 7, 8, 9]
+ [3, 4, 6, 7, 8, 9]
+ In all:
+ [6, 7, 8, 9]
+ Never:
+ [1]
+
+It displays all of the digit combinations, then a list of digits that appear in every combination then a list of digits that never appear (these are very helpful hints when solving).
+
+If you have already determined some of the digits, you can add them as additional parameters:
+
+ $ ruby kakuro.rb 20 4 3 6
+ Combinations:
+ [2, 3, 6, 9]
+ [3, 4, 6, 7]
+ In all:
+ [3, 6]
+ Never:
+ [1, 5, 8]
+
+This lists only those 4-digit combinations with the sum of 20 that contain both 3 and 6, in this case there are only 2 such combinations (without these constraints there are 12).
+
View
56 kakuro.hs
@@ -0,0 +1,56 @@
+import Data.List
+import Data.Char
+
+digits :: [Int]
+digits = [1..9]
+
+combinations :: Int -> [a] -> [[a]]
+combinations 0 _ = [ [] ]
+combinations n xs = [ y:ys | y:xs' <- tails xs
+ , ys <- combinations (n-1) xs']
+
+kakuroSums :: Int -> Int -> [[Int]]
+kakuroSums k count = sort $
+ filter (\l -> k == sum l)
+ (combinations count digits)
+
+combs :: [[a]] -> [[a]]
+combs ((x:xs):xxs) = (map (x:) (combs xxs)) ++ combs (xs:xxs)
+combs (xs:[]) = map (\x -> [x]) xs
+combs ([]:xxs) = []
+combs [] = [[]]
+
+unique :: Eq a => [a] -> Bool
+unique lst = lst == nub lst
+
+kakuro :: Int -> [[Int]] -> [[Int]]
+kakuro k lst = sort $
+ filter (\l -> k == sum l)
+ (filter unique (combs lst))
+
+parse :: String -> [[Int]]
+parse s = parse' s [] []
+ where parse' (' ':xs) tmp res = tmp:parse' xs [] res
+ parse' ('_':xs) tmp res = parse' xs digits res
+ parse' ( x:xs) tmp res = parse' xs ((digitToInt x):tmp) res
+ parse' [] tmp res = tmp:res
+
+pk :: Int -> String -> [[Int]]
+pk k s = kakuro k (parse s)
+
+packOne :: [Int] -> [[Int]] -> [[Int]]
+packOne (x:xs) (r:rs) | x `elem` r = r : (packOne xs rs)
+ | otherwise = (x:r) : (packOne xs rs)
+packOne xs [] = map (:[]) xs
+
+pack :: [[Int]] -> [[Int]]
+pack lst = map sort (pack' lst [])
+ where pack' (x:xs) res = pack' xs (packOne x res)
+ pack' [] res = res
+
+-- main = do putStrLn $ show (kakuro 33 [[8,9],[7,9],[3],[7,8,9],[6]])
+main = do putStrLn $ show $ pack result
+ putStrLn $ intercalate "\n" $ map show result
+ where result = pk 20 "8 15 12 145 45"
+ -- where result = pk 22 "2 13 _ 135 7 45"
+ -- where result = pk 23 "6 13 _ 1235 9"

0 comments on commit 19a60bd

Please sign in to comment.
Something went wrong with that request. Please try again.