Advertisement
dashohoxha

good_luck.rb

Apr 27th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.39 KB | None | 0 0
  1. # A solution for:
  2. # https://code.google.com/codejam/contest/2418487/dashboard#s=p2
  3. # Brute force, generating and checking all the cases.
  4. # Works for the small input, does not work for the large one.
  5.  
  6. def get_all_n_numbers(n, m)
  7.   if n == 1
  8.     numbers = []
  9.     for i in 2..m
  10.       numbers << i.to_s
  11.     end
  12.     return numbers
  13.   end
  14.  
  15.   numbers_1 = get_all_n_numbers(n-1, m)
  16.   numbers = []
  17.   for num in numbers_1
  18.     for i in 2..m
  19.       numbers << "#{num}#{i}"
  20.     end
  21.   end
  22.   return numbers
  23. end
  24.  
  25. def get_all_random_products(numbers, n)
  26.   return [1, numbers[0].to_i ] if n == 1
  27.   products_1 = get_all_random_products(numbers, n-1)
  28.   products = []
  29.   for p in products_1
  30.     products << p
  31.     products << p * numbers[n-1].to_i
  32.   end
  33.   return products
  34. end
  35.  
  36. gets
  37. puts "Case #1:"
  38.  
  39. r, n, m, k = gets.chomp.split.map { |x| x.to_i }
  40. products_list = []
  41. r.times do
  42.   products_list << gets.chomp.split.map { |x| x.to_i }
  43. end
  44.  
  45. all_n_numbers = get_all_n_numbers(n, m)
  46. product_hash = {}
  47. for numbers in all_n_numbers
  48.   for product in get_all_random_products(numbers, n)
  49.     product_hash[product] = [] if product_hash[product].nil?
  50.     product_hash[product] << numbers
  51.   end
  52. end
  53.  
  54. for products in products_list
  55.   possible_numbers = all_n_numbers
  56.   for product in products
  57.     possible_numbers = possible_numbers & product_hash[product]
  58.   end
  59.   puts possible_numbers.sample
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement