Zac_McDonald

Tens Combos - Ruby

Sep 22nd, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.60 KB | None | 0 0
  1. $results = { :ignore => [], :include => [], 1 => [], 2 => [], 4 => [], 8 => [] }
  2.  
  3. def sum_array (numbers)
  4.     n = 0
  5.     for i in 0...numbers.length
  6.         n += numbers[i]
  7.     end
  8.     return n
  9. end
  10.  
  11. def dec_to_array (n)
  12.     array = []
  13.     numbers = [1,2,1,3,6].reverse()
  14.     for i in 0..4
  15.         a = 2.pow(i)
  16.         if (n & a == a)
  17.             array.push(numbers[i])
  18.         end
  19.     end
  20.     return array
  21. end
  22.  
  23. # Calculate included and excluded values
  24. for i in 0...32
  25.     array = dec_to_array(i)
  26.     sum = sum_array(array)
  27.     if (sum > 9 || i >> 3 == 3)
  28.         $results[:ignore].push(i)
  29.     else
  30.         $results[:include].push(i)
  31.  
  32.         # Fill out bit-results
  33.         for j in 0..3
  34.             a = 2.pow(j)
  35.             if (sum & a == a)
  36.                 $results[a].push(i)
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. File.open("./tens_combos.txt", 'w') { |file|
  43.     file.puts "32 total combinations"
  44.     for i in 0...$results.keys.length
  45.         result = $results[$results.keys[i]]
  46.         file.puts "------------|| #{$results.keys[i]} (#{result.length} combos) ||------------"
  47.         for j in 0...result.length
  48.             dec = result[j]
  49.             file.puts "#{dec_to_array(dec).to_s} = #{dec}"
  50.         end
  51.     end
  52.     file.puts "\n"
  53.     for i in 0...$results.keys.length
  54.         result = $results[$results.keys[i]]
  55.         file.puts "------------|| #{$results.keys[i]} (#{result.length} combos) ||------------"
  56.         for j in 0...result.length
  57.             file.print "#{result[j]}"
  58.             if (j < result.length - 1)
  59.                 file.print(",")
  60.             end
  61.         end
  62.         file.print("\n")
  63.     end
  64. }
  65.  
  66. # MAPPING RESULTS
  67.  
  68. # 1-bit
  69. # F = 11'3' + 1'13' + 1'1'3 + 113
  70.  
  71. # 2-bit
  72. # F = 1'2'1'36' + 1'2'3'6 + 113'6' + 23'6' + 11'6 + 213
  73.  
  74. # 4-bit
  75. # F = 1'2'3'6 + 11'6 + 13 + 23 + 13(second 1)
  76.  
  77. # 8-bit
  78. # F = 116 + 26 + 36
Add Comment
Please, Sign In to add comment