Advertisement
SadBunny

AOC 2021 Day 6 Part 2 Java

Dec 8th, 2021
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. import java.util.stream.Stream;
  2.  
  3. class AOC2021_6 {
  4.  
  5.     private static long fishCount = 0;
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         long start = System.currentTimeMillis();
  10.  
  11.         String sample = "3,4,3,1,2";
  12.         String input = "2,1,2,1,5,1,5,1,2,2,1,1,5,1,4,4,4,3,1,2,2,3,4,1,1,5,1,1,4,2,5,5,5,1,1,4,5,4,1,1,4,2,1,4,1,2,2,5,1,1,5,1,1,3,4,4,1,2,3,1,5,5,4,1,4,1,2,1,5,1,1,1,3,4,1,1,5,1,5,1,1,5,1,1,4,3,2,4,1,4,1,5,3,3,1,5,1,3,1,1,4,1,4,5,2,3,1,1,1,1,3,1,2,1,5,1,1,5,1,1,1,1,4,1,4,3,1,5,1,1,5,4,4,2,1,4,5,1,1,3,3,1,1,4,2,5,5,2,4,1,4,5,4,5,3,1,4,1,5,2,4,5,3,1,3,2,4,5,4,4,1,5,1,5,1,2,2,1,4,1,1,4,2,2,2,4,1,1,5,3,1,1,5,4,4,1,5,1,3,1,3,2,2,1,1,4,1,4,1,2,2,1,1,3,5,1,2,1,3,1,4,5,1,3,4,1,1,1,1,4,3,3,4,5,1,1,1,1,1,2,4,5,3,4,2,1,1,1,3,3,1,4,1,1,4,2,1,5,1,1,2,3,4,2,5,1,1,1,5,1,1,4,1,2,4,1,1,2,4,3,4,2,3,1,1,2,1,5,4,2,3,5,1,2,3,1,2,2,1,4";
  13.  
  14.         String dataFish = input;
  15.  
  16.         int TTL = 256;
  17.  
  18.         Integer[] fish = Stream.of(dataFish.split(",")).map(Integer::valueOf).toArray(Integer[]::new);
  19.  
  20.         long[] data_ar = new long[8];
  21.  
  22.         for (int data=1; data<=7; data++) {
  23.             fishCount = 1;
  24.             reproduce(data, TTL);
  25.             data_ar[data] = fishCount;
  26.             System.out.println(data + " day-to-repro after " + TTL + " iterations leads to " + data_ar[data] + " fish.");
  27.         }
  28.  
  29.         System.out.println("Counting fish...");
  30.  
  31.         fishCount = 0;
  32.  
  33.         for (int i=0; i<fish.length; i++){
  34.             fishCount += data_ar[fish[i]];
  35.         }
  36.  
  37.         System.out.println("Total fishcount: " + fishCount);
  38.         long finish = System.currentTimeMillis();
  39.         System.out.println("Time in msec: " + (finish - start));
  40.         System.out.println("Time in sec: " + (finish - start) / 1000);
  41.     }
  42.  
  43.     static void reproduce(int daysToReproduce, int rTTL) {
  44.         for (int j = rTTL; j >= 0; j--) {
  45.             if (daysToReproduce == -1) {
  46.                 daysToReproduce = 6;
  47.                 fishCount++;
  48.                 reproduce(8, j);
  49.             }
  50.             daysToReproduce--;
  51.         }
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement