-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.cs
51 lines (37 loc) · 2.03 KB
/
Point.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
namespace AdventOfCode2020
{
public readonly struct Point : IEquatable<Point>
{
public int X { get; }
public int Y { get; }
public Point(int x = 0, int y = 0) => (X, Y) = (x, y);
public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
public static readonly Point Origin = new();
public int ManhattanDistance(Point otherPoint) => Math.Abs(X - otherPoint.X) + Math.Abs(Y - otherPoint.Y);
public static Point operator +(Point point) => point;
public static Point operator -(Point point) => -1 * point;
public static Point operator +(Point point, Point otherPoint)
=> new(point.X + otherPoint.X, point.Y + otherPoint.Y);
public static Point operator -(Point point, Point otherPoint)
=> new(point.X - otherPoint.X, point.Y - otherPoint.Y);
public static Point operator *(Point point, int value) => new(point.X * value, point.Y * value);
public static Point operator *(int value, Point point) => new(point.X * value, point.Y * value);
public static Point operator /(Point point, int value)
=> value == 0 ? throw new DivideByZeroException() : new Point(point.X / value, point.Y / value);
public static Point operator /(int value, Point point)
=> point.X == 0 || point.Y == 0
? throw new DivideByZeroException()
: new Point(value / point.X, value / point.Y);
public static bool operator ==(Point point, Point other) => point.Equals(other);
public static bool operator !=(Point point, Point other) => !point.Equals(other);
/// <inheritdoc />
public bool Equals(Point other) => X == other.X && Y == other.Y;
/// <inheritdoc />
public override bool Equals(object? obj) => obj is Point other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(X, Y);
/// <inheritdoc />
public override string ToString() => $"({X},{Y})";
}
}