Beispiel #1
0
        private Coordinate GetMaxOverlap(Fabric fabric1, Fabric fabric2)
        {
            int maxX;
            int maxY;

            if (fabric1.RightOffset <= fabric2.RightOffset)
            {
                maxX = fabric1.RightOffset;
            }
            else
            {
                maxX = fabric2.RightOffset;
            }

            if (fabric1.BottomOffset <= fabric2.BottomOffset)
            {
                maxY = fabric1.BottomOffset;
            }
            else
            {
                maxY = fabric2.BottomOffset;
            }

            return(new Coordinate {
                X = maxX, Y = maxY
            });
        }
Beispiel #2
0
        private static void Main(string[] args)
        {
            var rawClaims = File.ReadAllLines("input.txt");

            var allRectangleClaims = new List <ClaimRectangle>();

            foreach (var rawClaim in rawClaims)
            {
                var claimParams = rawClaim.Split(' ');
                var claimId     = claimParams[0];
                var edgeLengths = claimParams[2].TrimEnd(':').Split(',');
                var dimensions  = claimParams[3].Split('x');
                var newClaim    =
                    new ClaimRectangle(edgeLengths[0], edgeLengths[1], claimId, dimensions[0], dimensions[1]);
                allRectangleClaims.Add(newClaim);
            }

            //Now we have all of our claim data out of the input file, so lets begin adding it to our fabric
            var mainFabric = new Fabric(1000, 1000);

            foreach (var rect in allRectangleClaims)
            {
                mainFabric.AddClaim(rect);
            }

            var overlap = mainFabric.Overlap();

            mainFabric.FindNoOverlap();
            Console.ReadLine();
        }
Beispiel #3
0
        private Coordinate GetMinOverlap(Fabric fabric1, Fabric fabric2)
        {
            int minX;
            int minY;

            if (fabric1.LeftOffset <= fabric2.LeftOffset)
            {
                minX = fabric2.LeftOffset;
            }
            else
            {
                minX = fabric1.LeftOffset;
            }

            if (fabric1.TopOffset <= fabric2.TopOffset)
            {
                minY = fabric2.TopOffset;
            }
            else
            {
                minY = fabric1.TopOffset;
            }

            return(new Coordinate {
                X = minX, Y = minY
            });
        }
        private static void Part1()
        {
            string input = string.Empty;
            int    tempIndex;

            IList <Fabric>    claims          = new List <Fabric>();
            ISet <Coordinate> usedCoordinates = new HashSet <Coordinate>(new CoordinateComparer());

            while ((input = Console.ReadLine()) != "end")
            {
                tempIndex = input.IndexOf("@");
                claims.Add(Fabric.Factory(input.Substring(tempIndex + 2)));
            }

            for (int j = 0; j < claims.Count; j++)
            {
                for (int k = 0; k < claims.Count; k++)
                {
                    if (k != j)
                    {
                        claims[j].CheckClaimOverlaps(claims[k], usedCoordinates);
                    }
                }
            }

            Console.WriteLine($"The answer is: {usedCoordinates.Count}");
            Console.ReadLine();
        }
Beispiel #5
0
        public bool CheckForOverlap(Fabric fabric2)
        {
            bool widthsOverlap =
                (this.LeftOffset <= fabric2.LeftOffset && this.RightOffset > fabric2.LeftOffset) ||
                (fabric2.LeftOffset <= this.LeftOffset && fabric2.RightOffset > this.LeftOffset);
            bool heightsOverlap =
                (this.TopOffset <= fabric2.TopOffset && this.BottomOffset > fabric2.TopOffset) ||
                (fabric2.TopOffset <= this.TopOffset && fabric2.BottomOffset > this.TopOffset);

            return(widthsOverlap && heightsOverlap);
        }
Beispiel #6
0
        public ISet <Coordinate> CheckClaimOverlaps(Fabric fabric2, ISet <Coordinate> usedCoordinates)
        {
            if (CheckForOverlap(fabric2))
            {
                Coordinate min = GetMinOverlap(this, fabric2);
                Coordinate max = GetMaxOverlap(this, fabric2);

                for (int x = min.X; x < max.X; x++)
                {
                    for (int y = min.Y; y < max.Y; y++)
                    {
                        Coordinate overlapPoint = new Coordinate {
                            X = x, Y = y
                        };
                        if (!usedCoordinates.Contains(overlapPoint))
                        {
                            usedCoordinates.Add(overlapPoint);
                        }
                    }
                }
            }

            return(usedCoordinates);
        }
        private static void Part2()
        {
            string input = string.Empty;
            string id    = string.Empty;
            int    tempIndex;

            IList <Fabric> claims          = new List <Fabric>();
            ISet <string>  invalidClaimIDs = new HashSet <string>();

            while ((input = Console.ReadLine()) != "end")
            {
                tempIndex = input.IndexOf("@");
                id        = input.Substring(0, tempIndex);
                claims.Add(Fabric.Factory(input.Substring(tempIndex + 2), id));
            }

            for (int j = 0; j < claims.Count; j++)
            {
                for (int k = 0; k < claims.Count; k++)
                {
                    if (j != k && claims[j].CheckForOverlap(claims[k]))
                    {
                        invalidClaimIDs.Add(claims[j].ID);
                    }
                }
            }

            for (int i = 0; i < claims.Count; i++)
            {
                if (!invalidClaimIDs.Contains(claims[i].ID))
                {
                    Console.WriteLine(claims[i].ID);
                }
            }
            Console.ReadLine();
        }