Ejemplo n.º 1
0
        public void Part_2_example()
        {
            var ship = new Ship2();

            Assert.Equal((0, 0), (ship.Position.EastWestPosition, ship.Position.NorthSouthPosition));
            Assert.Equal((10, 1), (ship.Waypoint.EastWestPosition, ship.Waypoint.NorthSouthPosition));

            ship.Move('F', 10);
            Assert.Equal((100, 10), (ship.Position.EastWestPosition, ship.Position.NorthSouthPosition));
            Assert.Equal((10, 1), (ship.Waypoint.EastWestPosition, ship.Waypoint.NorthSouthPosition));

            ship.Move('N', 3);
            Assert.Equal((100, 10), (ship.Position.EastWestPosition, ship.Position.NorthSouthPosition));
            Assert.Equal((10, 4), (ship.Waypoint.EastWestPosition, ship.Waypoint.NorthSouthPosition));

            ship.Move('F', 7);
            Assert.Equal((170, 38), (ship.Position.EastWestPosition, ship.Position.NorthSouthPosition));
            Assert.Equal((10, 4), (ship.Waypoint.EastWestPosition, ship.Waypoint.NorthSouthPosition));

            ship.Move('R', 90);
            Assert.Equal((170, 38), (ship.Position.EastWestPosition, ship.Position.NorthSouthPosition));
            Assert.Equal((4, -10), (ship.Waypoint.EastWestPosition, ship.Waypoint.NorthSouthPosition));

            ship.Move('F', 11);
            Assert.Equal((214, -72), (ship.Position.EastWestPosition, ship.Position.NorthSouthPosition));
            Assert.Equal((4, -10), (ship.Waypoint.EastWestPosition, ship.Waypoint.NorthSouthPosition));

            Assert.Equal(286, Part2(Example));
        }
Ejemplo n.º 2
0
        private static long Part2(ReadOnlyMemory <string> input)
        {
            var ship = new Ship2();

            foreach (var line in input)
            {
                var(action, value) = Parse(line);

                ship.Move(action, value);
            }

            return(Math.Abs(ship.Position.EastWestPosition) + Math.Abs(ship.Position.NorthSouthPosition));
        }