Advertisement
Guest User

AoC_Day16

a guest
Dec 16th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. package com.adventofcode.day16;
  2.  
  3. public class DragonChecksum {
  4.  
  5.     public static void main(String[] args) {
  6.         String input = "01110110101001000";
  7.         int driveLength1 = 272;
  8.         int driveLength2 = 35651584;
  9.        
  10.         String data1 = driveData(input, driveLength1);
  11.         String checksum1 = driveChecksum(data1);               
  12.         System.out.println("First checksum: " + checksum1);
  13.                
  14.         String data2 = driveData(input, driveLength2);
  15.         String checksum2 = driveChecksum(data2);               
  16.         System.out.println("Second checksum: " + checksum2);
  17.                
  18.     }
  19.    
  20.     private static String driveChecksum(String input) {
  21.         StringBuilder sb = new StringBuilder();
  22.        
  23.         for (int i = 0; i < input.length(); i+=2) {
  24.             sb.append(input.charAt(i) == input.charAt(i + 1) ? '1' : '0');
  25.         }
  26.        
  27.         String checksum = sb.toString();
  28.        
  29.         if(checksum.length() % 2 == 0) {
  30.             checksum = driveChecksum(checksum);
  31.         }
  32.        
  33.         return checksum;
  34.  
  35.     }
  36.  
  37.     public static String driveData(String a, int driveLength) {
  38.        
  39.         String b = new StringBuffer(a).reverse().toString();
  40.  
  41.         b = b.replace('0', 'x');
  42.         b = b.replace('1', '0');
  43.         b = b.replace('x', '1');
  44.        
  45.         String toReturn = a + '0' + b;
  46.        
  47.         if(toReturn.length() < driveLength) {
  48.             toReturn = driveData(toReturn, driveLength);
  49.         }
  50.        
  51.         toReturn = toReturn.substring(0, driveLength);
  52.        
  53.         return toReturn;
  54.        
  55.     }
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement