Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active December 5, 2017 06:15
Show Gist options
  • Save snarkbait/08300394caef4ae36d01762d4d8b7460 to your computer and use it in GitHub Desktop.
Save snarkbait/08300394caef4ae36d01762d4d8b7460 to your computer and use it in GitHub Desktop.
Advent of Code 2017 Day 5
package Advent2017;
import util.FileIO;
import java.util.List;
public class Day5 {
private static int part1(int[] nums) {
int current = 0;
int steps = 0;
while (current < nums.length && current >= 0) {
int jump = nums[current]++;
current += jump;
steps++;
}
return steps;
}
private static int part2(int[] nums) {
int current = 0;
int steps = 0;
while (current < nums.length && current >= 0) {
int jump = nums[current];
if (jump >= 3) {
nums[current]--;
} else {
nums[current]++;
}
current += jump;
steps++;
}
return steps;
}
public static void main(String[] args) {
List<String> input = FileIO.getAOCInputForDay(2017, 5, FileIO.SESSION_ID);
Timer.startTimer();
int[] nums = input.stream()
.mapToInt(Integer::parseInt)
.toArray();
System.out.println("Part 1: " + part1(nums.clone()));
System.out.println("Part 2: " + part2(nums));
System.out.println(Timer.endTimer());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment