Advertisement
Zac_McDonald

Carry Combos - Ruby

Sep 22nd, 2019
1,640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.64 KB | None | 0 0
  1. $results = { 0 => [], 1 => [], 2 => [], 3 => [], 4 => [], 5 => [] }
  2.  
  3. def get_tens_column (numbers)
  4.     n = 0
  5.     for i in 0...numbers.length
  6.         n += numbers[i]
  7.     end
  8.  
  9.     return n / 10
  10. end
  11.  
  12. def dec_to_array (n)
  13.     array = []
  14.     numbers = [1,2,4,8,6,2,4].reverse()
  15.     for i in 0..7
  16.         a = 2.pow(i)
  17.         if (n & a == a)
  18.             array.push(numbers[i])
  19.         end
  20.     end
  21.     return array
  22. end
  23.  
  24. # Populate the results
  25. for i in 0...128
  26.     combo = dec_to_array(i)
  27.     tens = get_tens_column(combo)
  28.     $results[tens].push({ :array => combo, :decimal => i })
  29. end
  30.  
  31. # Output Combinations
  32. File.open("./combos.txt", 'w') { |file|
  33.     file.puts "128 total combinations"
  34.     for i in 0...$results.keys.length
  35.         result = $results[$results.keys[i]]
  36.         file.puts "------------|| #{$results.keys[i]} (#{result.length} combos) ||------------"
  37.         for j in 0...result.length
  38.             file.puts "#{result[j][:array].to_s} = #{result[j][:decimal]}"
  39.         end
  40.     end
  41.     file.puts "\n"
  42.     for i in 0...$results.keys.length
  43.         result = $results[$results.keys[i]]
  44.         file.puts "------------|| #{$results.keys[i]} (#{result.length} combos) ||------------"
  45.         for j in 0...result.length
  46.             file.print "#{result[j][:decimal]}"
  47.             if (j < result.length - 1)
  48.                 file.print(",")
  49.             end
  50.         end
  51.         file.print("\n")
  52.     end
  53. }
  54.  
  55.  
  56. # MAPPING RESULTS
  57. # using: https://www.charlie-coleman.com/experiments/kmap/
  58.  
  59. # Does 10s column equal 0:
  60. # F = 2'4'6'2'4' + 4'8'2'4' + 2'8'6'2' + 2'4'8'4' + 8'6'4' + 4'8'6'
  61.  
  62. # Does 10s column equal 1:
  63. # F = 2'862'4' + 2'4'62'4 + 24'86' + 486'2' + 2'486' + 4'864' + 86'24' + 2'86'2 + 2'86'4 + 248'4 + 28'62 + 48'24 + 48'6 + 8'64
  64.  
  65. # Does 10s column equal 2:
  66. # F = 24824 + 2486 + 2864 + 4862 + 4864 + 8624
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement