-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2017_05.java
69 lines (58 loc) · 1.8 KB
/
AoC2017_05.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Arrays;
import java.util.List;
import java.util.function.IntUnaryOperator;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public final class AoC2017_05 extends SolutionBase<int[], Integer, Integer> {
private AoC2017_05(final boolean debug) {
super(debug);
}
public static AoC2017_05 create() {
return new AoC2017_05(false);
}
public static AoC2017_05 createDebug() {
return new AoC2017_05(true);
}
@Override
protected int[] parseInput(final List<String> inputs) {
return inputs.stream().mapToInt(Integer::parseInt).toArray();
}
private int countJumps(
final int[] input,
final IntUnaryOperator jumpCalculator
) {
final int[] offsets = Arrays.copyOf(input, input.length);
int cnt = 0;
int i = 0;
while (i < offsets.length) {
final int jump = offsets[i];
offsets[i] = jumpCalculator.applyAsInt(jump);
i += jump;
cnt++;
}
return cnt;
}
@Override
public Integer solvePart1(final int[] input) {
return countJumps(input, jump -> jump + 1);
}
@Override
public Integer solvePart2(final int[] input) {
return countJumps(input, jump -> jump >= 3 ? jump - 1 : jump + 1);
}
@Samples({
@Sample(method = "part1", input = TEST, expected = "5"),
@Sample(method = "part2", input = TEST, expected = "10"),
})
public static void main(final String[] args) throws Exception {
AoC2017_05.create().run();
}
private static final String TEST = """
0
3
0
1
-3
""";
}