From 0ab92fadfbe5d680dde7d0ba9e142b6ce64b2308 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 3 Dec 2018 18:44:04 +0100 Subject: [PATCH] Day03 Part 1 --- .../AdventOfCode2018.Tests.csproj | 1 + AdventOfCode2018.Tests/Claim_Tests.cs | 60 +++++++++++++++ AdventOfCode2018/Day03.cs | 74 ++++++++++++++++++- 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 AdventOfCode2018.Tests/Claim_Tests.cs diff --git a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj index 9ebcc8c..38902e8 100644 --- a/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj +++ b/AdventOfCode2018.Tests/AdventOfCode2018.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/AdventOfCode2018.Tests/Claim_Tests.cs b/AdventOfCode2018.Tests/Claim_Tests.cs new file mode 100644 index 0000000..6600f26 --- /dev/null +++ b/AdventOfCode2018.Tests/Claim_Tests.cs @@ -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 + } +} \ No newline at end of file diff --git a/AdventOfCode2018/Day03.cs b/AdventOfCode2018/Day03.cs index 196c791..b579264 100644 --- a/AdventOfCode2018/Day03.cs +++ b/AdventOfCode2018/Day03.cs @@ -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 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; + } + } } + + }