-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.nix
More file actions
243 lines (219 loc) · 3.83 KB
/
grid.nix
File metadata and controls
243 lines (219 loc) · 3.83 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
pkgs ? import ../locked.nix,
}:
let
lib = pkgs.lib;
myLib = (import ./default.nix { inherit pkgs; });
/**
type Grid<T> = {
lst: List<T>
width: number
height: number
}
*/
parse2dGrid =
text:
let
rowStrs = (lib.strings.splitString "\n" (lib.strings.trim text));
# 2d list of charcters
rows = builtins.map (lib.stringToCharacters) rowStrs;
width = builtins.length (builtins.head rows);
height = builtins.length rows;
in
{
inherit width height;
lst = lib.flatten rows;
};
# returns a list of `{x,y, value}` to get all values of the grid with their coordinates
asList =
grid: # grid<T>
lib.imap0 (
i: v:
(
{
value = v;
}
// (idxToCoord {
idx = i;
width = grid.width;
})
)
) grid.lst;
mapGrid =
fn: # T -> U
grid: # grid<T>
let
newLst = builtins.map fn grid.lst;
in
grid // { lst = newLst; };
coordToIndex =
{
x,
y,
width,
}:
x + (y * width);
getCol = { idx, width }: myLib.modulo idx width;
getRow = { idx, width }: idx / width;
idxToCoord =
{ idx, width }:
let
x = getCol { inherit idx width; };
y = getRow { inherit idx width; };
in
{
inherit x y;
};
isValidCoord =
{
grid, # Grid<T>
x,
y,
}:
let
xValid = x >= 0 && x < grid.width;
yValid = y >= 0 && y < grid.height;
in
xValid && yValid;
getCoordSafe =
{
grid,
x,
y,
}:
let
validcoord = isValidCoord {
inherit
grid
x
y
;
};
idx = coordToIndex {
inherit x y;
width = grid.width;
};
in
if validcoord then
{ result = builtins.elemAt grid.lst idx; }
else
{
error = "invalid coord";
};
getCoord =
{
grid, # Grid<T>
x,
y,
}:
let
res = getCoordSafe {
inherit
grid
x
y
;
};
in
if builtins.hasAttr "result" res then res.result else throw res.error;
# functions of coord -> coord for a particular direction
movementFuncs = {
north =
{ x, y }:
{
inherit x;
y = y - 1;
};
south =
{ x, y }:
{
inherit x;
y = y + 1;
};
east =
{ x, y }:
{
inherit y;
x = x + 1;
};
west =
{ x, y }:
{
inherit y;
x = x - 1;
};
northEast =
{ x, y }:
{
y = y - 1;
x = x + 1;
};
northWest =
{ x, y }:
{
y = y - 1;
x = x - 1;
};
southEast =
{ x, y }:
{
y = y + 1;
x = x + 1;
};
southWest =
{ x, y }:
{
y = y + 1;
x = x - 1;
};
};
# enum to track direction
directions = {
north = 0;
south = 1;
east = 2;
west = 3;
northEast = 4;
northWest = 5;
southEast = 6;
southWest = 7;
};
movementForDir =
dir:
if dir == directions.east then
movementFuncs.east
else if dir == directions.west then
movementFuncs.west
else if dir == directions.north then
movementFuncs.north
else if dir == directions.south then
movementFuncs.south
else
throw "unhandled dir ${toString dir}";
# manhattan distance between two coords
coordDist =
c1: c2:
let
xDiff = myLib.abs (c1.x - c2.x);
yDiff = myLib.abs (c1.y - c2.y);
in
xDiff + yDiff;
in
{
inherit
parse2dGrid
asList
mapGrid
coordToIndex
getCoordSafe
getCoord
getCol
getRow
idxToCoord
isValidCoord
movementFuncs
directions
coordDist
movementForDir
;
}