Exemple #1
0
    public override string SolvePart2()
    {
        IShip ship   = new WaypointShip();
        int   result = GetManhattanDistanceOfShip(ship, _instructions);

        return(result.ToString());
    }
        public void WayPointShip_GivenTurns_MovesWaypointAsExpected(string instruction, int expectedLatitude, int expectedLongitude)
        {
            // Waypoint begins at -10 long, +1 lat
            var sut = new WaypointShip(new[] { instruction });

            Assert.Equal(expectedLatitude, Math.Round(sut.WaypointRelativeLatitude));
            Assert.Equal(expectedLongitude, Math.Round(sut.WaypointRelativeLongitude));
        }
Exemple #3
0
        public void Part2(string filename, int expected)
        {
            var parser = new Parser(filename);
            var moves  = parser.Parse(new NavigationFactory());
            var sut    = new WaypointShip(moves);
            var actual = sut.Sail();

            Assert.Equal(expected, actual);
        }
        public void WaypointShip_TurnLeft_MovesWaypointAsExpected()
        {
            var sut = new WaypointShip(new[] { "L90" });

            Assert.Equal(0, sut.Latitude);
            Assert.Equal(0, sut.Longitude);
            Assert.Equal(0, sut.ComputeManhattanDistanceToOrigin());
            Assert.Equal(10, Math.Round(sut.WaypointRelativeLatitude));
            Assert.Equal(1, Math.Round(sut.WaypointRelativeLongitude));
        }
        public void WaypointShip_TurnRight_MovesWaypointAsExpected()
        {
            var sut = new WaypointShip(new[] { "R90" });

            Assert.Equal(0, sut.Latitude);
            Assert.Equal(0, sut.Longitude);
            Assert.Equal(0, sut.ComputeManhattanDistanceToOrigin());
            Assert.Equal(-10, sut.WaypointRelativeLatitude);
            Assert.Equal(-1, sut.WaypointRelativeLongitude);
        }
        public void WaypointShip_East_DecreasesWaypointRelativeLongitude()
        {
            var sut = new WaypointShip(new[] { "E40" });

            Assert.Equal(0, sut.Latitude);
            Assert.Equal(0, sut.Longitude);
            Assert.Equal(0, sut.ComputeManhattanDistanceToOrigin());
            Assert.Equal(1, sut.WaypointRelativeLatitude);
            Assert.Equal(-10 + -40, sut.WaypointRelativeLongitude);
        }
        public void WaypointShip_North_IncreasesWaypointRelativeLatitude()
        {
            var sut = new WaypointShip(new [] { "N10" });

            Assert.Equal(0, sut.Latitude);
            Assert.Equal(0, sut.Longitude);
            Assert.Equal(0, sut.ComputeManhattanDistanceToOrigin());
            // Waypoint starts 1 unit north.
            Assert.Equal(10 + 1, sut.WaypointRelativeLatitude);
            Assert.Equal(-10, sut.WaypointRelativeLongitude);
        }
Exemple #8
0
        public static void Run()
        {
            var inputActions = File.ReadAllLines("day12/input.txt").Select(l => new Action(l)).ToArray();

            var shipAtTarget = new Ship(inputActions);

            Console.WriteLine(shipAtTarget.Pos.LengthManhattan);
            var wpShipAtTarget = new WaypointShip(inputActions);

            Console.WriteLine(wpShipAtTarget.ShipPos.LengthManhattan);
        }
Exemple #9
0
        public WaypointShip(IEnumerable <Action> actions)
        {
            var ship = Initial;

            foreach (var action in actions)
            {
                ship = new WaypointShip(ship, action);
            }
            ShipPos = ship.ShipPos;
            WpPos   = ship.WpPos;
        }
        public override int Second()
        {
            var actions = GetInputLines();
            var ship    = new WaypointShip();

            foreach (var action in actions)
            {
                ship.Action(action);
            }

            return(ship.ManhattanDistance);
        }
