-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperCA.ts
183 lines (141 loc) · 5 KB
/
SuperCA.ts
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
// An attempt at asynchronusly calculating a cellular automata
// such that it can be computed in a less strict order
// in this version of the program the only restriction on where or not a cell can be computed
// is that it's three parents must be computed first
// this means we do not have to compute the entire generation before beginning to compute the next
// though obviously we can never complete generation N before completing generation N-1
type RuleArray = [number, number, number, number, number, number, number, number];
async function readInputsFromFile(filePath: string): Promise<[number, string, number]> {
const text = await Deno.readTextFile(filePath);
const lines = text.split('\n');
const ruleNumber = parseInt(lines[0].trim(), 10);
const initialConditions = lines[1].trim();
const generations = parseInt(lines[2].trim(), 10);
return [ruleNumber, initialConditions, generations];
}
const [ruleNumber, initialConditions, generations] = await readInputsFromFile('input.txt');
console.log(`Rule Number: ${ruleNumber}`);
console.log(`Initial Conditions: ${initialConditions}`);
console.log(`Generations: ${generations}`);
type Cell = {
state: 'alive' | 'dead' | 'uncomputed'
position: {
zeroOffset: number, // negative is left, p
generation: number, // always positive
},
parents: [string, string, string] | null, // these three strings can be used to look up the parent cells in the state map
}
type DefaultZero = "defaultZero"
const state: Map<string, Cell> = new Map()
state.set('0|0', {
state: 'alive',
position: {
zeroOffset: 0,
generation: 0,
},
parents: null,
})
const generation1 = [] as Cell[]
const KeyFromPosition = (p: Cell['position']): string => {
return `${p.zeroOffset}|${p.generation}`
}
const KeyPointsToMemberOfGenerationZero = (k: string): boolean => {
const [zeroOffset, generation] = k.split('|')
return generation === '0'
}
const CalculateParentsFromPosition = (a: Cell): Cell => {
const { zeroOffset, generation } = a.position
const parentGeneration = generation - 1
const p1 = `${a.position.zeroOffset - 1}|${parentGeneration}`
const p2 = `${a.position.zeroOffset}|${parentGeneration}`
const p3 = `${a.position.zeroOffset + 1}|${parentGeneration}`
return {
...a,
parents: [p1, p2, p3],
}
}
const FindCell = (s: string): Cell | DefaultZero => {
const result = state.get(s)
if (result === undefined) {
// if s is part of generation 0
if (KeyPointsToMemberOfGenerationZero(s))
return "defaultZero" as DefaultZero
throw new Error(`Cell at position ${s} not found in state map`)
// otherwise we need to compute the cell
}
return result as Cell
}
const FindParents = (a: Cell): [Cell | DefaultZero, Cell | DefaultZero, Cell | DefaultZero] => {
const { parents } = a
if (parents === null) {
return FindParents(CalculateParentsFromPosition(a))
}
const [p1, p2, p3] = parents
return [
FindCell(p1),
FindCell(p2),
FindCell(p3),
]
}
// const parents = FindParents(t1)
// console.log(parents)
function calculateCell(pState: string, rule: RuleArray): number {
const ruleMap: Record<string, number> = {
"111": rule[0],
"110": rule[1],
"101": rule[2],
"100": rule[3],
"011": rule[4],
"010": rule[5],
"001": rule[6],
"000": rule[7]
};
return ruleMap[pState];
}
function ruleToBinaryArray(ruleNumber: number): RuleArray {
let binaryString = ruleNumber.toString(2).padStart(8, '0');
return binaryString.split('').map(bit => parseInt(bit, 10)) as RuleArray;
}
const ParentsToStateString = (p: [Cell | DefaultZero, Cell | DefaultZero, Cell | DefaultZero]): string => {
const ParentToBit = (p: Cell | DefaultZero): string => {
if (p === "defaultZero")
return '0'
return p.state === 'alive' ? '1' : '0'
}
return p.map(ParentToBit).join('')
}
const ComputeCell = (a: Cell): Cell => {
const parents = FindParents(a)
const stateString = ParentsToStateString(parents)
const computedState = calculateCell(stateString, ruleToBinaryArray(ruleNumber)) === 1 ? 'alive' : 'dead'
const updatedCell: Cell = {
...a,
state: computedState,
}
state.set(KeyFromPosition(updatedCell.position), updatedCell)
return updatedCell
}
// const t1: Cell = {
// state: 'uncomputed',
// position: {
// zeroOffset: -1,
// generation: 1,
// },
// parents: null,
// }
// state.set(KeyFromPosition(t1.position), t1)
const g1 : Cell[] = Array.from({ length: 3 }, (_, i) => ({
state: 'uncomputed',
position: {
zeroOffset: i - 1,
generation: 1,
},
parents: null,
}) as Cell)
// const computed = ComputeCell(t1)
// console.log(computed)
console.log(`Before computing generation 1`)
console.log(state)
const computed = g1.map(ComputeCell)
console.log(`After computing generation 1`)
console.log(state)