@@ -21,12 +21,12 @@ func (c GridCoord) IsZero() bool {
2121 return c .X == 0 && c .Y == 0
2222}
2323
24- // Add performs a + operation and returns the result coordinate.
24+ // Add performs a '+' operation and returns the result coordinate.
2525func (c GridCoord ) Add (other GridCoord ) GridCoord {
2626 return GridCoord {X : c .X + other .X , Y : c .Y + other .Y }
2727}
2828
29- // Sub performs a - operation and returns the result coordinate.
29+ // Sub performs a '-' operation and returns the result coordinate.
3030func (c GridCoord ) Sub (other GridCoord ) GridCoord {
3131 return GridCoord {X : c .X - other .X , Y : c .Y - other .Y }
3232}
@@ -74,6 +74,59 @@ func (c GridCoord) Dist(other GridCoord) int {
7474 return intabs (c .X - other .X ) + intabs (c .Y - other .Y )
7575}
7676
77+ func (c GridCoord ) ToTinyCoord () TinyGridCoord {
78+ return TinyGridCoord {
79+ X : int8 (c .X ),
80+ Y : int8 (c .Y ),
81+ }
82+ }
83+
84+ // TinyGridCoord is a compact GridCoord representation
85+ // that is only capable of storing small values that fit in int8 X/Y.
86+ // It can be expanded into GridCoord and vice versa.
87+ type TinyGridCoord struct {
88+ X int8
89+ Y int8
90+ }
91+
92+ func (c TinyGridCoord ) ToCoord () GridCoord {
93+ return GridCoord {
94+ X : int (c .X ),
95+ Y : int (c .Y ),
96+ }
97+ }
98+
99+ // IsZero reports whether the coord is {0, 0}.
100+ func (c TinyGridCoord ) IsZero () bool {
101+ return c .X == 0 && c .Y == 0
102+ }
103+
104+ // Add performs a '+' operation and returns the result coordinate.
105+ func (c TinyGridCoord ) Add (other TinyGridCoord ) TinyGridCoord {
106+ return TinyGridCoord {X : c .X + other .X , Y : c .Y + other .Y }
107+ }
108+
109+ // Sub performs a '-' operation and returns the result coordinate.
110+ func (c TinyGridCoord ) Sub (other TinyGridCoord ) TinyGridCoord {
111+ return TinyGridCoord {X : c .X - other .X , Y : c .Y - other .Y }
112+ }
113+
114+ func (c TinyGridCoord ) Move (d Direction ) TinyGridCoord {
115+ return c .ToCoord ().Move (d ).ToTinyCoord ()
116+ }
117+
118+ // Dist finds a Manhattan distance between the two coordinates.
119+ func (c TinyGridCoord ) Dist (other TinyGridCoord ) int {
120+ return int (int8abs (c .X - other .X )) + int (int8abs (c .Y - other .Y ))
121+ }
122+
123+ func int8abs (x int8 ) int8 {
124+ if x < 0 {
125+ return - x
126+ }
127+ return x
128+ }
129+
77130func intabs (x int ) int {
78131 if x < 0 {
79132 return - x
0 commit comments