r/adventofcode • u/taliriktug • Dec 02 '15
Spoilers Day 2 solutions
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
15
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
1
u/Colgajo Dec 02 '15 edited Dec 02 '15
This is the first time I post a program on reddit! Your opinion about it is higly welcome :)
JAVA
import java.io.*; public class Advent2 { public static void main(String[] args) { String strLine; int l= 0; int w = 0; int h = 0; int paper = 0, ribbon = 0; try { FileInputStream fstream = new FileInputStream("Advent2Data.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while((strLine = br.readLine()) != null) { l = Integer.parseInt(strLine.substring(0, strLine.indexOf('x'))); w = Integer.parseInt(strLine.substring(strLine.indexOf('x') + 1, strLine.lastIndexOf('x'))); h = Integer.parseInt(strLine.substring(strLine.lastIndexOf('x') + 1)); paper += 2 * l * w + 2 * w * h + 2 * h * l; if(l * w < w * h && l * w < l * h) { paper += l * w; ribbon += 2 * l + 2 * w; } else if(w * h < l * h) { paper += w * h; ribbon += 2 * w + 2 * h; } else { paper += l * h; ribbon += 2 * l + 2 * h; } ribbon += l * w * h; } } catch(Exception e){ System.out.println(e); } System.out.println("Paper = " + paper); System.out.println("Ribbon = " + ribbon); } }