01020304050607080910111213141516171819202122232425

Advent of Code

2015/15

Science for Hungry People

in C#

by encse

Today, you set out on the task of perfecting your milk-dunking cookie recipe. All you have to do is find the right balance of ingredients.

Your recipe leaves room for exactly 100 teaspoons of ingredients. You make a list of the remaining ingredients you could use to finish the recipe (your puzzle input) and their properties per teaspoon:

Read the full puzzle.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace AdventOfCode.Y2015.Day15;

[ProblemName("Science for Hungry People")]
class Solution : Solver {

    public object PartOne(string input) => Solve(input, null);
    public object PartTwo(string input) => Solve(input, 500);

    long Solve(string input, int? calories) {
        var ingredients = Parse(input);
        var propsCount = ingredients[0].Length;

        var maxValue = 0L;
        foreach (var amounts in Partition(100, ingredients.Length)) {
            var props = new int[propsCount];
            for (int ingredient = 0; ingredient < ingredients.Length; ingredient++) {
                for (int prop = 0; prop < 5; prop++) {
                    props[prop] += ingredients[ingredient][prop] * amounts[ingredient];
                }
            }
            if (!calories.HasValue || calories.Value == props.Last()) {
                var value = props.Take(propsCount - 1).Aggregate(1L, (acc, p) => acc * Math.Max(0, p));
                maxValue = Math.Max(maxValue, value);
            }
        }
        return maxValue;
    }

    int[][] Parse(string input) =>
        (from line in input.Split('\n')
         let m = Regex.Match(line, @".*: capacity (.*), durability (.*), flavor (.*), texture (.*), calories (.*)")
         let nums = m.Groups.Cast<Group>().Skip(1).Select(g => int.Parse(g.Value)).ToArray()
         select nums).ToArray();

    IEnumerable<int[]> Partition(int n, int k) {
        if (k == 1) {
            yield return new int[] { n };
        } else {
            for (var i = 0; i <= n; i++) {
                foreach (var rest in Partition(n - i, k - 1)) {
                    yield return rest.Select(x => x).Append(i).ToArray();
                }
            }
        }
    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator