Day03 part 2
This commit is contained in:
@@ -12,7 +12,7 @@ namespace AdventOfCode2018.Tests
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#123 @ 3,2: 5x4");
|
||||
|
||||
Assert.AreEqual("#123", claim.ID);
|
||||
Assert.AreEqual(123, claim.ID);
|
||||
Assert.AreEqual(3, claim.Left);
|
||||
Assert.AreEqual(2, claim.Top);
|
||||
Assert.AreEqual(5, claim.Width);
|
||||
@@ -24,7 +24,7 @@ namespace AdventOfCode2018.Tests
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#1 @ 1,3: 4x4");
|
||||
|
||||
Assert.AreEqual("#1", claim.ID);
|
||||
Assert.AreEqual(1, claim.ID);
|
||||
Assert.AreEqual(1, claim.Left);
|
||||
Assert.AreEqual(3, claim.Top);
|
||||
Assert.AreEqual(4, claim.Width);
|
||||
@@ -36,7 +36,7 @@ namespace AdventOfCode2018.Tests
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#2 @ 3,1: 4x4");
|
||||
|
||||
Assert.AreEqual("#2", claim.ID);
|
||||
Assert.AreEqual(2, claim.ID);
|
||||
Assert.AreEqual(3, claim.Left);
|
||||
Assert.AreEqual(1, claim.Top);
|
||||
Assert.AreEqual(4, claim.Width);
|
||||
@@ -48,7 +48,7 @@ namespace AdventOfCode2018.Tests
|
||||
{
|
||||
Day03.Claim claim = Day03.Claim.FromString("#3 @ 5,5: 2x2");
|
||||
|
||||
Assert.AreEqual("#3", claim.ID);
|
||||
Assert.AreEqual(3, claim.ID);
|
||||
Assert.AreEqual(5, claim.Left);
|
||||
Assert.AreEqual(5, claim.Top);
|
||||
Assert.AreEqual(2, claim.Width);
|
||||
@@ -56,5 +56,42 @@ namespace AdventOfCode2018.Tests
|
||||
}
|
||||
|
||||
#endregion FromString
|
||||
|
||||
#region Overlaps
|
||||
|
||||
[TestMethod()]
|
||||
public void Overlaps__Test1()
|
||||
{
|
||||
Day03.Claim claim1 = Day03.Claim.FromString("#1 @ 1,3: 4x4");
|
||||
Day03.Claim claim2 = Day03.Claim.FromString("#3 @ 5,5: 2x2");
|
||||
|
||||
bool result = Day03.Claim.Overlaps(claim1, claim2);
|
||||
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Overlaps__Test2()
|
||||
{
|
||||
Day03.Claim claim1 = Day03.Claim.FromString("#2 @ 3,1: 4x4");
|
||||
Day03.Claim claim2 = Day03.Claim.FromString("#3 @ 5,5: 2x2");
|
||||
|
||||
bool result = Day03.Claim.Overlaps(claim1, claim2);
|
||||
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Overlaps__Test3()
|
||||
{
|
||||
Day03.Claim claim1 = Day03.Claim.FromString("#1 @ 1,3: 4x4");
|
||||
Day03.Claim claim2 = Day03.Claim.FromString("#2 @ 3,1: 4x4");
|
||||
|
||||
bool result = Day03.Claim.Overlaps(claim1, claim2);
|
||||
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
#endregion Overlaps
|
||||
}
|
||||
}
|
||||
@@ -18,5 +18,19 @@ namespace AdventOfCode2018.Tests
|
||||
|
||||
Assert.AreEqual("4", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ResolveDay03_Part2__Test()
|
||||
{
|
||||
Day03 day03 = new Day03();
|
||||
|
||||
string result = day03.ResolveDay03_Part2(new string[] {
|
||||
"#1 @ 1,3: 4x4",
|
||||
"#2 @ 3,1: 4x4",
|
||||
"#3 @ 5,5: 2x2",
|
||||
});
|
||||
|
||||
Assert.AreEqual("3", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,9 +95,36 @@ namespace AdventOfCode2018
|
||||
return overlappedArea.ToString();
|
||||
}
|
||||
|
||||
public string ResolveDay03_Part2(string[] inputs)
|
||||
{
|
||||
List<Claim> claims = inputs.Select(i => Claim.FromString(i)).ToList();
|
||||
|
||||
Claim unoverlappingClaim = null;
|
||||
for (int i = 0; i < claims.Count; i++)
|
||||
{
|
||||
bool overlaps = false;
|
||||
for (int j = 0; j < claims.Count; j++)
|
||||
{
|
||||
if (i == j) { continue; }
|
||||
if (Claim.Overlaps(claims[i], claims[j]))
|
||||
{
|
||||
overlaps = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (overlaps == false)
|
||||
{
|
||||
unoverlappingClaim = claims[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return unoverlappingClaim.ID.ToString();
|
||||
|
||||
}
|
||||
|
||||
public class Claim
|
||||
{
|
||||
public string ID { get; set; }
|
||||
public int ID { get; set; }
|
||||
|
||||
public int Left { get; set; }
|
||||
public int Top { get; set; }
|
||||
@@ -120,13 +147,33 @@ namespace AdventOfCode2018
|
||||
{
|
||||
Claim claim = new Claim();
|
||||
string[] parts = strClaim.Split(new string[] { " @ ", ",", ": ", "x", }, StringSplitOptions.None);
|
||||
claim.ID = parts[0];
|
||||
claim.ID = Convert.ToInt32(parts[0].Substring(1));
|
||||
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;
|
||||
}
|
||||
|
||||
public static bool Overlaps(Claim claim1, Claim claim2)
|
||||
{
|
||||
if (claim1.MinX <= claim2.MaxX &&
|
||||
claim2.MinX <= claim1.MaxX &&
|
||||
claim1.MinY <= claim2.MaxY &&
|
||||
claim2.MinY <= claim1.MaxY)
|
||||
{
|
||||
int minX = Math.Max(claim1.MinX, claim2.MinX);
|
||||
int maxX = Math.Min(claim1.MaxX, claim2.MaxX);
|
||||
int minY = Math.Max(claim1.MinY, claim2.MinY);
|
||||
int maxY = Math.Min(claim1.MaxY, claim2.MaxY);
|
||||
int width = maxX - minX;
|
||||
int height = maxY - minY;
|
||||
if (width <= 0 || height <= 0) { return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ namespace AdventOfCode2018
|
||||
string[] linesDay03 = File.ReadAllLines("inputs/Day03.txt");
|
||||
string resultDay03 = day03.ResolveDay03(linesDay03);
|
||||
Console.WriteLine("Day03 Result: {0}", resultDay03);
|
||||
string resultDay03Part2 = day03.ResolveDay03_Part2(linesDay03);
|
||||
Console.WriteLine("Day03_Part2 Result: {0}", resultDay03Part2);
|
||||
}
|
||||
|
||||
Console.Read();
|
||||
|
||||
Reference in New Issue
Block a user