private int FindIdOfClaimWithNoOverlap(string input) { var fabricClaims = new List <FabricClaim>(); var coordinateMap = new Dictionary <Coordinate, int>(); string[] elfFabricClaims = Parsing.SplitOnNewLine(input); foreach (string claimString in elfFabricClaims) { FabricClaim fabricClaim = ConvertFabricClaimString(claimString); fabricClaims.Add(fabricClaim); foreach (Coordinate coordinate in fabricClaim.Coordinates) { if (coordinateMap.ContainsKey(coordinate)) { coordinateMap[coordinate]++; } else { coordinateMap.Add(coordinate, 1); } } } return(fabricClaims .Single(c => c.Coordinates.All(coor => coordinateMap[coor] == 1)) .Id); }
private int CalculateSquareInchesOfOverlappedFabric(string input) { var coordinateMap = new Dictionary <Coordinate, int>(); string[] elfFabricClaims = Parsing.SplitOnNewLine(input); foreach (string claimString in elfFabricClaims) { FabricClaim fabricClaim = ConvertFabricClaimString(claimString); foreach (Coordinate coordinate in fabricClaim.Coordinates) { if (coordinateMap.ContainsKey(coordinate)) { coordinateMap[coordinate]++; } else { coordinateMap.Add(coordinate, 1); } } } return(coordinateMap.Where(kvp => kvp.Value > 1).Count());; }
private FabricClaim ConvertFabricClaimString(string claimString) { var fabricClaim = new FabricClaim(); string[] delimitedValues = claimString.Split(new[] { " @ ", ": " }, StringSplitOptions.None); fabricClaim.Id = int.Parse(delimitedValues[0].Substring(1)); string[] positionInfo = delimitedValues[1].Split(','); string[] sizeInfo = delimitedValues[2].Split('x'); int cornerCoordinateX = int.Parse(positionInfo[0]) + 1; int cornerCoordinateY = int.Parse(positionInfo[1]) + 1; fabricClaim.Width = int.Parse(sizeInfo[0]); fabricClaim.Height = int.Parse(sizeInfo[1]); for (int i = cornerCoordinateX; i < cornerCoordinateX + fabricClaim.Width; i++) { for (int j = cornerCoordinateY; j < cornerCoordinateY + fabricClaim.Height; j++) { fabricClaim.Coordinates.Add(new Coordinate { X = i, Y = j }); } } return(fabricClaim); }