Day03 Part 1
This commit is contained in:
@@ -56,6 +56,7 @@
|
||||
<Otherwise />
|
||||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="Claim_Tests.cs" />
|
||||
<Compile Include="Day01_Tests.cs" />
|
||||
<Compile Include="Day02_Tests.cs" />
|
||||
<Compile Include="Day03_Tests.cs" />
|
||||
|
||||
60
AdventOfCode2018.Tests/Claim_Tests.cs
Normal file
60
AdventOfCode2018.Tests/Claim_Tests.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace AdventOfCode2018.Tests
|
||||
{
|
||||
[TestClass()]
|
||||
public class Claim_Tests
|
||||
{
|
||||
#region FromString
|
||||
|
||||
[TestMethod()]
|
||||
public void FromString__Test1()
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#123 @ 3,2: 5x4");
|
||||
|
||||
Assert.AreEqual("#123", claim.ID);
|
||||
Assert.AreEqual(3, claim.Left);
|
||||
Assert.AreEqual(2, claim.Top);
|
||||
Assert.AreEqual(5, claim.Width);
|
||||
Assert.AreEqual(4, claim.Height);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void FromString__Test2()
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#1 @ 1,3: 4x4");
|
||||
|
||||
Assert.AreEqual("#1", claim.ID);
|
||||
Assert.AreEqual(1, claim.Left);
|
||||
Assert.AreEqual(3, claim.Top);
|
||||
Assert.AreEqual(4, claim.Width);
|
||||
Assert.AreEqual(4, claim.Height);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void FromString__Test3()
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#2 @ 3,1: 4x4");
|
||||
|
||||
Assert.AreEqual("#2", claim.ID);
|
||||
Assert.AreEqual(3, claim.Left);
|
||||
Assert.AreEqual(1, claim.Top);
|
||||
Assert.AreEqual(4, claim.Width);
|
||||
Assert.AreEqual(4, claim.Height);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void FromString__Test4()
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#3 @ 5,5: 2x2");
|
||||
|
||||
Assert.AreEqual("#3", claim.ID);
|
||||
Assert.AreEqual(5, claim.Left);
|
||||
Assert.AreEqual(5, claim.Top);
|
||||
Assert.AreEqual(2, claim.Width);
|
||||
Assert.AreEqual(2, claim.Height);
|
||||
}
|
||||
|
||||
#endregion FromString
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace AdventOfCode2018
|
||||
{
|
||||
@@ -55,8 +57,78 @@ namespace AdventOfCode2018
|
||||
{
|
||||
public string ResolveDay03(string[] inputs)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
List<Claim> claims = inputs.Select(i => Claim.FromString(i)).ToList();
|
||||
|
||||
const int edgeSize = 1000;
|
||||
int[,] cells = new int[edgeSize, edgeSize];
|
||||
|
||||
for (int j = 0; j < edgeSize; j++)
|
||||
{
|
||||
for (int i = 0; i < edgeSize; i++)
|
||||
{
|
||||
cells[i, j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Claim claim in claims)
|
||||
{
|
||||
for (int j = 0; j < claim.Height; j++)
|
||||
{
|
||||
for (int i = 0; i < claim.Width; i++)
|
||||
{
|
||||
cells[claim.Left + i, claim.Top + j]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int overlappedArea = 0;
|
||||
for (int j = 0; j < edgeSize; j++)
|
||||
{
|
||||
for (int i = 0; i < edgeSize; i++)
|
||||
{
|
||||
if (cells[i, j] > 1)
|
||||
{
|
||||
overlappedArea++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return overlappedArea.ToString();
|
||||
}
|
||||
|
||||
public class Claim
|
||||
{
|
||||
public string ID { get; set; }
|
||||
|
||||
public int Left { get; set; }
|
||||
public int Top { get; set; }
|
||||
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
|
||||
public int MinX { get { return Left; } }
|
||||
public int MaxX { get { return Left + Width; } }
|
||||
|
||||
public int MinY { get { return Top; } }
|
||||
public int MaxY { get { return Top + Height; } }
|
||||
|
||||
public int GetArea()
|
||||
{
|
||||
return Width * Height;
|
||||
}
|
||||
|
||||
public static Claim FromString(string strClaim)
|
||||
{
|
||||
Claim claim = new Claim();
|
||||
string[] parts = strClaim.Split(new string[] { " @ ", ",", ": ", "x", }, StringSplitOptions.None);
|
||||
claim.ID = parts[0];
|
||||
claim.Left = Convert.ToInt32(parts[1]);
|
||||
claim.Top = Convert.ToInt32(parts[2]);
|
||||
claim.Width = Convert.ToInt32(parts[3]);
|
||||
claim.Height = Convert.ToInt32(parts[4]);
|
||||
return claim;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user