diff options
author | elioat <elioat@tilde.institute> | 2024-06-07 19:02:36 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-06-07 19:02:36 -0400 |
commit | a860fa1fc561209ee7924746e0dc0f425227c43c (patch) | |
tree | 393a347bd4d7833238548aaf1c6f6648c4e49616 | |
parent | 6adc6a232cc4a76206195451d51a233268b2280a (diff) | |
download | tour-a860fa1fc561209ee7924746e0dc0f425227c43c.tar.gz |
*
-rw-r--r-- | ts/bun/__tests__/gameOfLife.test.ts | 27 | ||||
-rw-r--r-- | ts/bun/index.ts | 75 |
2 files changed, 98 insertions, 4 deletions
diff --git a/ts/bun/__tests__/gameOfLife.test.ts b/ts/bun/__tests__/gameOfLife.test.ts new file mode 100644 index 0000000..1fef3de --- /dev/null +++ b/ts/bun/__tests__/gameOfLife.test.ts @@ -0,0 +1,27 @@ +import { step, countNeighbors } from '../index'; + +describe('Game of Life', () => { + test('countNeighbors counts the number of live neighbors', () => { + const grid = [ + [false, true, false], + [false, true, false], + [false, true, false] + ]; + expect(countNeighbors(grid, 1, 1)).toBe(2); + expect(countNeighbors(grid, 0, 0)).toBe(1); + }); + + test('step advances the game by one step', () => { + const initial = [ + [false, true, false], + [false, true, false], + [false, true, false] + ]; + const expected = [ + [false, true, false], + [false, true, false], + [false, true, false] + ]; + expect(step(initial)).toEqual(expected); + }); +}); \ No newline at end of file diff --git a/ts/bun/index.ts b/ts/bun/index.ts index a453244..bcde236 100644 --- a/ts/bun/index.ts +++ b/ts/bun/index.ts @@ -1,6 +1,73 @@ -const a = (b:string):number => { - console.log(b); - return 123; +type Grid = boolean[][]; + +export function printGrid(grid: Grid): void { + grid.forEach(row => console.log(row.map(cell => cell ? '1' : '0').join(' '))); + console.log('\n'); +} + +export function countNeighbors(grid: Grid, x: number, y: number): number { + const neighborOffsets = [ + [-1, -1], [-1, 0], [-1, 1], + [0, -1], [0, 1], + [1, -1], [1, 0], [1, 1] + ]; + + return neighborOffsets.reduce((count, [dx, dy]) => { + const nx = x + dx; + const ny = y + dy; + if (nx >= 0 && ny >= 0 && nx < grid.length && ny < grid[0].length && grid[nx][ny]) { + return count + 1; + } + return count; + }, 0); +} + +export function step(grid: Grid): Grid { + return grid.map((row, x) => + row.map((cell, y) => { + const neighbors = countNeighbors(grid, x, y); + return neighbors === 3 || (neighbors === 2 && cell); + }) + ); } -console.log(a('Hello World!')); \ No newline at end of file +export function simulate(initial: Grid, steps: number): void { + let grid = initial; + for (let i = 0; i < steps; i++) { + printGrid(grid); + grid = step(grid); + } +} + +const initial: Grid = [ + [false, true, false], + [false, true, false], + [false, true, false] +]; + +const flyer: Grid = [ + [false, false, false, false, false, false], + [false, false, false, false, false, false], + [false, false, false, false, false, false], + [false, false, false, true, false, false], + [false, false, false, false, true, false], + [false, false, true, true, true, false], + [false, false, false, false, false, false], + [false, false, false, false, false, false], + [false, false, false, false, false, false] +]; + +const toad: Grid = [ + [false, false, false, false, false, false], + [false, false, false, false, false, false], + [false, false, false, true, false, false], + [false, true, false, false, true, false], + [false, true, false, false, true, false], + [false, false, true, false, false, false], + [false, false, false, false, false, false] +]; + + +simulate(initial, 5); +simulate(flyer, 5); +simulate(toad, 5); \ No newline at end of file |