Refactored to add a class and handle part 2

This commit is contained in:
Josh Creek
2019-10-14 16:57:59 +01:00
parent e971e4dcd4
commit 7132535568
3 changed files with 77 additions and 11 deletions
+11
View File
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
namespace Day_03
{
public class Claim
{
public int ClaimId { get; set; }
public bool CellsUsedMoreThanOnce { get; set; }
}
}
+60 -11
View File
@@ -9,15 +9,10 @@ namespace Day_03
class Program
{
static void Main(string[] args)
{
Part1();
//Part2();
}
static void Part1()
{
// Declare an empty 2D array with 1000x1000 cells, material[x,y] from left and top respectively
int[,] material = new int[1000, 1000];
List<Claim> claims = new List<Claim>();
var lines = File.ReadAllLines("input.txt");
// For each line of the input file
@@ -56,6 +51,7 @@ namespace Day_03
}
}
// Part 1
// Loop through both arrays and count how many cells have a recorded overlap
int overlapCounter = 0;
@@ -68,12 +64,65 @@ namespace Day_03
}
Console.WriteLine(material[0, 0]);
Console.WriteLine(material.Length);
Console.WriteLine("The number of overlapping square inches is " + overlapCounter);
}
Console.WriteLine("The number of overlapping square inches is " + overlapCounter);
static void Part2()
{
// Part 2
// Loop through all claims (lines in the file) and find the one where the material is only used once in every cell
// For each line of the input file
foreach (var line in lines)
{
var claim = new Claim();
// A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall.
// Remove the id number and everything prior to the left edge measurement
int startIndex = line.IndexOf("@") + 1;
int endIndex = line.Length;
var lineData = line.Substring(startIndex, endIndex - startIndex);
// Store all the data
Regex re0 = new Regex(@"(#(\d+))");
var claimIdList = re0.Matches(line);
string claimId = claimIdList[0].Value.Substring(1);
claim.ClaimId = Int32.Parse(claimId);
Regex re1 = new Regex(@"(@ [0-9]+,)");
var leftPositionList = re1.Matches(line);
string leftPosition = leftPositionList[0].Value.Substring(2, leftPositionList[0].Value.Length -1 - 2);
Regex re2 = new Regex(@"(,[0-9]+:)");
var topPositionList = re2.Matches(line);
string topPosition = topPositionList[0].Value.Substring(1, topPositionList[0].Value.Length -1 - 1);
Regex re3 = new Regex(@"(: [0-9]+x)");
var widthList = re3.Matches(line);
int width = Int32.Parse(widthList[0].Value.Substring(1, widthList[0].Value.Length -1 - 1));
Regex re4 = new Regex(@"(x[0-9]+)");
var lengthList = re4.Matches(line);
int length = Int32.Parse(lengthList[0].Value.Substring(1));
// Store data in all cells horizontally and vertically
for (int i = 0; i < width; i++) {
for (int j = 0; j < length; j++) {
// Check how many times the cell has been used at the appropriate co-ordinates
// If it has been used more than once, set CellsUsedMoreThanOnce to true
if (material[(Int32.Parse(leftPosition) + i), (Int32.Parse(topPosition) + j)] > 1)
{
claim.CellsUsedMoreThanOnce = true;
}
}
}
claims.Add(claim);
}
// Find the only claim that has no overlap
int finalClaimId = claims.First(c => !c.CellsUsedMoreThanOnce).ClaimId;
Console.WriteLine("The ID of the only claim that doesn't overlap is: " + finalClaimId);
}
}
}
+6
View File
@@ -0,0 +1,6 @@
--- Part Two ---
Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!
For example, in the claims above, only claim 3 is intact after all claims are made.
What is the ID of the only claim that doesn't overlap?