Skip to content

Instantly share code, notes, and snippets.

@jmartelletti
Created July 14, 2012 07:09
Show Gist options
  • Save jmartelletti/3109793 to your computer and use it in GitHub Desktop.
Save jmartelletti/3109793 to your computer and use it in GitHub Desktop.
scans = [
['A','B','C','D','A','B','A','A'],
['C','C','C','C','C','C','C'],
['A','B','C','D']
]
products = [
{ :code => 'A', :volume => 4, :price => 7 },
{ :code => 'A', :volume => 1, :price => 2 },
{ :code => 'B', :volume => 1, :price => 12 },
{ :code => 'C', :volume => 6, :price => 6 },
{ :code => 'C', :volume => 1, :price => 1.25 },
{ :code => 'D', :volume => 1, :price => 0.15 }
]
unique_products = products.uniq { |x| x[:code] }
scans.each do |scan|
puts scan.inspect
value = 0
unique_products.each do |product|
code = product[:code]
scan_num = scan.select { |x| x == code }.count
volume_discount = products.select { |x| x[:code] == code && x[:volume] > 1 }
if !volume_discount.empty?
volume = volume_discount.first[:volume]
price = volume_discount.first[:price]
volume_num = scan_num / volume
remainder = scan_num % volume
value += volume_num * price
end
remainder ||= scan_num
price = products.select { |x| x[:code] == code && x[:volume] == 1 }.first[:price]
value += remainder * price
end
puts value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment