Solved Day 15

main
s00ner 2022-07-18 16:55:17 -04:00
parent f8d6c51748
commit 7785cbb1d9
2 changed files with 42 additions and 0 deletions

4
day15/input Normal file
View File

@ -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

38
day15/solution.rb Normal file
View File

@ -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}."