Solved Day 15
parent
f8d6c51748
commit
7785cbb1d9
|
@ -0,0 +1,4 @@
|
||||||
|
Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5
|
||||||
|
PeanutButter: capacity -1, durability 3, flavor 0, texture 0, calories 1
|
||||||
|
Frosting: capacity 0, durability -1, flavor 4, texture 0, calories 6
|
||||||
|
Sugar: capacity -1, durability 0, flavor 0, texture 2, calories 8
|
|
@ -0,0 +1,38 @@
|
||||||
|
#!/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}."
|
Loading…
Reference in New Issue