diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-11-07 22:27:13 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-11-07 22:27:13 +0000 |
commit | 7d86157dfe52a76e03694be4592e32d9dec69ed9 (patch) | |
tree | 2e322463a0c5a15c09a78954334753d374316e31 /rb/day15.rb | |
parent | d7d62da5e9c2ca991885641e0a55c2907eb0fb7e (diff) |
Diffstat (limited to 'rb/day15.rb')
-rwxr-xr-x | rb/day15.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/rb/day15.rb b/rb/day15.rb new file mode 100755 index 0000000..1400862 --- /dev/null +++ b/rb/day15.rb @@ -0,0 +1,64 @@ +#!/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}" |