# AoC 2015 Puzzle 14

###### 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 14: Reindeer Olympics

This year is the Reindeer Olympics! Reindeer can fly at high speeds, but must rest occasionally to recover their energy. Santa would like to know which of his reindeer is fastest, and so he has them race.

Reindeer can only either be flying (always at their top speed) or resting (not moving at all), and always spend whole seconds in either state.

For example, suppose you have the following Reindeer:

• Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.
• Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.

After one second, Comet has gone 14 km, while Dancer has gone 16 km. After ten seconds, Comet has gone 140 km, while Dancer has gone 160 km. On the eleventh second, Comet begins resting (staying at 140 km), and Dancer continues on for a total distance of 176 km. On the 12th second, both reindeer are resting. They continue to rest until the 138th second, when Comet flies for another ten seconds. On the 174th second, Dancer flies for another 11 seconds.

In this example, after the 1000th second, both reindeer are resting, and Comet is in the lead at 1120 km (poor Dancer has only gotten 1056 km by that point). So, in this situation, Comet would win (if the race ended at 1000 seconds).

Given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, what distance has the winning reindeer traveled?

### Part 2

Seeing how reindeer move in bursts, Santa decides he's not pleased with the old scoring system.

Instead, at the end of each second, he awards one point to the reindeer currently in the lead. (If there are multiple reindeer tied for the lead, they each get one point.) He keeps the traditional 2503 second time limit, of course, as doing otherwise would be entirely ridiculous.

Given the example reindeer from above, after the first second, Dancer is in the lead and gets one point. He stays in the lead until several seconds into Comet's second burst: after the 140th second, Comet pulls into the lead and gets his first point. Of course, since Dancer had been in the lead for the 139 seconds before that, he has accumulated 139 points by the 140th second.

After the 1000th second, Dancer has accumulated 689 points, while poor Comet, our old champion, only has 312. So, with the new scoring system, Dancer would win (if the race ended at 1000 seconds).

Again given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, how many points does the winning reindeer have?

### 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];
}
}
}
}
}

@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());
}
```