static public void Day3b() { int maxX = 0; int maxY = 0; Stopwatch sw = new Stopwatch(); sw.Start(); List <Day3aObject> elfClaims = new List <Day3aObject>(); Dictionary <Point, List <Day3aObject> > claimIntersections = new Dictionary <Point, List <Day3aObject> >(); using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day3.txt")) using (StreamReader reader = new StreamReader(stream)) { elfClaims = EnumerateClaims(reader).ToList(); } foreach (Day3aObject d3ao in elfClaims) { if (d3ao.topLeft.X + d3ao.xLength > maxX) { maxX = d3ao.topLeft.X + d3ao.xLength; } if (d3ao.topLeft.Y + d3ao.yHeight > maxY) { maxY = d3ao.topLeft.Y + d3ao.yHeight; } } //FillClaimIntersections(ref claimIntersections, elfClaims); int[,] grid = new int[maxX, maxY]; FillGrid(ref grid, elfClaims); Day3aObject properClaim = TestGrid(grid, elfClaims); sw.Stop(); MessageBox.Show("Claim with no overlaps: " + properClaim.id + "\n\n" + "Found in: " + sw.ElapsedMilliseconds + " ms (" + sw.ElapsedTicks + " ticks)."); sw.Reset(); }
static IEnumerable <Day3aObject> EnumerateClaims(TextReader reader) { string line; while ((line = reader.ReadLine()) != null) { Day3aObject d3ao = new Day3aObject(); string[] lineread = line.Split(new char[0]); d3ao.id = Convert.ToInt32(lineread[0].Replace("#", "")); string[] coords = lineread[2].Replace(":", "").Split(','); d3ao.topLeft = new Point(int.Parse(coords[0]), int.Parse(coords[1])); string[] sizes = lineread[3].Split('x'); d3ao.xLength = int.Parse(sizes[0]); d3ao.yHeight = int.Parse(sizes[1]); yield return(d3ao); } }
static Day3aObject FindProperClaim(Dictionary <Point, List <Day3aObject> > claimIntersections) { Day3aObject _ret = null; List <List <Day3aObject> > sample = new List <List <Day3aObject> >(); foreach (List <Day3aObject> _list in claimIntersections.Values) { sample.Add(_list); } var test = sample.Where(c => c.Count() == 1).Distinct() as List <Day3aObject>; foreach (Day3aObject d3ao in test) { //List<Point> sampling = new List<Point>(); //sampling = claimIntersections.Values. } return(_ret); }
static Day3aObject TestGrid(int[,] grid, List <Day3aObject> elfClaims) { Day3aObject _ret = null; foreach (Day3aObject d3ao in elfClaims) { for (int x = d3ao.topLeft.X; x < d3ao.topLeft.X + d3ao.xLength; x++) { for (int y = d3ao.topLeft.Y; y < d3ao.topLeft.Y + d3ao.yHeight; y++) { if (grid[x, y] > 1) { d3ao.overlaps = true; } } } } _ret = elfClaims.Single(c => c.overlaps == false); return(_ret); }