NilsKirchhoff

AoC 2017 - Day 5

Dec 5th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. public class Day5 {
  2.    
  3.     public static void main(String[] args) {
  4.    
  5.     //*******************************************//
  6.     // ==========   Day 5 - Part 1    ========== //    
  7.     //*******************************************//
  8.        
  9.     // my puzzel input
  10.     String maze = "0\r\n" +
  11.             "3\r\n" +
  12.             "0\r\n" +
  13.             "1\r\n" +
  14.             "-3\r\n";
  15.    
  16.     // splitting the puzzel input into lines
  17.     String[] lines = maze.split("\r\n|\r|\n");
  18.    
  19.     //  creating an integer array with all the number from the line string
  20.     int[] trampolin = new int[lines.length];
  21.    
  22.     for (int i=0; i<lines.length; i++) {
  23.         for (int j=0; j<lines[i].length(); j++) {
  24.             trampolin[i] = Integer.parseInt(lines[i]);
  25.         }
  26.     }
  27.    
  28.     // declaring int variable for jump search
  29.     int position = 0, erhoeher = 0, spruenge = 0;
  30.     // running the loop until the position is outside the maze
  31.     while (position < trampolin.length) {
  32.         // saving the position of the jump
  33.         erhoeher = position;
  34.         // jump to new position
  35.         position = position + trampolin[position];
  36.         // raising the jump offset
  37.         trampolin[erhoeher] = trampolin[erhoeher] + 1;
  38.         // counting the number of jumps
  39.         spruenge++;
  40.     }
  41.    
  42.     // printing out the number of jumps needed
  43.     System.out.println(spruenge);
  44.    
  45.     //*******************************************//
  46.     // ==========   Day 5 - Part 2    ========== //
  47.     //*******************************************//
  48.    
  49.     // resetting variable for new problem
  50.     for (int i=0; i<lines.length; i++) {
  51.         for (int j=0; j<lines[i].length(); j++) {
  52.             trampolin[i] = Integer.parseInt(lines[i]);
  53.         }
  54.     }
  55.     position = 0; erhoeher = 0; spruenge = 0;
  56.    
  57.     // running the loop until the position is outside the maze
  58.     while (position < trampolin.length) {
  59.         // saving the position of the jump
  60.         erhoeher = position;
  61.         // jump to new position
  62.         position = position + trampolin[position];
  63.         // checking if offset is equal or higher then 3 and raising or lowering it dependent on the outcome
  64.         if (trampolin[erhoeher] < 3) {
  65.             trampolin[erhoeher] = trampolin[erhoeher] + 1;
  66.         } else {
  67.             trampolin[erhoeher] = trampolin[erhoeher] - 1;
  68.         }
  69.         // counting the number of jumps
  70.         spruenge++;
  71.     }  
  72.    
  73.     // printing out the number of jumps needed
  74.     System.out.println(spruenge);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment