public void ExecuteInstructions(ShipPart2 ship) { switch (instr) { case Instruction.North: ship.waypointPosition += new LongPoint2D(0, quantity); break; case Instruction.South: ship.waypointPosition += new LongPoint2D(0, -quantity); break; case Instruction.East: ship.waypointPosition += new LongPoint2D(quantity, 0); break; case Instruction.West: ship.waypointPosition += new LongPoint2D(-quantity, 0); break; case Instruction.Left: case Instruction.Right: ship.waypointPosition = Rotate(ship.waypointPosition, instr, quantity); break; case Instruction.Forward: ship.position += quantity * ship.waypointPosition; break; default: throw new Exception(); } }
static void Main(string[] args) { Console.WriteLine("Day 12 - Rain Risk"); Console.WriteLine("Star 1"); Console.WriteLine(); List <Navigation> instructions = File.ReadAllLines(inputFile) .Select(x => new Navigation(x)) .ToList(); Ship ship = new Ship(); ShipPart2 shipPart2 = new ShipPart2(); foreach (Navigation navigation in instructions) { navigation.ExecuteInstructions(ship); navigation.ExecuteInstructions(shipPart2); } int output1 = Math.Abs(ship.position.x) + Math.Abs(ship.position.y); Console.WriteLine($"The answer is: {output1}"); Console.WriteLine(); Console.WriteLine("Star 2"); Console.WriteLine(); long output2 = Math.Abs(shipPart2.position.x) + Math.Abs(shipPart2.position.y); Console.WriteLine($"The answer is: {output2}"); Console.WriteLine(); Console.ReadKey(); }
public static void Main(string[] args) { var instructions = InputReader.ReadInput( "input.txt", s => new Instruction(s.First(), int.Parse(s.Substring(1)))); var timer = new Stopwatch(); timer.Start(); var ship = new Ship(); foreach (var instruction in instructions) { ship.Execute(instruction); } timer.Stop(); Console.WriteLine($"Part1: {ship.ManhattanDistance}"); Console.WriteLine($"Timer: {timer.Elapsed}"); timer.Restart(); var shipPart2 = new ShipPart2(); var waypoint = new Waypoint(shipPart2); foreach (var instruction in instructions) { ExecutePart2Instruction(instruction, waypoint, shipPart2); } timer.Stop(); Console.WriteLine($"Part2: {shipPart2.ManhattanDistance}"); Console.WriteLine($"Timer: {timer.Elapsed}"); }
public static long Part2(string input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } var ship = new ShipPart2(); ship.ProcessInput(input); return(ship.Distance); }
private static void ExecutePart2Instruction(Instruction instruction, Waypoint waypoint, ShipPart2 shipPart2) { switch (instruction.Operation) { case 'N': waypoint.NorthPosition += instruction.Amount; break; case 'E': waypoint.EastPosition += instruction.Amount; break; case 'S': waypoint.NorthPosition -= instruction.Amount; break; case 'W': waypoint.EastPosition -= instruction.Amount; break; case 'L': case 'R': waypoint.Rotate(instruction.Operation, instruction.Amount); break; case 'F': shipPart2.EastPosition += waypoint.EastPosition * instruction.Amount; shipPart2.NorthPosition += waypoint.NorthPosition * instruction.Amount; break; } }