From a860fa1fc561209ee7924746e0dc0f425227c43c Mon Sep 17 00:00:00 2001 From: elioat Date: Fri, 7 Jun 2024 19:02:36 -0400 Subject: * --- ts/bun/__tests__/gameOfLife.test.ts | 27 +++++++++++++ ts/bun/index.ts | 75 +++++++++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 ts/bun/__tests__/gameOfLife.test.ts (limited to 'ts') 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 -- cgit 1.4.1-2-gfad0 r Anselm R. Garbe <garbeam@wmii.de> 2006-07-13 10:05:46 +0200 committer Anselm R. Garbe <garbeam@wmii.de> 2006-07-13 10:05:46 +0200 another change' href='/acidbong/suckless/dwm/commit/dwm.1?h=5.3.1&id=f33a30a2074fd1782e6bf2251801a0bcd39d2b27'>f33a30a ^
1076f2b
4ae1105 ^
1076f2b

4ae1105 ^
b439892 ^
dc5c070 ^

e6cbe9c ^
0c3544d ^
dc5c070 ^



e6cbe9c ^
dc5c070 ^


e6cbe9c ^
e743836 ^
c518345 ^


b439892 ^
c2ac851 ^

7e597ae ^
901b3ed ^
1076f2b

901b3ed ^
0c3544d ^
2b35fae ^

a3e3f0b ^

2b35fae ^
3e06ede ^

2dd5212 ^





292ccc4 ^
05fbbbd ^





d4b7a9a ^
3e06ede ^


2b35fae ^
72655f0 ^
df74b26 ^


72655f0 ^
df74b26 ^
72655f0 ^
3e06ede ^
72655f0 ^
df74b26 ^
3af6434 ^
3e06ede ^
3af6434 ^
df74b26 ^








72655f0 ^

3e06ede ^
72655f0 ^
1549faf ^

3af6434 ^

3e06ede ^
1549faf ^
df74b26 ^





45aea23 ^



df74b26 ^






1549faf ^
df74b26 ^


1549faf ^
ba59bc8 ^
3e06ede ^


4bb89e2 ^
df74b26 ^


2b35fae ^
0c3544d ^
0e5c819 ^
4bd0d33 ^
0e5c819 ^
df74b26 ^


0e5c819 ^
4bd0d33 ^




df74b26 ^


4bd0d33 ^
0e5c819 ^
4bd0d33 ^
0e5c819 ^
df74b26 ^


0c3544d ^

dc5c070 ^

4bb89e2 ^
60b3dce ^
3e06ede ^
4bb89e2 ^




3e06ede ^
5ef6ef1 ^

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