Guest User

Untitled

a guest
Dec 17th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4.  * @author /u/Philboyd_Studge on 12/16/2015.
  5.  */
  6. public class Advent17 {
  7.  
  8.     public static int count(int total, int[] array) {
  9.         if (total == 0) return 1;
  10.         if (array.length == 0) return 0;
  11.         if (total < 0) return 0;
  12.  
  13.         int test = array[0];
  14.         array = Arrays.copyOfRange(array, 1, array.length);
  15.         return count(total - test, array) + count(total, array);
  16.     }
  17.  
  18.     public static int count(int total, int[] array, int number) {
  19.         if (total == 0 && number == 0) return 1;
  20.         if (array.length == 0) return 0;
  21.         if (total < 0) return 0;
  22.  
  23.         int test = array[0];
  24.         array = Arrays.copyOfRange(array, 1, array.length);
  25.         return count(total - test, array, number - 1) + count(total, array, number);
  26.     }
  27.     public static void main(String[] args) {
  28.  
  29.         final int TARGET = 150;
  30.         int[] containers = {43, 3, 4, 10, 21, 44, 4, 6, 47, 41, 34, 17, 17,
  31.                             44, 36, 31, 46, 9, 27, 38};
  32.  
  33.         System.out.printf("Part 1 : %d%n", count(TARGET, containers));
  34.  
  35.         int min = 0;
  36.         int combos = 0;
  37.         while (combos == 0) {
  38.             combos = count(TARGET, containers, min++);
  39.         }
  40.  
  41.         System.out.printf("Part 2 : %d%n" , combos);
  42.  
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment