Adding Day 2 Solution

This commit is contained in:
Brandon Scott 2017-12-11 22:10:57 -06:00
parent 5b3d4ca1f7
commit bd8c9744da
1 changed files with 59 additions and 0 deletions

59
csharp/Day2Solution.cs Normal file
View File

@ -0,0 +1,59 @@
using System;
using System.IO;
namespace AOC2015
{
class Day2Solution : Solution
{
public override int Day => 2;
public override string Name => "Day 2: I Was Told There Would Be No Math";
public override void Run()
{
string[] input = File.ReadAllLines("Input\\Day2Input.txt");
Console.WriteLine($"Part 1 Answer Example 1: {Part1(new string[] { "2x3x4" })}");
Console.WriteLine($"Part 1 Answer Example 2: {Part1(new string[] { "1x1x10" })}");
Console.WriteLine($"Part 1 Answer: {Part1(input)}");
Console.WriteLine($"Part 2 Answer Example 1: {Part2(new string[] { "2x3x4" })}");
Console.WriteLine($"Part 2 Answer Example 2: {Part2(new string[] { "1x1x10" })}");
Console.WriteLine($"Part 2 Answer: {Part2(input)}");
}
private int Part1(string[] input)
{
int result = 0;
for (int i = 0; i < input.Length; i++)
{
int[] args = Array.ConvertAll(input[i].Split('x'), s => int.Parse(s));
int length = args[0], width = args[1], height = args[2];
int area = 2 * length * width + 2 * width * height + 2 * height * length;
int extra = Math.Min(length * width, Math.Min(width * height, height * length));
result += area + extra;
}
return result;
}
private int Part2(string[] input)
{
int result = 0;
for (int i = 0; i < input.Length; i++)
{
int[] args = Array.ConvertAll(input[i].Split('x'), s => int.Parse(s));
int length = args[0], width = args[1], height = args[2];
int ribbonLength = Math.Min(length * 2 + width * 2, Math.Min(width * 2 + height * 2, length * 2 + height * 2));
int bowLength = length * width * height;
result += ribbonLength + bowLength;
}
return result;
}
}
}