Code for Day06 part 1
This commit is contained in:
@@ -56,6 +56,7 @@
|
||||
<Otherwise />
|
||||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="ChronoPoint_Tests.cs" />
|
||||
<Compile Include="Claim_Tests.cs" />
|
||||
<Compile Include="Day01_Tests.cs" />
|
||||
<Compile Include="Day02_Tests.cs" />
|
||||
|
||||
65
AdventOfCode2018.Tests/ChronoPoint_Tests.cs
Normal file
65
AdventOfCode2018.Tests/ChronoPoint_Tests.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace AdventOfCode2018.Tests
|
||||
{
|
||||
[TestClass()]
|
||||
public class ChronoPoint_Tests
|
||||
{
|
||||
#region FromString
|
||||
|
||||
[TestMethod()]
|
||||
public void FromString__Test1()
|
||||
{
|
||||
ChronoPoint point = ChronoPoint.FromString("1, 1");
|
||||
|
||||
Assert.AreEqual(1, point.X);
|
||||
Assert.AreEqual(1, point.Y);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void FromString__Test2()
|
||||
{
|
||||
ChronoPoint point = ChronoPoint.FromString("1, 6");
|
||||
|
||||
Assert.AreEqual(1, point.X);
|
||||
Assert.AreEqual(6, point.Y);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void FromString__Test3()
|
||||
{
|
||||
ChronoPoint point = ChronoPoint.FromString("8, 9");
|
||||
|
||||
Assert.AreEqual(8, point.X);
|
||||
Assert.AreEqual(9, point.Y);
|
||||
}
|
||||
|
||||
#endregion FromString
|
||||
|
||||
#region ManhattanDistance
|
||||
|
||||
[TestMethod()]
|
||||
public void ManhattanDistance__Test1()
|
||||
{
|
||||
ChronoPoint p0 = ChronoPoint.FromString("8, 9");
|
||||
ChronoPoint p1 = ChronoPoint.FromString("1, 6");
|
||||
|
||||
int distance = ChronoPoint.ManhattanDistance(p0, p1);
|
||||
|
||||
Assert.AreEqual(10, distance);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ManhattanDistance__Test2()
|
||||
{
|
||||
ChronoPoint p0 = ChronoPoint.FromString("1, 1");
|
||||
ChronoPoint p1 = ChronoPoint.FromString("1, 6");
|
||||
|
||||
int distance = ChronoPoint.ManhattanDistance(p0, p1);
|
||||
|
||||
Assert.AreEqual(5, distance);
|
||||
}
|
||||
|
||||
#endregion ManhattanDistance
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
namespace AdventOfCode2018
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace AdventOfCode2018
|
||||
{
|
||||
/*
|
||||
--- Day 6: Chronal Coordinates ---
|
||||
@@ -59,7 +63,64 @@
|
||||
{
|
||||
public string ResolvePart1(string[] inputs)
|
||||
{
|
||||
return null;
|
||||
List<ChronoPoint> points = inputs
|
||||
.Where(input => string.IsNullOrEmpty(input) == false)
|
||||
.Select(input => ChronoPoint.FromString(input))
|
||||
.ToList();
|
||||
Dictionary<int, int> pointsAreas = new Dictionary<int, int>();
|
||||
for (int i = 0; i < points.Count; i++)
|
||||
{
|
||||
pointsAreas.Add(i, 0);
|
||||
}
|
||||
|
||||
int minX = points.Min(p => p.X) - 1;
|
||||
int maxX = points.Max(p => p.X) + 1;
|
||||
int minY = points.Min(p => p.Y) - 1;
|
||||
int maxY = points.Max(p => p.Y) + 1;
|
||||
|
||||
ChronoPoint samplingPoint = new ChronoPoint();
|
||||
for(int i=minX; i <= maxX; i++)
|
||||
{
|
||||
for(int j = minY; j <= maxY; j++)
|
||||
{
|
||||
samplingPoint.X = i;
|
||||
samplingPoint.Y = j;
|
||||
bool isEdge = i == minX || i == maxX || j == minY || j == maxY;
|
||||
|
||||
int idxMin = -1;
|
||||
int distanceMin = int.MaxValue;
|
||||
for (int idx = 0; idx < points.Count; idx++)
|
||||
{
|
||||
int distance = ChronoPoint.ManhattanDistance(samplingPoint, points[idx]);
|
||||
if (distance == distanceMin)
|
||||
{
|
||||
idxMin = -1;
|
||||
}
|
||||
else if (distance < distanceMin)
|
||||
{
|
||||
distanceMin = distance;
|
||||
idxMin = idx;
|
||||
}
|
||||
}
|
||||
if (idxMin < 0) { continue; }
|
||||
|
||||
if (isEdge)
|
||||
{
|
||||
pointsAreas[idxMin] = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int previousArea = pointsAreas[idxMin];
|
||||
if (previousArea >= 0)
|
||||
{
|
||||
pointsAreas[idxMin] = previousArea + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int maxArea = pointsAreas.Max(p => p.Value);
|
||||
return maxArea.ToString();
|
||||
}
|
||||
|
||||
public string ResolvePart2(string[] inputs)
|
||||
@@ -67,4 +128,29 @@
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class ChronoPoint
|
||||
{
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
|
||||
public static ChronoPoint FromString(string strPoint)
|
||||
{
|
||||
if (string.IsNullOrEmpty(strPoint)) { return null; }
|
||||
string[] parts = strPoint.Split(new string[] { ", ", }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length < 2) { return null; }
|
||||
ChronoPoint point = new ChronoPoint
|
||||
{
|
||||
X = Convert.ToInt32(parts[0]),
|
||||
Y = Convert.ToInt32(parts[1]),
|
||||
};
|
||||
return point;
|
||||
}
|
||||
|
||||
public static int ManhattanDistance(ChronoPoint p0, ChronoPoint p1)
|
||||
{
|
||||
int distance = Math.Abs(p1.X - p0.X) + Math.Abs(p1.Y - p0.Y);
|
||||
return distance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user