Beispiel #1
0
        protected override void RunPart1(string Input)
        {
            using (var Reader = new StreamReader(Input))
            {
                var Data       = Reader.ReadToEnd();
                var Directives = Data.Split(new string[] { ", " }, StringSplitOptions.None);

                var CurrentDirection = Direction.North;
                var CurrentPosition  = new Position {
                    X = 0, Y = 0
                };

                foreach (var Directive in Directives)
                {
                    CurrentDirection = Turn(CurrentDirection, Directive[0]);

                    var MovementRate = int.Parse(Directive.Substring(1));

                    CurrentPosition.Move(CurrentDirection, MovementRate);
                }

                var Distance = CurrentPosition.Distance;

                Console.WriteLine("Distance = {0}", Distance);
            }
        }
Beispiel #2
0
        protected override void RunPart2(string Input)
        {
            using (var Reader = new StreamReader(Input))
            {
                var Data       = Reader.ReadToEnd();
                var Directives = Data.Split(new string[] { ", " }, StringSplitOptions.None);

                var CurrentDirection = Direction.North;
                var CurrentPosition  = new Position {
                    X = 0, Y = 0
                };

                var VisitedPositions = new HashSet <Position>();

                foreach (var Directive in Directives)
                {
                    CurrentDirection = Turn(CurrentDirection, Directive[0]);

                    var MovementRate = int.Parse(Directive.Substring(1));

                    for (var i = 0; i < MovementRate; i++)
                    {
                        CurrentPosition.Move(CurrentDirection, 1);
                        if (VisitedPositions.Contains(CurrentPosition))
                        {
                            Console.WriteLine("First location is {0}", CurrentPosition);
                            Console.WriteLine("Distance = {0}", CurrentPosition.Distance);
                            return;
                        }
                        else
                        {
                            VisitedPositions.Add(new Position {
                                X = CurrentPosition.X, Y = CurrentPosition.Y
                            });
                        }
                    }
                }
            }
        }