-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2015_02.java
88 lines (72 loc) · 2.73 KB
/
AoC2015_02.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import static java.util.stream.Collectors.toList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public final class AoC2015_02
extends SolutionBase<List<AoC2015_02.Present>, Integer, Integer> {
private AoC2015_02(final boolean debug) {
super(debug);
}
public static AoC2015_02 create() {
return new AoC2015_02(false);
}
public static AoC2015_02 createDebug() {
return new AoC2015_02(true);
}
@Override
protected List<Present> parseInput(final List<String> inputs) {
return inputs.stream().map(Present::fromInput).collect(toList());
}
@Override
public Integer solvePart1(final List<Present> input) {
return input.stream()
.mapToInt(Present::calculateRequiredArea)
.sum();
}
@Override
public Integer solvePart2(final List<Present> input) {
return input.stream()
.mapToInt(Present::calculateRequiredLength)
.sum();
}
@Override
@Samples({
@Sample(method = "part1", input = TEST1, expected = "58"),
@Sample(method = "part1", input = TEST2, expected = "43"),
@Sample(method = "part2", input = TEST1, expected = "34"),
@Sample(method = "part2", input = TEST2, expected = "14"),
})
public void samples() {
}
public static void main(final String[] args) throws Exception {
AoC2015_02.create().run();
}
private static final String TEST1 = "2x3x4";
private static final String TEST2 = "1x1x10";
record Present(int length, int width, int height) {
public static Present fromInput(final String s) {
final int[] sp = Stream.of(s.split("x"))
.mapToInt(Integer::parseInt).toArray();
return new Present(sp[0], sp[1], sp[2]);
}
public int calculateRequiredArea() {
final int[] sides = {
2 * this.length * this.width,
2 * this.width * this.height,
2 * this.height * this.length};
return Arrays.stream(sides).sum()
+ Arrays.stream(sides).min().getAsInt() / 2;
}
public int calculateRequiredLength() {
final int[] circumferences = {
2 * (this.length + this.width),
2 * (this.width + this.height),
2 * (this.height + this.length)};
return Arrays.stream(circumferences).min().getAsInt()
+ this.length * this.width * this.height;
}
}
}