Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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<banks.length; i++) {
- if (banks[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<banks.length; i++)
- neu.values[i] = banks[i];
- if(anker == null) {
- anker = neu;
- lauf = neu;
- } else {
- lauf.next = neu;
- lauf = lauf.next;
- }
- // increase counter after each loop iteration
- counter++;
- }
- System.out.println("Loops for Part 1: " + counter);
- //*******************************************//
- // ========== Day 5 - Part 2 ========== //
- //*******************************************//
- // copy the previous exsisting result into a new arrwa
- int[] banken = new int[16];
- for (int i=0; i<banks.length; i++)
- banken[i] = banks[i];
- //reset counter
- counter = 0;
- // loop until previous result is found again
- do {
- //reset value location each loop
- maxlocation = 0;
- //get the highest values and its location
- for (int i=0; i<banks.length; i++) {
- if (banks[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<banks.length; i++) {
- if (liste.values[i] == banks[i])
- hit++;
- //when all values are hit, return true
- if(hit == 16)
- return true;
- }
- //go to the next item of the list
- liste = liste.next;
- }
- }
- return false;
- }
- //method for part 2 - checking if newly calculated result matches result of day 2
- static boolean wiederGesehen(int[] banken, int[] banks) {
- // variable to track matches within the list
- int hit = 0;
- // loop through result from part 1 and compare with new calcualted result
- for (int i=0; i<banks.length; i++) {
- //increase hit-counter for each value matched
- if (banken[i] == banks[i])
- hit++;
- //when all values are hit, return true
- if(hit == 16)
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment