| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- namespace Day22;
- public record struct Vec(long X, long Y, long Z)
- {
- public static Vec Zero { get; } = new Vec(0, 0, 0);
- public static Vec Up { get; } = new Vec(0, 0, 1);
- public static Vec Down { get; } = new Vec(0, 0, -1);
- public static Vec FromArray(int[] nums)
- {
- return new Vec(nums[0], nums[1], nums[2]);
- }
- public Vec Add(Vec other)
- {
- return new Vec(X + other.X, Y + other.Y, Z + other.Z);
- }
- public Vec Subtract(Vec other)
- {
- return new Vec(X - other.X, Y - other.Y, Z - other.Z);
- }
-
- public long Magnitude()
- {
- if (new[] { X, Y, Z }.Count(v => v == 0) < 2)
- {
- throw new Exception("Only works with one-axis vectors");
- }
- return X + Y + Z;
- }
- public Vec Normalize()
- {
- var length = Magnitude();
- length = length == 0 ? 1 : length;
- return new Vec(X / length, Y / length, Z / length);
- }
- public Vec Multiply(long f)
- {
- return new Vec(f * X, f * Y, f * Z);
- }
- }
|