public class Day6 { //*******************************************// // ========== Day 5 - Part 1 ========== // //*******************************************// //class to creat list of previous change iterations static class Results { int[] values = new int[16]; Results next = null; } // main method to get the number of loops until a previous result is received public static void main(String[] args) { // question 0 5 10 0 11 14 13 4 11 8 8 7 1 4 12 11 int[] banks = {0,5,10,0,11,14,13,4,11,8,8,7,1,4,12,11}; //example question // int banks[] = {0, 2, 7, 0}; //initializing needed variables int max = 0, maxlocation = 0, counter = 0; Results anker = null; Results lauf = new Results(); //loop until a previous result is received while (gesehen(anker, banks) != true) { //reset value location each loop maxlocation = 0; //get the highest values and its location for (int i=0; i max) { max = banks[i]; maxlocation = i; } } //reset highest value to 0 banks[maxlocation] = 0; //increase the location by one maxlocation++; //loop though array and increase values until max value is 0 while (max != 0) { if (maxlocation < banks.length) { banks[maxlocation]++; max--; maxlocation++; } else { maxlocation = 0; banks[maxlocation]++; max--; maxlocation++; } } //create list of previous results, or attach last result to exsisting list Results neu = new Results(); for (int i=0; i max) { max = banks[i]; maxlocation = i; } } //reset highest value to 0 banks[maxlocation] = 0; //increase the location by one maxlocation++; //loop though array and increase values until max value is 0 while (max != 0) { if (maxlocation < banks.length) { banks[maxlocation]++; max--; maxlocation++; } else { maxlocation = 0; banks[maxlocation]++; max--; maxlocation++; } } // increase counter after each loop iteration counter ++; //check if previous result equal the newly calculated result } while (wiederGesehen(banken, banks) == false); System.out.println("Loops for Part 2: " + counter); } // method for part 1 - checking if list of previous calculations matches new calculated result static boolean gesehen(Results liste, int[] banks) { // variable to track matches within the list int hit = 0; //only check if the method received a valif list if (liste != null) { //loop through list, while it still has new items while (liste.next != null) { //reset hit-counter each loop through hit = 0; //loop through the values of the list and check for matches with current result for (int i=0; i