Exemple #11
0
        /// <summary>
        /// TurtleShip.Cmd entry point
        /// </summary>
        /// <param name="args">Command line arguments (not used)</param>
        static void Main(string[] args)
        {
            var filePath     = "./input";
            var reader       = new FileReader();
            var instructions = reader.ReadFileByLines(filePath);
            var ship         = new Ship(instructions);

            Console.WriteLine(Math.Round(ship.ComputeManhattanDistanceToOrigin()));

            var waypointShip = new WaypointShip(instructions);

            Console.WriteLine(waypointShip.ComputeManhattanDistanceToOrigin());
        }
        public void WaypointShip_GivenInstructions_IncreasesDistanceAsExpected()
        {
            var sut = new WaypointShip(new[]
            {
                "F10",
                "N3",
                "F7",
                "R90",
                "F11"
            });

            Assert.Equal(286, sut.ComputeManhattanDistanceToOrigin());
            Assert.Equal(-214, sut.Longitude);
            Assert.Equal(-72, sut.Latitude);
        }
Exemple #13
0
        public void SolvePuzzle2()
        {
            var input = new FileReader()
                        .GetResource("AdventOfCode2020.Tests.Day12.PuzzleInput.txt");

            var instructions = InstructionParser.ParseInstructions(input);
            var waypoint     = new Waypoint(FacingDirection.East, new Position {
                Horizontal = 10, Vertical = 1
            });
            var ship = new WaypointShip(FacingDirection.East, new Position(), waypoint);

            ship = instructions.Aggregate(ship, (current, instruction) => current.PerformInstruction(instruction));

            _testOutputHelper.WriteLine(ship.GetManhanttanDistance().ToString());
        }
Exemple #14
0
        public void Example2(string input, int distance)
        {
            var instructions = InstructionParser.ParseInstructions(input.Replace("|", Environment.NewLine));
            var waypoint     = new Waypoint(FacingDirection.East, new Position {
                Horizontal = 10, Vertical = 1
            });
            var ship = new WaypointShip(FacingDirection.East, new Position(), waypoint);

            foreach (var instruction in instructions)
            {
                ship = ship.PerformInstruction(instruction);
            }

            Assert.Equal(distance, ship.GetManhanttanDistance());
        }
Exemple #15
0
        public void Example2()
        {
            var ship0 = WaypointShip.Initial;
            var ship1 = new WaypointShip(ship0, new Action("F10"));
            var ship2 = new WaypointShip(ship1, new Action("N3"));
            var ship3 = new WaypointShip(ship2, new Action("F7"));
            var ship4 = new WaypointShip(ship3, new Action("R90"));
            var ship5 = new WaypointShip(ship4, new Action("F11"));

            Assert.AreEqual(new IVec2(100, -10), ship1.ShipPos);
            Assert.AreEqual(new IVec2(100, -10), ship2.ShipPos);
            Assert.AreEqual(new IVec2(170, -38), ship3.ShipPos);
            Assert.AreEqual(new IVec2(170, -38), ship4.ShipPos);
            Assert.AreEqual(new IVec2(214, 72), ship5.ShipPos);

            Assert.AreEqual(286, ship5.ShipPos.LengthManhattan);
        }
Exemple #16
0
        public WaypointShip(WaypointShip prevShip, Action action)
        {
            ShipPos = prevShip.ShipPos;
            WpPos   = prevShip.WpPos;

            switch (action.Kind)
            {
            case ActionKind.North: WpPos += North * action.Value; break;

            case ActionKind.South: WpPos += South * action.Value; break;

            case ActionKind.East: WpPos += East * action.Value; break;

            case ActionKind.West: WpPos += West * action.Value; break;

            case ActionKind.Left: WpPos = WpPos.Rotate90(-action.Value); break;

            case ActionKind.Right: WpPos = WpPos.Rotate90(+action.Value); break;

            case ActionKind.Forward: ShipPos += WpPos * action.Value; break;

            default: throw new NotImplementedException();
            }
        }