Exemplo n.º 1
0
        protected virtual void DoWalk(RouteStep routeStep)
        {
            var currentCoordinates = new RoutePoint(XCoordinate, YCoordinate);

            switch (_direction)
            {
            case Direction.Up:
                YCoordinate += routeStep.Distance;
                break;

            case Direction.Down:
                YCoordinate -= routeStep.Distance;
                break;

            case Direction.Left:
                XCoordinate -= routeStep.Distance;
                break;

            case Direction.Right:
                XCoordinate += routeStep.Distance;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var newCoordinates = new RoutePoint(XCoordinate, YCoordinate);

            Links.Add(new RouteLink(currentCoordinates, newCoordinates));
        }
Exemplo n.º 2
0
        public RouteLink(RoutePoint start, RoutePoint finish)
        {
            if (start.X != finish.X && start.Y != finish.Y)
            {
                throw new ArgumentException("Must be strictly horizontal or vertical link.");
            }

            if (start.Equals(finish))
            {
                throw new ArgumentException("Must not be same points.");
            }

            Start  = start;
            Finish = finish;
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Advent Of Code 2016, day 1");

            var input = InputUtils.GetInput(args).ReadToEnd();

            var routeParser = new RouteParser();
            var steps       = routeParser.GetSteps(input).ToArray();

            var walker = new RouteWalker();

            walker.Walk(steps);

            var totalDistance = Math.Abs(walker.XCoordinate) + Math.Abs(walker.YCoordinate);

            Console.WriteLine($"Answer to question 1: {totalDistance}");

            RoutePoint intersection = null;

            for (int i = 4; i <= walker.Links.Count; i++)
            {
                var subList = walker.Links.Take(i).ToList();

                var subject = subList.Last();

                foreach (var routeLink in subList.AsEnumerable().Reverse().Skip(3))
                {
                    intersection = subject.FindIntersection(routeLink);
                    if (intersection != null)
                    {
                        break;
                    }
                }

                if (intersection != null)
                {
                    break;
                }
            }

            if (intersection != null)
            {
                var intersectionDistance = Math.Abs(intersection.X) + Math.Abs(intersection.Y);
                Console.WriteLine($"Answer to question 2: {intersectionDistance}");
            }
        }