Vec.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. namespace Day17;
  2. public record struct Vec
  3. {
  4. public int X { get; }
  5. public int Y { get; }
  6. public bool IsHorizontal => Y == 0;
  7. public bool IsVertical => X == 0;
  8. public Vec(int x, int y)
  9. {
  10. X = x;
  11. Y = y;
  12. }
  13. public Vec RotatePositive()
  14. {
  15. // clockwise
  16. return new Vec(-1 * Y, X);
  17. }
  18. public Vec RotateNegative()
  19. {
  20. // counter-clockwise
  21. return new Vec(Y, -1 * X);
  22. }
  23. public Vec Add(Vec other)
  24. {
  25. return new Vec(X + other.X, Y + other.Y);
  26. }
  27. public int Cross(Vec other)
  28. {
  29. return X * other.Y - other.X * Y;
  30. }
  31. public static Vec Direction(char c)
  32. {
  33. switch (c)
  34. {
  35. case 'R':
  36. return new Vec(1, 0);
  37. case 'L':
  38. return new Vec(-1, 0);
  39. case 'U':
  40. return new Vec(0, -1);
  41. case 'D':
  42. return new Vec(0, 1);
  43. }
  44. return default;
  45. }
  46. }