public class Day5 {
public static void main(String[] args) {
//*******************************************//
// ========== Day 5 - Part 1 ========== //
//*******************************************//
// my puzzel input
String maze = "0\r\n" +
"3\r\n" +
"0\r\n" +
"1\r\n" +
"-3\r\n";
// splitting the puzzel input into lines
String[] lines = maze.split("\r\n|\r|\n");
// creating an integer array with all the number from the line string
int[] trampolin = new int[lines.length];
for (int i=0; i<lines.length; i++) {
for (int j=0; j<lines[i].length(); j++) {
trampolin[i] = Integer.parseInt(lines[i]);
}
}
// declaring int variable for jump search
int position = 0, erhoeher = 0, spruenge = 0;
// running the loop until the position is outside the maze
while (position < trampolin.length) {
// saving the position of the jump
erhoeher = position;
// jump to new position
position = position + trampolin[position];
// raising the jump offset
trampolin[erhoeher] = trampolin[erhoeher] + 1;
// counting the number of jumps
spruenge++;
}
// printing out the number of jumps needed
System.out.println(spruenge);
//*******************************************//
// ========== Day 5 - Part 2 ========== //
//*******************************************//
// resetting variable for new problem
for (int i=0; i<lines.length; i++) {
for (int j=0; j<lines[i].length(); j++) {
trampolin[i] = Integer.parseInt(lines[i]);
}
}
position = 0; erhoeher = 0; spruenge = 0;
// running the loop until the position is outside the maze
while (position < trampolin.length) {
// saving the position of the jump
erhoeher = position;
// jump to new position
position = position + trampolin[position];
// checking if offset is equal or higher then 3 and raising or lowering it dependent on the outcome
if (trampolin[erhoeher] < 3) {
trampolin[erhoeher] = trampolin[erhoeher] + 1;
} else {
trampolin[erhoeher] = trampolin[erhoeher] - 1;
}
// counting the number of jumps
spruenge++;
}
// printing out the number of jumps needed
System.out.println(spruenge);
}
}
Comments