-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay2.java
71 lines (56 loc) · 1.5 KB
/
Day2.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
package com.sbaars.adventofcode.year15.days;
import com.sbaars.adventofcode.common.Day;
import com.sbaars.adventofcode.year15.Day2015;
import java.util.function.ToLongFunction;
import static com.sbaars.adventofcode.util.DataMapper.readString;
public class Day2 extends Day2015 {
public Day2() {
super(2);
}
public record Dimension(long l, long w, long h) {
private long[] smallestSides() {
if (l >= w && l >= h) return new long[]{w, h};
if (w >= l && w >= h) return new long[]{l, h};
return new long[]{l, w};
}
private long smallestArea() {
var s = smallestSides();
return s[0] * s[1];
}
private long area() {
return 2 * (l * w + w * h + h * l);
}
private long wrappingPaper() {
return smallestArea() + area();
}
private long sideDistance() {
var s = smallestSides();
return 2 * (s[0] + s[1]);
}
private long volume() {
return l * w * h;
}
private long ribbon() {
return sideDistance() + volume();
}
}
public static void main(String[] args) {
Day d = new Day2();
d.downloadIfNotDownloaded();
d.printParts();
}
@Override
public Object part1() {
return getResult(Dimension::wrappingPaper);
}
@Override
public Object part2() {
return getResult(Dimension::ribbon);
}
private long getResult(ToLongFunction<Dimension> func) {
return dayStream()
.map(s -> readString(s, "%nx%nx%n", Dimension.class))
.mapToLong(func)
.sum();
}
}