AoC2015/day15/solution.rb

39 lines
1.3 KiB
Ruby

#!/bin/env ruby
require 'pry'
input = File.readlines('./input').map(&:chomp)
ingredients = input.to_h do |line|
line = line.delete(':').delete(',').split
ingredient = line[0].to_sym
properties = line[1..-1].each_slice(2).map { |property, num| [property.to_sym, num.to_i] }.to_h
[ingredient, properties]
end
sets = []
100.times { |a| 100.times { |b| 100.times { |c| 100.times { |d| sets.push([a, b, c, d]) if a + b + c + d == 100 } } } }
totals = []
properties = ingredients.values[0].keys
properties.delete(:calories)
scores = []
sets.each.with_index do |set, _i|
p_scores = properties.map do |property|
ingredients.values.map { |hash| hash[property] }.zip(set).map { |pair| pair.reduce(&:*) }.sum
end
p_scores.map! { |score| score < 0 ? 0 : score }
scores.push(p_scores.reduce(&:*))
end
puts "Part 1 Solution: #{scores.max}."
# part 2
scores = []
sets.each.with_index do |set, _i|
next unless ingredients.values.map { |hash| hash[:calories] }.zip(set).map { |pair| pair.reduce(&:*) }.sum == 500
p_scores = properties.map do |property|
ingredients.values.map { |hash| hash[property] }.zip(set).map { |pair| pair.reduce(&:*) }.sum
end
p_scores.map! { |score| score < 0 ? 0 : score }
scores.push(p_scores.reduce(&:*))
end
puts "Part 2 Solution: #{scores.max}."