Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * @author /u/Philboyd_Studge on 12/14/2015.
- */
- public class Advent15 {
- static Random rand = new Random();
- public static Integer[] getRandom() {
- List<Integer> values= new ArrayList<>();
- getRandom(values, 0);
- Collections.shuffle(values);
- Integer[] retval = new Integer[4];
- return values.toArray(retval);
- }
- public static List<Integer> getRandom(List<Integer> values, int total) {
- if (values.size()==4) return values;
- if (values.size()==3) {
- values.add(100 - total);
- } else {
- int temp = 0;
- if (97 - total > 0) {
- temp = rand.nextInt(97 - total) + 1;
- }
- values.add(temp);
- total += temp;
- }
- return getRandom(values, total);
- }
- public static void main(String[] args) {
- boolean part2 = false;
- List<String[]> input = FileIO.getFileLinesSplit("advent15.txt", "[^-0-9]+");
- List<Integer[]> ingredients = new ArrayList<>();
- for (String[] each : input) {
- // get rid of first element as my regex didn't completely work
- each = Arrays.copyOfRange(each,1,each.length);
- Integer[] properties = FileIO.StringArrayToInteger(each);
- ingredients.add(properties);
- }
- Integer[] test;
- int n = 0;
- int maxTotal = Integer.MIN_VALUE;
- while(n++ < 1000000) {
- test = getRandom();
- int calories = 0;
- int[] subtotals = new int[4];
- int total = 1;
- for (int i=0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- subtotals[i] += test[j] * ingredients.get(j)[i];
- }
- if (part2) {
- calories += test[i] * ingredients.get(i)[4];
- }
- if (subtotals[i] < 0) subtotals[i] = 0;
- }
- for (int each : subtotals) total *= each;
- if (total > maxTotal) {
- if (part2) {
- if (calories == 500) {
- maxTotal = total;
- }
- } else {
- maxTotal = total;
- }
- }
- }
- System.out.println(maxTotal);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment