jwow22

AdventOfCode2021_Day2

Dec 2nd, 2021 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. public static Command[] Commands;
  2. public static Submarine Submarine;
  3.  
  4. public static void ProcessInput(string[] lines)
  5. {
  6.     Commands = new Command[lines.Length];
  7.     for (int i = 0; i < lines.Length; i++)
  8.     {
  9.         string[] words = lines[i].Split();
  10.         Commands[i].Direction = words[0];
  11.         Commands[i].Units = int.Parse(words[1]);
  12.     }
  13. }
  14.  
  15. public static int SolveP1()
  16. {
  17.     Submarine = new Submarine(Position.zero);
  18.     for (int i = 0; i < Commands.Length; i++)
  19.     {
  20.         if (Commands[i].Direction == "forward")
  21.         {
  22.             Submarine.Position.Horizontal += Commands[i].Units;
  23.         }
  24.         else if (Commands[i].Direction == "up")
  25.         {
  26.             Submarine.Position.Depth -= Commands[i].Units;
  27.         }
  28.         else if (Commands[i].Direction == "down")
  29.         {
  30.             Submarine.Position.Depth += Commands[i].Units;
  31.         }
  32.     }
  33.     return Submarine.Position.Horizontal * Submarine.Position.Depth;
  34. }
  35.  
  36. public static int SolveP2()
  37. {
  38.     Submarine = new Submarine(Position.zero);
  39.     for (int i = 0; i < Commands.Length; i++)
  40.     {
  41.         if (Commands[i].Direction == "forward")
  42.         {
  43.             Submarine.Position.Horizontal += Commands[i].Units;
  44.             Submarine.Position.Depth += Submarine.Aim * Commands[i].Units;
  45.         }
  46.         else if (Commands[i].Direction == "up")
  47.         {
  48.             Submarine.Aim -= Commands[i].Units;
  49.         }
  50.         else if (Commands[i].Direction == "down")
  51.         {
  52.             Submarine.Aim += Commands[i].Units;
  53.         }
  54.     }
  55.     return Submarine.Position.Horizontal * Submarine.Position.Depth;
  56. }
  57.  
  58. public struct Position
  59. {
  60.     public int Horizontal;
  61.     public int Depth;
  62.  
  63.     public static Position zero = new Position();
  64.  
  65.     public Position(int horizontal = 0, int depth = 0)
  66.     {
  67.         Horizontal = horizontal;
  68.         Depth = depth;
  69.     }
  70.  
  71.     public static Position operator +(Position p1, Position p2)
  72.     {
  73.         return new Position
  74.         {
  75.             Depth = p1.Depth + p2.Depth,
  76.             Horizontal = p1.Horizontal + p2.Horizontal
  77.             };
  78.     }
  79.     public static Position operator -(Position p1, Position p2)
  80.     {
  81.         return new Position
  82.         {
  83.             Depth = p1.Depth - p2.Depth,
  84.             Horizontal = p1.Horizontal - p2.Horizontal
  85.             };
  86.     }
  87.     public static Position operator *(Position p, int num)
  88.     {
  89.         return new Position
  90.         {
  91.             Depth = p.Depth * num,
  92.             Horizontal = p.Horizontal * num
  93.             };
  94.     }
  95. }
  96.  
  97. public struct Command
  98. {
  99.     public string Direction;
  100.     public int Units;
  101. }
Add Comment
Please, Sign In to add comment