Skip to content

Commit f819d73

Browse files
committed
add solutions for day 15, 16, 17, 2015
1 parent a76f6b8 commit f819d73

File tree

5 files changed

+304
-3
lines changed

5 files changed

+304
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ Link: https://adventofcode.com/2015/
188188
|[Day 12](https://github.com/tmrd993/advent-of-code-solutions/blob/master/src/main/java/2k15/aoc15/Day12.java) | JSAbacusFramework.io | [Blog Post](https://adventofcode.com/2015/day/12)|
189189
|[Day 13](https://github.com/tmrd993/advent-of-code-solutions/blob/master/src/main/java/2k15/aoc15/Day13.java) | Knights of the Dinner Table | [Blog Post](https://adventofcode.com/2015/day/13)|
190190
|[Day 14](https://github.com/tmrd993/advent-of-code-solutions/blob/master/src/main/java/2k15/aoc15/Day14.java) | Reindeer Olympics | [Blog Post](https://adventofcode.com/2015/day/14)|
191-
|[TBD] | Science for Hungry People | [Blog Post](https://adventofcode.com/2015/day/15)|
192-
|[TBD] | Aunt Sue | [Blog Post](https://adventofcode.com/2015/day/16)|
193-
|[TBD] | No Such Thing as Too Much | [Blog Post](https://adventofcode.com/2015/day/17)|
191+
|[Day 15](https://github.com/tmrd993/advent-of-code-solutions/blob/master/src/main/java/2k15/aoc15/Day15.java) | Science for Hungry People | [Blog Post](https://adventofcode.com/2015/day/15)|
192+
|[Day 16](https://github.com/tmrd993/advent-of-code-solutions/blob/master/src/main/java/2k15/aoc15/Day16.java) | Aunt Sue | [Blog Post](https://adventofcode.com/2015/day/16)|
193+
|[Day 17](https://github.com/tmrd993/advent-of-code-solutions/blob/master/src/main/java/2k15/aoc15/Day17.java) | No Such Thing as Too Much | [Blog Post](https://adventofcode.com/2015/day/17)|
194194
|[TBD] | Like a GIF For Your Yard | [Blog Post](https://adventofcode.com/2015/day/18)|
195195
|[TBD] | Medicine for Rudolph | [Blog Post](https://adventofcode.com/2015/day/19)|
196196
|[TBD] | Infinite Elves and Infinite Houses | [Blog Post](https://adventofcode.com/2015/day/20)|

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,10 @@
5656
<artifactId>javax.json</artifactId>
5757
<version>1.1</version>
5858
</dependency>
59+
<dependency>
60+
<groupId>com.googlecode.combinatoricslib</groupId>
61+
<artifactId>combinatoricslib</artifactId>
62+
<version>2.3</version>
63+
</dependency>
5964
</dependencies>
6065
</project>

src/main/java/2k15/aoc15/Day15.java

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package aoc15;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Map.Entry;
8+
import java.util.function.Function;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.IntStream;
13+
14+
import static org.paukov.combinatorics.CombinatoricsFactory.createVector;
15+
import static org.paukov.combinatorics.CombinatoricsFactory.createMultiCombinationGenerator;
16+
import org.paukov.combinatorics.Generator;
17+
import org.paukov.combinatorics.ICombinatoricsVector;
18+
19+
import myutils15.StaticUtils;
20+
21+
public class Day15 {
22+
23+
private List<String> rawData;
24+
private final Pattern NUMBER = Pattern.compile("-?\\d+");
25+
26+
public Day15(File input) {
27+
rawData = StaticUtils.fileToStringList(input);
28+
}
29+
30+
public int run1() {
31+
List<Ingredient> ingredients = getIngredients();
32+
ICombinatoricsVector<Integer> vector = createVector(
33+
IntStream.range(0, rawData.size()).boxed().toArray(Integer[]::new));
34+
Generator<Integer> gen = createMultiCombinationGenerator(vector, 100);
35+
36+
int max = 0;
37+
38+
for (ICombinatoricsVector<Integer> combination : gen) {
39+
Map<Integer, Long> ingredientFrequencyTable = combination.getVector().stream()
40+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
41+
int result = calculateCookieProperties(ingredients, ingredientFrequencyTable);
42+
if (result > max) {
43+
max = result;
44+
}
45+
46+
}
47+
return max;
48+
}
49+
50+
public int run2() {
51+
List<Ingredient> ingredients = getIngredients();
52+
ICombinatoricsVector<Integer> vector = createVector(
53+
IntStream.range(0, rawData.size()).boxed().toArray(Integer[]::new));
54+
Generator<Integer> gen = createMultiCombinationGenerator(vector, 100);
55+
56+
int max = 0;
57+
58+
for (ICombinatoricsVector<Integer> combination : gen) {
59+
Map<Integer, Long> ingredientFrequencyTable = combination.getVector().stream()
60+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
61+
int calories = ingredientFrequencyTable.entrySet().stream()
62+
.mapToInt(e -> e.getValue().intValue() * ingredients.get(e.getKey()).getCalories()).sum();
63+
int result = calculateCookieProperties(ingredients, ingredientFrequencyTable);
64+
if (calories == 500 && result > max) {
65+
max = result;
66+
}
67+
68+
}
69+
return max;
70+
}
71+
72+
private int calculateCookieProperties(List<Ingredient> ingredients, Map<Integer, Long> ingredientFrequencyTable) {
73+
int cap = 0;
74+
int dur = 0;
75+
int fla = 0;
76+
int tex = 0;
77+
for (Entry<Integer, Long> entry : ingredientFrequencyTable.entrySet()) {
78+
Ingredient currentIngredient = ingredients.get(entry.getKey());
79+
int multiplier = entry.getValue().intValue();
80+
cap += currentIngredient.getCapacity() * multiplier;
81+
dur += currentIngredient.getDurability() * multiplier;
82+
fla += currentIngredient.getFlavor() * multiplier;
83+
tex += currentIngredient.getTexture() * multiplier;
84+
}
85+
86+
return List.of(cap, dur, fla, tex).stream().filter(p -> p > 0).reduce(1, (a, b) -> a * b);
87+
}
88+
89+
private List<Ingredient> getIngredients() {
90+
List<Ingredient> ingredients = new ArrayList<>();
91+
for (String ingredientData : rawData) {
92+
Matcher matcher = NUMBER.matcher(ingredientData);
93+
matcher.find();
94+
int capacity = Integer.parseInt(matcher.group());
95+
matcher.find();
96+
int durability = Integer.parseInt(matcher.group());
97+
matcher.find();
98+
int flavor = Integer.parseInt(matcher.group());
99+
matcher.find();
100+
int texture = Integer.parseInt(matcher.group());
101+
matcher.find();
102+
int calories = Integer.parseInt(matcher.group());
103+
ingredients.add(new Ingredient(capacity, durability, flavor, texture, calories));
104+
}
105+
return ingredients;
106+
}
107+
108+
public static void main(String[] args) {
109+
Day15 test = new Day15(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2015\\Day 15\\InputFile.txt"));
110+
System.out.println(test.run1());
111+
System.out.println(test.run2());
112+
}
113+
114+
private static class Ingredient {
115+
private final int capacity;
116+
private final int durability;
117+
private final int flavor;
118+
private final int texture;
119+
private final int calories;
120+
121+
public Ingredient(int capacity, int durability, int flavor, int texture, int calories) {
122+
this.capacity = capacity;
123+
this.durability = durability;
124+
this.flavor = flavor;
125+
this.texture = texture;
126+
this.calories = calories;
127+
}
128+
129+
public int getCapacity() {
130+
return capacity;
131+
}
132+
133+
public int getDurability() {
134+
return durability;
135+
}
136+
137+
public int getFlavor() {
138+
return flavor;
139+
}
140+
141+
public int getTexture() {
142+
return texture;
143+
}
144+
145+
public int getCalories() {
146+
return calories;
147+
}
148+
}
149+
150+
}

src/main/java/2k15/aoc15/Day16.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package aoc15;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Set;
10+
import java.util.stream.Collectors;
11+
12+
import myutils15.StaticUtils;
13+
14+
public class Day16 {
15+
16+
private List<String> rawData;
17+
private List<String> requirements = List.of("children: 3", "cats: 7", "samoyeds: 2", "pomeranians: 3", "akitas: 0",
18+
"vizslas: 0", "goldfish: 5", "trees: 3", "cars: 2", "perfumes: 1");
19+
20+
public Day16(File input) {
21+
rawData = StaticUtils.fileToStringList(input);
22+
}
23+
24+
public int run1() {
25+
List<String> possibleSues = new ArrayList<>(rawData);
26+
Set<String> impossibleSues = new HashSet<>();
27+
for (String sue : possibleSues) {
28+
29+
for (String req : requirements) {
30+
String compound = req.substring(0, req.indexOf(':'));
31+
if (sue.contains(compound) && !sue.contains(req)) {
32+
impossibleSues.add(sue);
33+
}
34+
}
35+
}
36+
37+
possibleSues.removeAll(impossibleSues);
38+
39+
return Integer.parseInt(possibleSues.get(0).substring("Sue".length() + 1, possibleSues.get(0).indexOf(':')));
40+
}
41+
42+
public int run2() {
43+
List<String> possibleSues = new ArrayList<>(rawData);
44+
Set<String> impossibleSues = new HashSet<>();
45+
for (String sue : possibleSues) {
46+
Map<String, Integer> sueCompounds = Arrays.stream(sue.substring(sue.indexOf(':') + 2).split(","))
47+
.collect(Collectors.toMap(s -> s.trim().substring(0, s.trim().indexOf(':')),
48+
s -> Integer.parseInt(s.substring(s.indexOf(':') + 2))));
49+
for (String req : requirements) {
50+
String compound = req.substring(0, req.indexOf(':'));
51+
int compoundAmount = Integer.parseInt(Character.toString(req.charAt(req.length() - 1)));
52+
if ((compound.equals("trees") || compound.equals("cats"))) {
53+
if (sue.contains(compound) && sueCompounds.get(compound) <= compoundAmount)
54+
impossibleSues.add(sue);
55+
} else if ((compound.equals("pomeranians") || compound.equals("goldfish"))) {
56+
if (sue.contains(compound) && sueCompounds.get(compound) >= compoundAmount)
57+
impossibleSues.add(sue);
58+
} else if (sue.contains(compound) && !sue.contains(req)) {
59+
impossibleSues.add(sue);
60+
}
61+
}
62+
}
63+
64+
possibleSues.removeAll(impossibleSues);
65+
66+
return Integer.parseInt(possibleSues.get(0).substring("Sue".length() + 1, possibleSues.get(0).indexOf(':')));
67+
68+
}
69+
70+
public static void main(String[] args) {
71+
Day16 test = new Day16(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2015\\Day 16\\InputFile.txt"));
72+
System.out.println(test.run1());
73+
System.out.println(test.run2());
74+
}
75+
76+
}

src/main/java/2k15/aoc15/Day17.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package aoc15;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import myutils15.StaticUtils;
10+
11+
public class Day17 {
12+
13+
private List<Integer> inputNumbers;
14+
15+
public Day17(File input) {
16+
inputNumbers = StaticUtils.fileToStringList(input).stream().map(s -> Integer.parseInt(s))
17+
.collect(Collectors.toList());
18+
}
19+
20+
public int run1() {
21+
Counter counter = new Counter();
22+
count(0, 0, counter, inputNumbers, 0, new ArrayList<>());
23+
return counter.count();
24+
}
25+
26+
public int run2() {
27+
List<Integer> filledContainers = new ArrayList<>();
28+
count(0, 0, new Counter(), inputNumbers, 0, filledContainers);
29+
int min = Collections.min(filledContainers);
30+
return (int) filledContainers.stream().filter(n -> n == min).count();
31+
}
32+
33+
public void count(int index, int sum, Counter counter, List<Integer> numbers, int numOfContainers, List<Integer> filledContainers) {
34+
if(sum == 150) {
35+
filledContainers.add(numOfContainers);
36+
counter.increment();
37+
return;
38+
}
39+
40+
if(index >= numbers.size()) {
41+
return;
42+
}
43+
44+
count(index + 1, sum + numbers.get(index), counter, numbers, numOfContainers + 1, filledContainers);
45+
count(index + 1, sum, counter, numbers, numOfContainers, filledContainers);
46+
}
47+
48+
public static void main(String[] args) {
49+
Day17 test = new Day17(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2015\\Day 17\\InputFile.txt"));
50+
System.out.println(test.run1());
51+
System.out.println(test.run2());
52+
}
53+
54+
private static class Counter {
55+
private int count;
56+
57+
public Counter() {
58+
count = 0;
59+
}
60+
61+
public void increment() {
62+
count++;
63+
}
64+
65+
public int count() {
66+
return count;
67+
}
68+
}
69+
70+
}

0 commit comments

Comments
 (0)