Guest User

Untitled

a guest
Dec 17th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  * @author /u/Philboyd_Studge on 12/14/2015.
  5.  */
  6. public class Advent15 {
  7.  
  8.     static Random rand = new Random();
  9.  
  10.     public static Integer[] getRandom() {
  11.         List<Integer> values= new ArrayList<>();
  12.         getRandom(values, 0);
  13.         Collections.shuffle(values);
  14.         Integer[] retval = new Integer[4];
  15.         return values.toArray(retval);
  16.     }
  17.     public static List<Integer> getRandom(List<Integer> values, int total) {
  18.         if (values.size()==4) return values;
  19.         if (values.size()==3) {
  20.             values.add(100 - total);
  21.         } else {
  22.             int temp = 0;
  23.             if (97 - total > 0) {
  24.                 temp = rand.nextInt(97 - total) + 1;
  25.             }
  26.             values.add(temp);
  27.             total += temp;
  28.         }
  29.         return getRandom(values, total);
  30.  
  31.     }
  32.     public static void main(String[] args) {
  33.  
  34.         boolean part2 = false;
  35.  
  36.         List<String[]> input = FileIO.getFileLinesSplit("advent15.txt", "[^-0-9]+");
  37.  
  38.         List<Integer[]> ingredients = new ArrayList<>();
  39.         for (String[] each : input) {
  40.             // get rid of first element as my regex didn't completely work
  41.             each = Arrays.copyOfRange(each,1,each.length);
  42.             Integer[] properties = FileIO.StringArrayToInteger(each);
  43.             ingredients.add(properties);
  44.         }
  45.  
  46.         Integer[] test;
  47.         int n = 0;
  48.         int maxTotal = Integer.MIN_VALUE;
  49.  
  50.  
  51.         while(n++ < 1000000) {
  52.             test = getRandom();
  53.             int calories = 0;
  54.             int[] subtotals = new int[4];
  55.             int total = 1;
  56.             for (int i=0; i < 4; i++) {
  57.                 for (int j = 0; j < 4; j++) {
  58.                     subtotals[i] += test[j] * ingredients.get(j)[i];
  59.                 }
  60.                 if (part2) {
  61.                     calories += test[i] * ingredients.get(i)[4];
  62.                 }
  63.                 if (subtotals[i] < 0) subtotals[i] = 0;
  64.             }
  65.             for (int each : subtotals) total *= each;
  66.             if (total > maxTotal) {
  67.                 if (part2) {
  68.                     if (calories == 500) {
  69.                         maxTotal = total;
  70.                     }
  71.                 } else {
  72.                     maxTotal = total;
  73.                 }
  74.             }
  75.         }
  76.         System.out.println(maxTotal);
  77.  
  78.  
  79.  
  80.  
  81.  
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment