-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDay15.java
138 lines (117 loc) · 4.43 KB
/
Day15.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.sbaars.adventofcode.year15.days;
import com.sbaars.adventofcode.year15.Day2015;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Day15 extends Day2015 {
private static final int TOTAL_TEASPOONS = 100;
private final List<Ingredient> ingredients = new ArrayList<>();
public Day15() {
super(15);
parseInput();
}
public static void main(String[] args) {
Day15 day = new Day15();
day.printParts();
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 15, 1);
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 15, 2);
}
private void parseInput() {
Pattern pattern = Pattern.compile("(\\w+): capacity (-?\\d+), durability (-?\\d+), flavor (-?\\d+), texture (-?\\d+), calories (-?\\d+)");
Arrays.stream(day().split("\n"))
.map(pattern::matcher)
.filter(Matcher::find)
.map(matcher -> new Ingredient(
matcher.group(1),
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)),
Integer.parseInt(matcher.group(5)),
Integer.parseInt(matcher.group(6))
))
.forEach(ingredients::add);
}
@Override
public Object part1() {
return findBestScore();
}
@Override
public Object part2() {
return findBestScoreWithCalories(500);
}
private long findBestScore() {
return generateCombinations(TOTAL_TEASPOONS, ingredients.size())
.stream()
.mapToLong(this::calculateScore)
.max()
.orElse(0);
}
private long findBestScoreWithCalories(int targetCalories) {
return generateCombinations(TOTAL_TEASPOONS, ingredients.size())
.stream()
.filter(amounts -> calculateCalories(amounts) == targetCalories)
.mapToLong(this::calculateScore)
.max()
.orElse(0);
}
private List<List<Integer>> generateCombinations(int total, int parts) {
List<List<Integer>> result = new ArrayList<>();
generateCombinationsHelper(total, parts, new ArrayList<>(), result);
return result;
}
private void generateCombinationsHelper(int remaining, int parts, List<Integer> current, List<List<Integer>> result) {
if (parts == 1) {
List<Integer> combination = new ArrayList<>(current);
combination.add(remaining);
result.add(combination);
return;
}
for (int i = 0; i <= remaining; i++) {
List<Integer> newCurrent = new ArrayList<>(current);
newCurrent.add(i);
generateCombinationsHelper(remaining - i, parts - 1, newCurrent, result);
}
}
private long calculateScore(List<Integer> amounts) {
int capacity = 0;
int durability = 0;
int flavor = 0;
int texture = 0;
for (int i = 0; i < ingredients.size(); i++) {
Ingredient ingredient = ingredients.get(i);
int amount = amounts.get(i);
capacity += ingredient.capacity * amount;
durability += ingredient.durability * amount;
flavor += ingredient.flavor * amount;
texture += ingredient.texture * amount;
}
if (capacity <= 0 || durability <= 0 || flavor <= 0 || texture <= 0) {
return 0;
}
return (long) capacity * durability * flavor * texture;
}
private int calculateCalories(List<Integer> amounts) {
int calories = 0;
for (int i = 0; i < ingredients.size(); i++) {
calories += ingredients.get(i).calories * amounts.get(i);
}
return calories;
}
private static class Ingredient {
private final String name;
private final int capacity;
private final int durability;
private final int flavor;
private final int texture;
private final int calories;
public Ingredient(String name, int capacity, int durability, int flavor, int texture, int calories) {
this.name = name;
this.capacity = capacity;
this.durability = durability;
this.flavor = flavor;
this.texture = texture;
this.calories = calories;
}
}
}