-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoC2023_09.java
More file actions
73 lines (61 loc) · 2.28 KB
/
AoC2023_09.java
File metadata and controls
73 lines (61 loc) · 2.28 KB
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
70
71
72
73
import static com.github.pareronia.aoc.itertools.IterTools.zip;
import com.github.pareronia.aoc.ListUtils;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
@SuppressWarnings({"PMD.ClassNamingConventions", "PMD.NoPackage"})
public final class AoC2023_09 extends SolutionBase<List<List<Integer>>, Integer, Integer> {
private AoC2023_09(final boolean debug) {
super(debug);
}
public static AoC2023_09 create() {
return new AoC2023_09(false);
}
public static AoC2023_09 createDebug() {
return new AoC2023_09(true);
}
@Override
protected List<List<Integer>> parseInput(final List<String> inputs) {
return inputs.stream()
.map(line -> Arrays.stream(line.split(" ")).map(Integer::valueOf).toList())
.toList();
}
private int solve(final List<Integer> lineIn) {
List<Integer> line = new ArrayList<>(lineIn);
final Deque<Integer> tails = new ArrayDeque<>(List.of(line.getLast()));
while (!line.stream().allMatch(tails.peekLast()::equals)) {
line =
zip(line, line.subList(1, line.size())).stream()
.map(z -> z.second() - z.first())
.toList();
tails.addLast(line.getLast());
}
return tails.stream().mapToInt(Integer::valueOf).sum();
}
@Override
public Integer solvePart1(final List<List<Integer>> input) {
return input.stream().mapToInt(this::solve).sum();
}
@Override
public Integer solvePart2(final List<List<Integer>> input) {
return input.stream().map(ListUtils::reversed).mapToInt(this::solve).sum();
}
@Samples({
@Sample(method = "part1", input = TEST, expected = "114"),
@Sample(method = "part2", input = TEST, expected = "2"),
})
public static void main(final String[] args) throws Exception {
create().run();
}
private static final String TEST =
"""
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
""";
}