fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6.  
  7. class Day15 {
  8.  
  9. //private final static String FILENAME = ".\\InputDay15.txt";
  10. private final static Pattern PATTERN = Pattern.compile("(?<name>.+): capacity (?<cap>-??\\d+), durability (?<dur>-??\\d+), flavor (?<flav>-??\\d+), texture (?<tex>-??\\d+), calories (?<cal>-??\\d+)");
  11.  
  12. private List<Ingredient> recipe = new ArrayList<>();
  13.  
  14. class Ingredient{
  15. String name;
  16. int capacity;
  17. int durability;
  18. int flavor;
  19. int texture;
  20. int calories;
  21.  
  22. public Ingredient(String s) {
  23. Matcher m=PATTERN.matcher(s);
  24.  
  25. if(m.matches()) {
  26. name = m.group("name");
  27. capacity = Integer.parseInt(m.group("cap"));
  28. durability = Integer.parseInt(m.group("dur"));
  29. flavor = Integer.parseInt(m.group("flav"));
  30. texture = Integer.parseInt(m.group("tex"));
  31. calories = Integer.parseInt(m.group("cal"));
  32. }
  33. }
  34.  
  35. public int getTotalCapacity(int amount) {
  36. return capacity * amount;
  37. }
  38.  
  39. public int getTotalDurability(int amount) {
  40. return durability * amount;
  41. }
  42.  
  43. public int getTotalFlavor(int amount) {
  44. return flavor * amount;
  45. }
  46.  
  47. public int getTotalTexture(int amount) {
  48. return texture * amount;
  49. }
  50.  
  51. public int getTotalCalories(int amount) {
  52. return calories * amount;
  53. }
  54.  
  55.  
  56. @Override
  57. public String toString() {
  58. return String.format("%s: capacity %d, durability %d, flavor %d, texture %d, calories %d", name, capacity, durability, flavor, texture, calories);
  59. }
  60. }
  61.  
  62. public Day15() {
  63. List<String> data = new ArrayList<>();
  64. data.add("Frosting: capacity 4, durability -2, flavor 0, texture 0, calories 5");
  65. data.add("Candy: capacity 0, durability 5, flavor -1, texture 0, calories 8");
  66. data.add("Butterscotch: capacity -1, durability 0, flavor 5, texture 0, calories 6");
  67. data.add("Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1");
  68.  
  69. for(String s : data) {
  70. Ingredient i = new Ingredient(s);
  71. recipe.add(i);
  72. }
  73.  
  74. }
  75.  
  76. public static void main(String[] args) {
  77. Day15 day15 = new Day15();
  78.  
  79. day15.doPart1();
  80. day15.doPart2();
  81.  
  82. }
  83.  
  84. public void doPart1() {
  85. long startTime = System.currentTimeMillis();
  86. int maxScore = maxScore(recipe);
  87. long endTime = System.currentTimeMillis();
  88.  
  89. System.out.println("Max Cookie Score: " + maxScore + " Calculation took: " + (((double)(endTime-startTime))/1000) + "s");
  90. }
  91.  
  92. public void doPart2() {
  93. long startTime = System.currentTimeMillis();
  94. int maxScore = maxScore2(recipe);
  95. long endTime = System.currentTimeMillis();
  96.  
  97. System.out.println("Max Cookie Score with 500 calories: " + maxScore + " Calculation took: " + (((double)(endTime-startTime))/1000) + "s");
  98.  
  99. }
  100.  
  101.  
  102. public int score(List<Integer> amounts) {
  103. int cap = 0;
  104. int dur = 0;
  105. int flav = 0;
  106. int tex = 0;
  107. for(int i=0; i<recipe.size(); i++) {
  108. cap += recipe.get(i).getTotalCapacity(amounts.get(i));
  109. dur += recipe.get(i).getTotalDurability(amounts.get(i));
  110. flav += recipe.get(i).getTotalFlavor(amounts.get(i));
  111. tex += recipe.get(i).getTotalTexture(amounts.get(i));
  112. }
  113. cap = (cap < 0) ? 0 : cap;
  114. dur = (dur < 0) ? 0 : dur;
  115. flav = (flav < 0) ? 0 : flav;
  116. tex = (tex < 0) ? 0 : tex;
  117.  
  118. return cap * dur * flav * tex;
  119. }
  120.  
  121. public int getCalories(List<Integer> amounts) {
  122. int cal = 0;
  123. for(int i=0; i<recipe.size(); i++) {
  124. cal += recipe.get(i).getTotalCalories(amounts.get(i));
  125. }
  126. return cal;
  127. }
  128.  
  129. public int maxScore(List<Ingredient> recipe) {
  130. int maxScore = Integer.MIN_VALUE;
  131. for(int i=0; i <= 100; i++) {
  132. for(int j=0; j<=100; j++) {
  133. for(int k=0; k<=100; k++) {
  134. for(int l=0; l <= 100; l++) {
  135. if((i+j+k+l) == 100) {
  136. List<Integer> amounts = new ArrayList<>();
  137. amounts.add(i);
  138. amounts.add(j);
  139. amounts.add(k);
  140. amounts.add(l);
  141. int score = score(amounts);
  142. if (score > maxScore) {
  143. maxScore = score;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return maxScore;
  151. }
  152.  
  153. public int maxScore2(List<Ingredient> recipe) {
  154. int maxScore = Integer.MIN_VALUE;
  155. for(int i=0; i <= 100; i++) {
  156. for(int j=0; j<=100; j++) {
  157. for(int k=0; k<=100; k++) {
  158. for(int l=0; l <= 100; l++) {
  159. if((i+j+k+l) == 100) {
  160. List<Integer> amounts = new ArrayList<>();
  161. amounts.add(i);
  162. amounts.add(j);
  163. amounts.add(k);
  164. amounts.add(l);
  165. int score = score(amounts);
  166. if ((getCalories(amounts) == 500) && (score > maxScore)) {
  167. maxScore = score;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. return maxScore;
  175. }
  176.  
  177. }
  178.  
Success #stdin #stdout 1.12s 320704KB
stdin
Standard input is empty
stdout
Max Cookie Score: 18965440 Calculation took: 0.494s
Max Cookie Score with 500 calories: 15862900 Calculation took: 0.5s