|
| 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 | +} |
0 commit comments