Last active
August 21, 2024 05:26
-
-
Save rexim/7c966737e9a2737c7e17bdfc97ebc43a to your computer and use it in GitHub Desktop.
Rule110 Entirely in TypeScript Type System
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
// tsc -noErrorTruncation rule110.ts | sed 's/\[/\n[/g' | |
type Tail<Xs extends Array<any>> = Xs extends [infer _, ...infer Rest] ? Rest : never; | |
type Head<Xs extends Array<any>> = Xs extends [infer X, ...infer _] ? X : never; | |
type Pair<X, Xs extends Array<X>> = [X, ...Xs] | |
type Reverse< | |
Items extends Array<any>, | |
Acc extends Array<any> = [] | |
> = Items extends [] ? Acc : Reverse<Tail<Items>, Pair<Items[0], Acc>>; | |
type Cell = 0 | 1; | |
type Rule110<A extends Cell, B extends Cell, C extends Cell> = { | |
"000": 0, | |
"001": 1, | |
"010": 1, | |
"011": 1, | |
"100": 0, | |
"101": 1, | |
"110": 1, | |
"111": 0, | |
}[`${A}${B}${C}`] | |
// 0 0 [1 0] | |
// 0 | |
type Start<Xs extends Array<Cell>> = | |
Xs extends [infer X extends Cell, ...infer Rest extends Array<Cell>] | |
? [X, ...Next<X, Rest>] | |
: []; | |
type Next< | |
X extends Cell, | |
Items extends Array<Cell>, | |
> = Items extends [infer Y extends Cell, | |
infer Z extends Cell, | |
...infer Rest extends Array<Cell>] | |
? [Rule110<X, Y, Z>, ...Next<Y, [Z, ...Rest]>] | |
: Items extends [infer Y extends Cell] | |
? [Y] | |
: []; | |
type Gen<N extends Array<any>, State extends Array<Cell>> = | |
N extends [] | |
? [] | |
: [State, ...Gen<Tail<N>, Start<State>>] | |
type N = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | |
let x: Gen<N, [...N, 1, 0]> = 69; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment