-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ unit test ```js function tilesInRangeRec( tileId: TileID, range: number, acc: TileID[] ): TileID[] { const neighbourTiles = tilesInRange1Cached(tileId); if (range === 1) { return addAll(acc, neighbourTiles); } const tiles = neighbourTiles.flatMap((tileId) => tilesInRangeRec(tileId, range - 1, acc) ); return addAll(acc, tiles); } ```
- Loading branch information
1 parent
8a5a4c8
commit e012982
Showing
4 changed files
with
118 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { tilesInRange } from "./tiles-in-range"; | ||
|
||
describe("tilesInRange()", () => { | ||
/** | ||
* [ ] [X] [X] | ||
* [ ] [X] [.] [X] | ||
* [ ] [ ] [X] [X] [ ] | ||
* | ||
*/ | ||
it("returns tilesID[] in range 1 from origin tile", () => { | ||
const got = tilesInRange("1,-2", { range: 1 }); | ||
const expected = ["1,-3", "2,-3", "0,-2", "2,-2", "1,-1", "2,-1"]; | ||
expect(got.sort()).toEqual(expected.sort()); | ||
}); | ||
|
||
/** | ||
* [ ] [ ] [X] [X] [X] | ||
* [ ] [ ] [X] [X] [X] [X] | ||
* [ ] [ ] [X] [X] [.] [X] [X] | ||
* [ ] [ ] [ ] [X] [X] [X] [X] [ ] | ||
* [ ] [ ] [ ] [X] [X] [X] [ ] | ||
* [ ] [ ] [ ] [ ] [ ] [ ] | ||
* [ ] [ ] [ ] [ ] [ ] | ||
* | ||
*/ | ||
it("returns tilesID[] in range 2 from origin tile", () => { | ||
const got = tilesInRange("1,-1", { range: 2 }); | ||
const expected = [ | ||
"0,-3", | ||
"1,-3", | ||
"2,-3", | ||
"-1,-2", | ||
"0,-2", | ||
"1,-2", | ||
"2,-2", | ||
"-1,-1", | ||
"0,-1", | ||
"2,-1", | ||
"3,-1", | ||
"-1,0", | ||
"0,0", | ||
"1,0", | ||
"2,0", | ||
"0,1", | ||
"1,1", | ||
"2,1", | ||
]; | ||
expect(got.sort()).toEqual(expected.sort()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { asTileID, coordinates } from "./tiles"; | ||
|
||
const NEIGHBOUR_TILES_MEM_CACHE = {} as Record<TileID, TileID[]>; | ||
|
||
export function tilesInRange( | ||
tileId: TileID, | ||
{ range }: { range?: number } = {} | ||
) { | ||
return tilesInRangeRec(tileId, range || 1, []).filter((t) => t !== tileId); | ||
} | ||
|
||
function tilesInRangeRec( | ||
tileId: TileID, | ||
range: number, | ||
acc: TileID[] | ||
): TileID[] { | ||
const neighbourTiles = tilesInRange1Cached(tileId); | ||
if (range === 1) { | ||
return addAll(acc, neighbourTiles); | ||
} | ||
const tiles = neighbourTiles.flatMap((tileId) => | ||
tilesInRangeRec(tileId, range - 1, acc) | ||
); | ||
return addAll(acc, tiles); | ||
} | ||
|
||
function addAll<T>(a: T[], b: T[]) { | ||
return Array.from(new Set([...a, ...b])); | ||
} | ||
|
||
function tilesInRange1Cached(tileId: TileID) { | ||
if (!NEIGHBOUR_TILES_MEM_CACHE[tileId]) { | ||
NEIGHBOUR_TILES_MEM_CACHE[tileId] = tilesInRange1(tileId); | ||
} | ||
return NEIGHBOUR_TILES_MEM_CACHE[tileId]; | ||
} | ||
|
||
function tilesInRange1(tileId: TileID) { | ||
const { x, y } = coordinates(tileId); | ||
|
||
return range1Variance(y).flatMap((variance) => { | ||
const tileIdStr = asTileID({ | ||
x: x + variance[0], | ||
y: y + variance[1], | ||
}); | ||
return tileIdStr ? [tileIdStr] : []; | ||
}); | ||
} | ||
|
||
function range1Variance(y: number) { | ||
return y % 2 === 0 | ||
? [ | ||
[0, -1], | ||
[+1, -1], | ||
[-1, 0], | ||
[+1, 0], | ||
[0, +1], | ||
[+1, +1], | ||
] | ||
: [ | ||
[-1, -1], | ||
[0, -1], | ||
[-1, 0], | ||
[+1, 0], | ||
[-1, +1], | ||
[0, +1], | ||
]; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters