AoC 2015 Puzzle 15

Info

I solved this puzzle. You can find the code here, but I have yet to write a good explanation of the solution here. I will do this shortly, thank you for your patience.

Day 15: Science for Hungry People

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:

  • capacity (how well it helps the cookie absorb milk)
  • durability (how well it keeps the cookie intact when full of milk)
  • flavor (how tasty it makes the cookie)
  • texture (how it improves the feel of the cookie)
  • calories (how many calories it adds to the cookie)

You can only measure ingredients in whole-teaspoon amounts accurately, and you have to be accurate so you can reproduce your results in the future. The total score of a cookie can be found by adding up each of the properties (negative totals become 0) and then multiplying together everything except calories.

For instance, suppose you have these two ingredients:

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

Then, choosing to use 44 teaspoons of butterscotch and 56 teaspoons of cinnamon (because the amounts of each ingredient must add up to 100) would result in a cookie with the following properties:

  • A capacity of 44*-1 + 56*2 = 68
  • A durability of 44*-2 + 56*3 = 80
  • A flavor of 44*6 + 56*-2 = 152
  • A texture of 44*3 + 56*-1 = 76

Multiplying these together (68 * 80 * 152 * 76, ignoring calories for now) results in a total score of 62842880, which happens to be the best score possible given these ingredients. If any properties had produced a negative total, it would have instead become zero, causing the whole score to multiply to zero.

Given the ingredients in your kitchen and their properties, what is the total score of the highest-scoring cookie you can make?

Part 2

Your cookie recipe becomes wildly popular! Someone asks if you can make another recipe that has exactly 500 calories per cookie (so they can use it as a meal replacement). Keep the rest of your award-winning process the same (100 teaspoons, same ingredients, same scoring system).

For example, given the ingredients above, if you had instead selected 40 teaspoons of butterscotch and 60 teaspoons of cinnamon (which still adds to 100), the total calorie count would be 40*8 + 60*3 = 500. The total score would go down, though: only 57600000, the best you can do in such trying circumstances.

Given the ingredients in your kitchen and their properties, what is the total score of the highest-scoring cookie you can make with a calorie total of 500?

Complete Solution

@Data
@AllArgsConstructor
@ToString
class Ingredient {
    String name;
    int capacity;
    int durability;
    int flavor;
    int texture;
    int calories;
}

Ingredient getIngredient(String description) {
    Pattern p = Pattern.compile("(.+): capacity (.+), durability (.+), flavor (.+), texture (.+), calories (.+)");
    Matcher m = p.matcher(description);

    if (m.find()) {
        return new Ingredient(m.group(1), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)),
                Integer.parseInt(m.group(4)), Integer.parseInt(m.group(5)), Integer.parseInt(m.group(6)));
    }
    return null;
}

void calcIngredients(List<Ingredient> in, int offset, int[] quantities, List<Long[]> acc) {
    int used = Arrays.stream(quantities).limit(offset).sum();

    for (int i = 0; i <= 100 - used; i++) {
        quantities[offset] = i;
        if (offset < in.size() - 1) {
            // recurse a bit more
            calcIngredients(in, offset + 1, quantities, acc);
        } else {
            if (Arrays.stream(quantities).sum() == 100) {
                Long[] result = new Long[] { 0l, 0l, 0l, 0l, 0l };
                for (int q = 0; q <= offset; q++) {
                    Ingredient x = in.get(q);
                    result[0] += x.getCapacity() * quantities[q];
                    result[1] += x.getDurability() * quantities[q];
                    result[2] += x.getFlavor() * quantities[q];
                    result[3] += x.getTexture() * quantities[q];
                    result[4] += x.getCalories() * quantities[q];
                }
                // System.out.println(
                // String.format("%3d %3d %3d %3d %3d %s", quantities[0], quantities[1],
                // quantities[2], quantities[3], offset, "[" +
                // Math.max(0, result[0])
                // * Math.max(0, result[1])
                // * Math.max(0, result[2])
                // * Math.max(0, result[3]) + "]"));
                acc.add(result);
            }
        }
    }
}

@Override
public String part1(List<String> input) {
    List<Ingredient> ingredients = input.stream().map(this::getIngredient).collect(Collectors.toList());

    int[] q = new int[ingredients.size()];
    List<Long[]> acc = new ArrayList<>();
    calcIngredients(ingredients, 0, q, acc);

    return String.valueOf(acc.stream()
            .mapToLong(e -> Math.max(0, e[0]) * Math.max(0, e[1]) * Math.max(0, e[2]) * Math.max(0, e[3])).max()
            .getAsLong());
}

@Override
public String part2(List<String> input) {
    List<Ingredient> ingredients = input.stream().map(this::getIngredient).collect(Collectors.toList());

    int[] q = new int[ingredients.size()];
    List<Long[]> acc = new ArrayList<>();
    calcIngredients(ingredients, 0, q, acc);

    return String.valueOf(acc.stream().filter(e -> e[4] == 500)
            .mapToLong(e -> Math.max(0, e[0]) * Math.max(0, e[1]) * Math.max(0, e[2]) * Math.max(0, e[3])).max()
            .getAsLong());
}