Commits

Anonymous committed 332ea12

Added the internal_to_human method.

Comments (0)

Files changed (2)

solver/ruby/kakuro-perms.rb

 module Kakuro
     class Perms
-        
         def human_to_internal(sum, num)
             return [sum-num, num]
         end
+
+        def internal_to_human(bitmask)
+            d = 1
+            nums = Array.new
+            while bitmask > 0 do
+                if ((bitmask & 0x1) == 0x1)
+                    nums.push(d)
+                end
+                bitmask >>= 1
+                d += 1
+            end
+            return nums
+        end
     end
 end

solver/ruby/t/check-perms.rb

         @perm.human_to_internal(3,2).should eql([1,2])
     end
 
+    it "human->internal transform for 5/2" do
+        @perm.human_to_internal(5,2).should eql([3,2])
+    end
+
+    it "human->internal transform for 10/4" do
+        # 10 = 1 + 2 + 3 + 4
+        # 10/int = 0 + 1 + 2 + 3 == 6
+        @perm.human_to_internal(10,4).should eql([6,4])
+    end
+
+    it "internal->human transform for 1+2+3+4" do
+        @perm.internal_to_human(
+            (1 << (1-1)) | (1 << (2-1)) | (1 << (3-1)) | (1 << (4-1))
+        ).should eql([1,2,3,4])
+    end
+
 end