about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ts/bun/__tests__/gameOfLife.test.ts2
-rw-r--r--ts/bun/index.ts43
-rw-r--r--ts/bun/life.ts40
-rw-r--r--ts/bun/package.json9
4 files changed, 50 insertions, 44 deletions
diff --git a/ts/bun/__tests__/gameOfLife.test.ts b/ts/bun/__tests__/gameOfLife.test.ts
index 1fef3de..21ad86d 100644
--- a/ts/bun/__tests__/gameOfLife.test.ts
+++ b/ts/bun/__tests__/gameOfLife.test.ts
@@ -1,4 +1,4 @@
-import { step, countNeighbors } from '../index';
+import { step, countNeighbors } from '../life';
 
 describe('Game of Life', () => {
     test('countNeighbors counts the number of live neighbors', () => {
diff --git a/ts/bun/index.ts b/ts/bun/index.ts
index 6eae1c1..51938d2 100644
--- a/ts/bun/index.ts
+++ b/ts/bun/index.ts
@@ -1,43 +1,4 @@
-type Grid = number[][];
-
-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) ? 1 : 0;
-        })
-    );
-}
-
-export function simulate(initial: Grid, steps: number): void {
-    let grid = initial;
-    Array.from({ length: steps }).forEach(() => {
-        printGrid(grid);
-        grid = step(grid);
-    });
-}
+import { type Grid, simulate } from './life';
 
 const initial: Grid = [
     [0, 1, 0],
@@ -74,4 +35,4 @@ console.log('Flyer:');
 simulate(flyer, 5);
 
 console.log('Toad:');
-simulate(toad, 5);
+simulate(toad, 5);
\ No newline at end of file
diff --git a/ts/bun/life.ts b/ts/bun/life.ts
new file mode 100644
index 0000000..965e422
--- /dev/null
+++ b/ts/bun/life.ts
@@ -0,0 +1,40 @@
+export type Grid = number[][];
+
+export function printGrid(grid: Grid): void {
+    grid.forEach(row => console.log(row.map(cell => cell ? '🟢' : '⚪️').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) ? 1 : 0;
+        })
+    );
+}
+
+export function simulate(initial: Grid, steps: number): void {
+    let grid = initial;
+    Array.from({ length: steps }).forEach(() => {
+        printGrid(grid);
+        grid = step(grid);
+    });
+}
diff --git a/ts/bun/package.json b/ts/bun/package.json
index 480af6b..6941cd5 100644
--- a/ts/bun/package.json
+++ b/ts/bun/package.json
@@ -1,11 +1,16 @@
 {
-  "name": "bun",
-  "module": "index.ts",
+  "name": "life",
+  "description": "Conway's Game of Life",
+  "module": "life.ts",
   "type": "module",
   "devDependencies": {
     "@types/bun": "latest"
   },
   "peerDependencies": {
     "typescript": "^5.0.0"
+  },
+  "scripts": {
+    "run": "bun run index.ts",
+    "test": "bun test"
   }
 }
\ No newline at end of file