#!/usr/bin/env ruby class Ingredient def initialize(nam, cap, dur, fla, tex, cal) @name = nam @capacity = cap @durability = dur @flavour = fla @texture = tex @calories = cal end attr_reader :capacity, :durability, :flavour, :texture, :calories def self.parse(line) name, data = line.strip.split(": ") new(name, *data.scan(/(-?\d+)/).flatten.map(&:to_i)) end end def cookiescore(zip) cap = zip.map { |qty, ing| qty * ing.capacity }.sum cap = 0 if cap < 0 dur = zip.map { |qty, ing| qty * ing.durability }.sum dur = 0 if dur < 0 fla = zip.map { |qty, ing| qty * ing.flavour }.sum fla = 0 if fla < 0 tex = zip.map { |qty, ing| qty * ing.texture }.sum tex = 0 if tex < 0 cap * dur * fla * tex end def cookiecal(zip) zip.map { |qty, ing| qty * ing.calories }.sum end ingredients = File .readlines("day15.input") .map { |line| Ingredient.parse(line) } recipies = (0..100).to_a .permutation(ingredients.length) .select { |s| s.sum == 100 } bestscore = 0 recipies.each do |recipe| zip = recipe.zip(ingredients) score = cookiescore(zip) bestscore = score if score > bestscore end puts "Part 1: #{bestscore}" bestscore = 0 recipies.each do |recipe| zip = recipe.zip(ingredients) next unless cookiecal(zip) == 500 score = cookiescore(zip) bestscore = score if score > bestscore end puts "Part 2: #{bestscore}"