NilsKirchhoff

AoC 2017 - Day 6

Dec 6th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. public class Day6 {
  2.    
  3.     //*******************************************//
  4.     // ==========   Day 5 - Part 1    ========== //    
  5.     //*******************************************//
  6.    
  7.     //class to creat list of previous change iterations
  8.     static class Results {
  9.         int[] values = new int[16];
  10.         Results next = null;
  11.     }
  12.  
  13.     // main method to get the number of loops until a previous result is received
  14.     public static void main(String[] args) {
  15.        
  16.         // question 0   5   10  0   11  14  13  4   11  8   8   7   1   4   12  11
  17.          int[] banks = {0,5,10,0,11,14,13,4,11,8,8,7,1,4,12,11};
  18.  
  19.         //example question
  20.         // int banks[] = {0, 2, 7, 0};
  21.        
  22.         //initializing needed variables
  23.         int max = 0,  maxlocation = 0, counter = 0;
  24.         Results anker = null;
  25.         Results lauf = new Results();
  26.        
  27.         //loop until a previous result is received
  28.         while (gesehen(anker, banks) != true) {
  29.             //reset value location each loop
  30.             maxlocation = 0;
  31.            
  32.             //get the highest values and its location
  33.             for (int i=0; i<banks.length; i++) {
  34.                 if (banks[i] > max) {
  35.                     max = banks[i];
  36.                     maxlocation = i;
  37.                 }
  38.             }
  39.            
  40.             //reset highest value to 0
  41.             banks[maxlocation] = 0;
  42.            
  43.             //increase the location by one
  44.             maxlocation++;
  45.            
  46.             //loop though array and increase values until max value is 0
  47.             while (max != 0) {
  48.                 if (maxlocation < banks.length) {
  49.                 banks[maxlocation]++;
  50.                 max--;
  51.                 maxlocation++;
  52.                 } else {
  53.                     maxlocation = 0;
  54.                     banks[maxlocation]++;
  55.                     max--;
  56.                     maxlocation++;
  57.                 }      
  58.                
  59.             }
  60.                
  61.             //create list of previous results, or attach last result to exsisting list
  62.             Results neu = new Results();
  63.             for (int i=0; i<banks.length; i++)
  64.                 neu.values[i] = banks[i];
  65.                 if(anker == null) {
  66.                     anker = neu;
  67.                     lauf = neu;
  68.                 } else {
  69.                     lauf.next = neu;
  70.                     lauf = lauf.next;
  71.                 }
  72.            
  73.             // increase counter after each loop iteration
  74.             counter++;
  75.         }
  76.        
  77.         System.out.println("Loops for Part 1: " + counter);
  78.        
  79.         //*******************************************//
  80.         // ==========   Day 5 - Part 2    ========== //    
  81.         //*******************************************//
  82.         // copy the previous exsisting result into a new arrwa
  83.         int[] banken = new int[16];
  84.         for (int i=0; i<banks.length; i++)
  85.                 banken[i] = banks[i];
  86.  
  87.         //reset counter
  88.         counter = 0;
  89.        
  90.         // loop until previous result is found again
  91.         do {
  92.                 //reset value location each loop
  93.                 maxlocation = 0;
  94.                
  95.                 //get the highest values and its location
  96.                 for (int i=0; i<banks.length; i++) {
  97.                     if (banks[i] > max) {
  98.                         max = banks[i];
  99.                         maxlocation = i;
  100.                     }
  101.                 }
  102.                
  103.                 //reset highest value to 0
  104.                 banks[maxlocation] = 0;
  105.                 //increase the location by one
  106.                 maxlocation++;
  107.                
  108.                 //loop though array and increase values until max value is 0
  109.                 while (max != 0) {
  110.                     if (maxlocation < banks.length) {
  111.                     banks[maxlocation]++;
  112.                     max--;
  113.                     maxlocation++;
  114.                     } else {
  115.                         maxlocation = 0;
  116.                         banks[maxlocation]++;
  117.                         max--;
  118.                         maxlocation++;
  119.                     }      
  120.                 }
  121.                 // increase counter after each loop iteration
  122.                 counter ++;
  123.         //check if previous result equal the newly calculated result
  124.         } while (wiederGesehen(banken, banks) == false);
  125.        
  126.         System.out.println("Loops for Part 2: " + counter);
  127.     }
  128.    
  129.    
  130.     // method for part 1 - checking if list of previous calculations matches new calculated result
  131.     static boolean gesehen(Results liste, int[] banks) {
  132.         // variable to track matches within the list
  133.         int hit = 0;
  134.        
  135.         //only check if the method received a valif list
  136.         if (liste != null) {
  137.             //loop through list, while it still has new items
  138.             while (liste.next != null) {
  139.                 //reset hit-counter each loop through
  140.                 hit = 0;
  141.                 //loop through the values of the list and check for matches with current result
  142.                 for (int i=0; i<banks.length; i++) {
  143.                     if (liste.values[i] == banks[i])
  144.                         hit++;             
  145.                     //when all values are hit, return true
  146.                     if(hit == 16)
  147.                         return true;
  148.                 }
  149.                 //go to the next item of the list
  150.                 liste = liste.next;
  151.             }
  152.            
  153.         }      
  154.         return  false;
  155.     }
  156.    
  157.     //method for part 2 - checking if newly calculated result matches result of day 2
  158.     static boolean wiederGesehen(int[] banken, int[] banks) {
  159.         // variable to track matches within the list
  160.         int hit = 0;
  161.         // loop through result from part 1 and compare with new calcualted result
  162.         for (int i=0; i<banks.length; i++) {
  163.                 //increase hit-counter for each value matched
  164.                 if (banken[i] == banks[i])
  165.                     hit++; 
  166.                 //when all values are hit, return true
  167.                 if(hit == 16)
  168.                     return true;
  169.             }
  170.         return false;
  171.     }
  172.    
  173. }
Advertisement
Add Comment
Please, Sign In to add comment