|
4 | 4 | import java.util.Collections; |
5 | 5 | import java.util.List; |
6 | 6 | import java.util.Scanner; |
| 7 | +import java.util.function.ToIntFunction; |
7 | 8 | import org.ck.adventofcode.util.AOCSolution; |
8 | 9 | import org.ck.codechallengelib.annotation.Solution; |
9 | 10 |
|
|
20 | 21 | public class Day02 extends AOCSolution { |
21 | 22 | @Override |
22 | 23 | protected void runPartOne(final Scanner in) { |
23 | | - run( |
24 | | - in, |
25 | | - (l, w, h) -> { |
26 | | - final int lw = l * w; |
27 | | - final int lh = l * h; |
28 | | - final int wh = w * h; |
29 | | - |
30 | | - final int min = Math.min(Math.min(lw, lh), wh); |
31 | | - |
32 | | - return 2 * lw + 2 * lh + 2 * wh + min; |
33 | | - }); |
| 24 | + run(in, Day02::calculateWrappingNeeded); |
34 | 25 | } |
35 | 26 |
|
36 | 27 | @Override |
37 | 28 | protected void runPartTwo(final Scanner in) { |
38 | | - run( |
39 | | - in, |
40 | | - (l, w, h) -> { |
41 | | - final List<Integer> lengths = Arrays.asList(l, w, h); |
42 | | - Collections.sort(lengths); |
| 29 | + run(in, Day02::calculateRibbonNeeded); |
| 30 | + } |
43 | 31 |
|
44 | | - final int s = lengths.get(0); |
45 | | - final int m = lengths.get(1); |
| 32 | + private void run(final Scanner in, final MaterialNeedCalculator materialNeedCalculator) { |
| 33 | + int needed = |
| 34 | + in.tokens() |
| 35 | + .map(line -> line.split("x")) |
| 36 | + .mapToInt(calculateMaterialNeeded(materialNeedCalculator)) |
| 37 | + .sum(); |
46 | 38 |
|
47 | | - return 2 * s + 2 * m + l * w * h; |
48 | | - }); |
| 39 | + print(needed); |
49 | 40 | } |
50 | 41 |
|
51 | | - private void run(final Scanner in, final MaterialNeedCalculator materialNeedCalculator) { |
52 | | - int needed = 0; |
| 42 | + private static int calculateWrappingNeeded(final int l, final int w, final int h) { |
| 43 | + final int lw = l * w; |
| 44 | + final int lh = l * h; |
| 45 | + final int wh = w * h; |
53 | 46 |
|
54 | | - while (in.hasNextLine()) { |
55 | | - final String[] dimensions = in.nextLine().split("x"); |
| 47 | + final int min = Math.min(Math.min(lw, lh), wh); |
56 | 48 |
|
57 | | - final int l = Integer.parseInt(dimensions[0]); |
58 | | - final int w = Integer.parseInt(dimensions[1]); |
59 | | - final int h = Integer.parseInt(dimensions[2]); |
| 49 | + return 2 * lw + 2 * lh + 2 * wh + min; |
| 50 | + } |
60 | 51 |
|
61 | | - needed += materialNeedCalculator.get(l, w, h); |
62 | | - } |
| 52 | + private static int calculateRibbonNeeded(final int l, final int w, final int h) { |
| 53 | + final List<Integer> lengths = Arrays.asList(l, w, h); |
| 54 | + Collections.sort(lengths); |
63 | 55 |
|
64 | | - print(needed); |
| 56 | + final int s = lengths.get(0); |
| 57 | + final int m = lengths.get(1); |
| 58 | + |
| 59 | + return 2 * s + 2 * m + l * w * h; |
| 60 | + } |
| 61 | + |
| 62 | + private static ToIntFunction<String[]> calculateMaterialNeeded( |
| 63 | + final MaterialNeedCalculator materialNeedCalculator) { |
| 64 | + return dimensions -> |
| 65 | + materialNeedCalculator.get( |
| 66 | + Integer.parseInt(dimensions[0]), |
| 67 | + Integer.parseInt(dimensions[1]), |
| 68 | + Integer.parseInt(dimensions[2])); |
65 | 69 | } |
66 | 70 |
|
67 | 71 | private interface MaterialNeedCalculator { |
|
0 commit comments