-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay17.java
53 lines (42 loc) · 1.2 KB
/
Day17.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.sbaars.adventofcode.year17.days;
import com.sbaars.adventofcode.year17.Day2017;
import java.util.ArrayList;
import java.util.List;
public class Day17 extends Day2017 {
public Day17() {
super(17);
}
public static void main(String[] args) {
new Day17().printParts();
}
@Override
public Object part1() {
int steps = Integer.parseInt(day().trim());
List<Integer> buffer = new ArrayList<>();
buffer.add(0);
int currentPos = 0;
for (int i = 1; i <= 2017; i++) {
currentPos = ((currentPos + steps) % buffer.size()) + 1;
buffer.add(currentPos, i);
}
// Find the value after 2017
int index2017 = buffer.indexOf(2017);
return buffer.get((index2017 + 1) % buffer.size());
}
@Override
public Object part2() {
int steps = Integer.parseInt(day().trim());
int currentPos = 0;
int valueAfterZero = 0;
int bufferSize = 1;
for (int i = 1; i <= 50_000_000; i++) {
currentPos = ((currentPos + steps) % bufferSize) + 1;
// If we're inserting at position 1 (after 0), update valueAfterZero
if (currentPos == 1) {
valueAfterZero = i;
}
bufferSize++;
}
return valueAfterZero;
}
}