Vec.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. namespace Day22;
  2. public record struct Vec(long X, long Y, long Z)
  3. {
  4. public static Vec Zero { get; } = new Vec(0, 0, 0);
  5. public static Vec Up { get; } = new Vec(0, 0, 1);
  6. public static Vec Down { get; } = new Vec(0, 0, -1);
  7. public static Vec FromArray(int[] nums)
  8. {
  9. return new Vec(nums[0], nums[1], nums[2]);
  10. }
  11. public Vec Add(Vec other)
  12. {
  13. return new Vec(X + other.X, Y + other.Y, Z + other.Z);
  14. }
  15. public Vec Subtract(Vec other)
  16. {
  17. return new Vec(X - other.X, Y - other.Y, Z - other.Z);
  18. }
  19. public long Magnitude()
  20. {
  21. if (new[] { X, Y, Z }.Count(v => v == 0) < 2)
  22. {
  23. throw new Exception("Only works with one-axis vectors");
  24. }
  25. return X + Y + Z;
  26. }
  27. public Vec Normalize()
  28. {
  29. var length = Magnitude();
  30. length = length == 0 ? 1 : length;
  31. return new Vec(X / length, Y / length, Z / length);
  32. }
  33. public Vec Multiply(long f)
  34. {
  35. return new Vec(f * X, f * Y, f * Z);
  36. }
  37. }