Puzzle description: input is dimensions (length, width, heigth) of various presents.
Example:
2x3x1
5x9x10
1x40x2
Part 1: determine the area of paper needed to wrap the gifts, wich is equal to 2*(l*w + w*h + l*h) + the smallest between l*w, w*h and l*h
for each gift.
Part 2: determine the length of ribbon needed to wrap the gift, wich is equal to l*w*h + smallest perimeter
for each gift.
I learned about aoc recently and decided to start from 2015. Below is my code from the day 2 (problem link). How can I improve it? Is it too long?
import java.io.*;
import java.util.*;
public class adventofcode2 {
public static void main(String[] args){
System.out.println("input: ");
Scanner input = new Scanner(System.in);
String line;
String text = "";
while (!(line = input.nextLine()).isEmpty()){
text += line + "\n";
}
calcTotal(text);
System.out.println("Paper area used - " + TotalPaper);
System.out.println("Ribbon length used - " + TotalRibbon);
}
/**
* 1 - Calculates the area of paper necessary to make the presents wrapping
* 2 - Calculates the length of ribbon necessary to the wrapping
* @param presentsText list of strings in the style numberxnumberxnumber
*/
private static void calcTotal(String presentsText){
String[] presents = presentsText.split("\n");
for (String present : presents){
calcEachPresent(present.split("x"));
}
}
/**
* Iteration inside the calcTotal method
* @param presentText array of each present string written in
* the style numberxnumberxnumber
*/
private static void calcEachPresent(String[] presentText){
//Definition of the three dimensions of the present.
int l = Integer.parseInt(presentText[0]), w = Integer.parseInt(presentText[1]), h = Integer.parseInt(presentText[2]);
//Definition of the area of the sides in the present.
int[] d = {l*w,w*h,h*l};
int min = getMin(d);
int area = 2*(d[0] + d[1] + d[2]) + min;
int ribbon = (l*w*h) + smallestPerimeter(l,w,h);
TotalPaper += area;
TotalRibbon += ribbon;
}
/**Get the smallest area to use in the area calculation
*
* @param num array of numbers
* @return smallest number in an array
*/
private static int getMin(int[] num){
int min = num[0];
for (int n : num){
if(n < min) min = n;
}
return min;
}
/**Get smallest perimeter to use in ribbon length calculation
*
* @param l length of present
* @param w width of present
* @param h heigth of present
* @return smallest perimeter
*/
private static int smallestPerimeter(int l,int w,int h){
int[] num = {l,w,h};
Arrays.sort(num);
return 2*(num[0] + num[1]);
}
/**
* VARIABLES TO RETURN IN THE MAIN METHOD
*/
private static int TotalPaper = 0;
private static int TotalRibbon = 0;
}