Vec.cs 1.2 KB

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