|
| 1 | +package by.andd3dfx.game; |
| 2 | + |
| 3 | +import lombok.SneakyThrows; |
| 4 | +import org.apache.commons.lang3.math.NumberUtils; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * <pre> |
| 16 | + * --- Day 2: I Was Told There Would Be No Math --- |
| 17 | + * |
| 18 | + * The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need. |
| 19 | + * |
| 20 | + * Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for each present: the area of the smallest side. |
| 21 | + * |
| 22 | + * For example: |
| 23 | + * |
| 24 | + * A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet. |
| 25 | + * A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet. |
| 26 | + * |
| 27 | + * All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order? |
| 28 | + * </pre> |
| 29 | + * |
| 30 | + * @see <a href="https://youtu.be/RRyUtaNnntI">Video solution</a> |
| 31 | + */ |
| 32 | +public class IWasToldThereWouldBeNoMath { |
| 33 | + |
| 34 | + public static int calculate(List<String> dimensionsList) { |
| 35 | + int result = 0; |
| 36 | + for (var dimensions : dimensionsList) { |
| 37 | + var dimensionStrings = dimensions.split("x"); |
| 38 | + var sortedDimensions = Arrays.stream(dimensionStrings).map(NumberUtils::toInt) |
| 39 | + .sorted() |
| 40 | + .toList(); |
| 41 | + result += fullArea(sortedDimensions) + areaOfSmallestSide(sortedDimensions); |
| 42 | + } |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + private static int fullArea(List<Integer> dimension) { |
| 47 | + return 2 * (dimension.get(0) * dimension.get(1) |
| 48 | + + dimension.get(1) * dimension.get(2) |
| 49 | + + dimension.get(2) * dimension.get(0)); |
| 50 | + } |
| 51 | + |
| 52 | + private static int areaOfSmallestSide(List<Integer> dimension) { |
| 53 | + return dimension.get(0) * dimension.get(1); |
| 54 | + } |
| 55 | + |
| 56 | + @SneakyThrows |
| 57 | + public static void main(String[] args) { |
| 58 | + var lines = read("/game/i-was-told-there-would-be-no-math.txt"); |
| 59 | + var result = calculate(lines); |
| 60 | + System.out.println(result); |
| 61 | + } |
| 62 | + |
| 63 | + private static List<String> read(String filePathName) throws IOException { |
| 64 | + InputStream inputStream = IWasToldThereWouldBeNoMath.class.getResourceAsStream(filePathName); |
| 65 | + var result = new ArrayList<String>(); |
| 66 | + try (var br = new BufferedReader(new InputStreamReader(inputStream))) { |
| 67 | + String line; |
| 68 | + while ((line = br.readLine()) != null) { |
| 69 | + result.add(line); |
| 70 | + } |
| 71 | + } |
| 72 | + return result; |
| 73 | + } |
| 74 | +} |
0 commit comments