Skip to content

Instantly share code, notes, and snippets.

@Ebtoulson
Created December 15, 2015 06:23
Show Gist options
  • Save Ebtoulson/3f49e552a5f78b232800 to your computer and use it in GitHub Desktop.
Save Ebtoulson/3f49e552a5f78b232800 to your computer and use it in GitHub Desktop.
ingredients = IO.read("./15_ingredients.txt").split("\n").map do |line|
name, *ingred_list = /(.+): .* (.*), .* (.*), .* (.*), .* (.*), .* (.*)/.match(line).captures
{name => ingred_list.map(&:to_i)}
end.inject({}) { |nh, oh| nh.merge!(oh)}
recipes = ingredients.keys.repeated_combination(100).map do |combo|
ingredients.keys.map do |i|
[i, combo.count{|ing| ing == i}]
end
end
scores = recipes.map do |recipe|
quantities = recipe.map do |name, quantity|
ingredients[name].map do |i|
quantity * i
end
end
quantity_totals = quantities.transpose.map do |x|
sum = x.reduce(:+)
sum < 0 ? 0 : sum
end
score = quantity_totals.slice(0,4).reduce(:*)
calories = quantity_totals[4]
[score, calories]
end
within_calories = scores.select do |score, calories|
calories == 500
end
best = within_calories.max_by{|score, calories| score}
puts best.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment