Add solution for 2015 day 15

This commit is contained in:
Patrick Auernig 2021-02-18 19:58:43 +01:00
parent 7e08bdaf90
commit ab4557ec1b
5 changed files with 55 additions and 1 deletions

View File

@ -16,7 +16,7 @@
| ✓ | 12 | Ruby | ✓ | 12 | Ruby
| ✓ | 13 | Ruby | ✓ | 13 | Ruby
| ✓ | 14 | Ruby | ✓ | 14 | Ruby
| | 15 | | ✓ | 15 | Ruby
| | 16 | | | 16 |
| | 17 | | | 17 |
| | 18 | | | 18 |

View File

@ -0,0 +1 @@
3.0.0

View File

@ -0,0 +1,4 @@
Sprinkles: capacity 2, durability 0, flavor -2, texture 0, calories 3
Butterscotch: capacity 0, durability 5, flavor -3, texture 0, calories 3
Chocolate: capacity 0, durability 0, flavor 5, texture -1, calories 8
Candy: capacity 0, durability -1, flavor 0, texture 5, calories 8

View File

@ -0,0 +1,2 @@
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3

47
2015/day-15/main.rb Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require "pathname"
Ingredient = Struct.new(:name, :capacity, :durability, :flavor, :texture, :calories, keyword_init: true)
def parse_file(file)
file.each_line.map do |line|
line.split(": ") => [name, tail]
tail.split(", ").map(&:split).map { |(k, v)| [k.to_sym, v.to_i] }.to_h => properties
Ingredient.new(name: name, **properties)
end
end
# Not the most efficient solution, but it does its job
def calculate_scores(ingredients)
size = ingredients.size
(1..(100 - size + 1)).to_a.repeated_permutation(size).select { |x| x.sum == 100 }.map do |perm|
total_score = %i[capacity durability flavor texture].reduce(1) do |total_score, property|
score = ingredients.each_with_index.sum { |ing, idx| perm[idx] * ing[property] }
total_score * [score, 0].max
end
calories = ingredients.each_with_index.sum { |ing, idx| perm[idx] * ing.calories }
[total_score, calories]
end
end
def main(file_paths)
file_paths.each do |file_path|
puts "File: #{file_path}"
ingredients = parse_file(file_path)
scores = calculate_scores(ingredients)
solution1 = scores.max_by { |(score, _)| score }.first
puts " solution 1: #{solution1}"
solution2 = scores.select { |(_, cal)| cal == 500 }.max_by { |(score, _)| score }.first
puts " solution 2: #{solution2}"
end
end
main(ARGV.map { |x| Pathname(x) })