Advertisement
SoundGuyChris

Advent of Code, Day 2: I Was Told There Would Be No Math

Dec 12th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace ElvesWrappingPaper
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //input file management
  11.             int lineCounter = 0;
  12.             string currentLine;
  13.  
  14.             int length = 0, width = 0, height = 0, totalSurfaceArea = 0, totalRibbonLength = 0;
  15.  
  16.             // Read the file and display it line by line.
  17.             System.IO.StreamReader file = new System.IO.StreamReader("Input.txt");
  18.  
  19.             //process
  20.             while ((currentLine = file.ReadLine()) != null)
  21.             {
  22.                 //split values from the current line, assign them
  23.                 string[] values = currentLine.Split('x');
  24.                 length = Int32.Parse(values[0]);
  25.                 width = Int32.Parse(values[1]);
  26.                 height = Int32.Parse(values[2]);
  27.  
  28.                 //find the area
  29.                 int currentBoxArea = (2 * length * width) + (2 * width * height) + (2 * height * length);
  30.  
  31.                 //find the smallest side
  32.                 int[] min = { length * width, width * height, length * height };
  33.                 int currentBoxMin = min.Min();
  34.                
  35.                 //find the smallest perimeter
  36.                 int[] ribbons = { 2 * length + 2 * width, 2 * width + 2 * height, 2 * height + 2 * length };
  37.                 int thisBoxRibbonPerimeter = ribbons.Min();
  38.  
  39.                 //find the area
  40.                 int thisBoxRibbonArea = length * width * height;
  41.  
  42.                 //Total up each box (these could be made into one line each, but I wanted to summarize each box)
  43.                 int thisBoxTotalRibbon = thisBoxRibbonPerimeter + thisBoxRibbonArea;
  44.                 totalRibbonLength += thisBoxTotalRibbon;
  45.  
  46.                 int thisBoxTotal = currentBoxArea + currentBoxMin;
  47.                 totalSurfaceArea += thisBoxTotal;
  48.  
  49.                 //summarize each box
  50.                 Console.WriteLine("BOX #:{0}\n--Length: {1}\n--Width: {2}\n--Height: {3}\n--Box area: {4}\n--Mininum area: {5}\n--Box Total Paper Requirement: {6}\n--Box Ribbon Length: {7}\n--Box Area: {8}\n--Box's Total Ribbon Requirement: {9}\n-----------", lineCounter+1, length, width, height, currentBoxArea, currentBoxMin, thisBoxTotal, thisBoxRibbonPerimeter, thisBoxRibbonArea, thisBoxTotalRibbon);
  51.  
  52.                 lineCounter++;
  53.             }
  54.            
  55.             //Display the total
  56.             Console.WriteLine("Total Wrapping Paper Needed: {0}\n Total Ribbon Needed: {1}", totalSurfaceArea, totalRibbonLength);
  57.             file.Close();
  58.  
  59.             // Suspend the screen.
  60.             Console.ReadLine();
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement