Advertisement
Guest User

A java implementation for AdventCode Day 2

a guest
Dec 2nd, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.util.*;
  2. import java.nio.file.Files;
  3. import java.nio.charset.StandardCharsets;
  4. import java.nio.file.Paths;
  5. import java.io.IOException;
  6.  
  7. public class HelloWorld{
  8.  
  9.      public static void main(String []args){
  10.        
  11.         try{
  12.             List<String> boxes = Files.readAllLines(Paths.get("input"), StandardCharsets.UTF_8);
  13.             for(String b : boxes) {
  14.                 BoxList.add(new Box(b.split("x")));
  15.             }
  16.         } catch(IOException e){
  17.             e.printStackTrace();
  18.         }
  19.        
  20.         if(BoxList.size() > 0) {
  21.             System.out.println("Paper needed: " + BoxList.totalPaperNeeded());
  22.             System.out.println("Ribbon needed: " + BoxList.totalRibbonNeeded());
  23.         }
  24.      }
  25. }
  26.  
  27. final class BoxList {
  28.     static List<Box> boxList = new ArrayList<Box>();
  29.    
  30.     public static void add(Box box) {
  31.         boxList.add(box);
  32.     }
  33.    
  34.     public static Integer size() {
  35.         return boxList.size();
  36.     }
  37.    
  38.     public static Integer totalPaperNeeded() {
  39.         Integer totalPaper = 0;
  40.        
  41.         for(Box b : boxList) {
  42.             totalPaper += b.paperNeeded();
  43.         }
  44.        
  45.         return totalPaper;
  46.     }
  47.    
  48.     public static Integer totalRibbonNeeded() {
  49.         Integer totalRibbon = 0;
  50.        
  51.         for(Box b : boxList) {
  52.             totalRibbon += b.ribbonNeeded();
  53.         }
  54.        
  55.         return totalRibbon;
  56.     }
  57. }
  58.  
  59. class Box {
  60.     private Integer l, w, h;
  61.    
  62.     public Box(String[] dimensions) {
  63.         this.l = Integer.parseInt(dimensions[0]);
  64.         this.w = Integer.parseInt(dimensions[1]);
  65.         this.h = Integer.parseInt(dimensions[2]);
  66.     }
  67.    
  68.     public void render() {
  69.         System.out.println("l: " + l + " w: " + w + " h: " + h);
  70.         System.out.println("Paper needed: " + paperNeeded());
  71.         System.out.println("Ribbon needed: " + ribbonNeeded());
  72.     }
  73.    
  74.     public Integer paperNeeded() {
  75.         Integer lw = l * w;
  76.         Integer wh = w * h;
  77.         Integer hl = h * l;
  78.        
  79.         return 2 * (lw + wh + hl) + findMin(lw, wh, hl);
  80.     }
  81.    
  82.     public Integer ribbonNeeded() {
  83.         Integer minPerim = 2 * findMin(l + w, w + h, h + l);
  84.         return minPerim + (l * w * h);
  85.     }
  86.    
  87.     private Integer findMin(Integer a, Integer b, Integer c) {
  88.         return Math.min(a, Math.min(b, c));
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement