Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active December 17, 2017 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snarkbait/bf006b0dc247bba1a00acee50950b1e1 to your computer and use it in GitHub Desktop.
Save snarkbait/bf006b0dc247bba1a00acee50950b1e1 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 17
package Advent2017;
import util.AdventOfCode;
import util.ArrayUtils;
import java.util.List;
public class Day17 extends AdventOfCode {
private int steps = 349;
private final int ITERATIONS_PART1 = 2017;
private final int ITERATIONS_PART2 = 50_000_000;
private Integer[] buffer = { 0 };
private int position = 0;
private int num = 1;
public Day17(List<String> input) {
super(input);
}
private void step() {
position = (position + steps) % buffer.length;
growAndMove();
}
private void growAndMove() {
buffer = ArrayUtils.insert(buffer, num++, ++position );
}
@Override
public Object part1() {
while (num <= ITERATIONS_PART1) {
step();
}
return buffer[position + 1];
}
/**
* If you print out the first 20 or so of the arrays for part 1
* after each iteration, you will see that position 0 always stays 0.
* So, for part two we want the second value. Rebuilding the array everytime
* will take too long so...
* Notice that the number after the zero will ONLY CHANGE IF THE NEXT POSITION
* LANDS ON ZERO, and the array grows by one every time. So we just do the iterations,
* and just get the current position plus the step length, MODULUS the
* current iteration number. This gives us the position of the next insertion point, and
* the current iterations value will be the next number inserted in the second array
* place.
* @return value at the second array location
*/
@Override
public Object part2() {
position = 0;
int second = 0;
for (int i = 1; i <= ITERATIONS_PART2; i++) {
position = (position + steps) % i;
if (position == 0) second = i;
position++;
}
return second;
}
@Override
public void parse() { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment