-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.ts
179 lines (170 loc) Β· 5.33 KB
/
solution.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
type Dasher = "π¨";
type Dancer = "π";
type Prancer = "π¦";
type Vixen = "π";
type Comet = "βοΈ";
type Cupid = "β€οΈ";
type Donner = "π©οΈ";
type Blitzen = "β‘";
type Rudolph = "π΄";
type Reindeer =
| Dasher
| Dancer
| Prancer
| Vixen
| Comet
| Cupid
| Donner
| Blitzen
| Rudolph;
type BoardLineElementType = Reindeer[]; // ['π¦', 'π΄', 'π']
type BoardLineType = BoardLineElementType[]; // [ [], [], []]
type BoardType = BoardLineType[]; //[ [ [] ] ]
type ValidationAccumulatorType = Record<number, true>;
type ValidateHorizontalLine<
Board extends BoardLineType,
Index extends number = 0,
Accumulator extends BoardLineElementType = []
> = Accumulator["length"] extends 9
? ValidateLine<Accumulator>
: ValidateHorizontalLine<
Board,
Index,
[...Accumulator, Board[Accumulator["length"]][Index]]
>;
type ValidateHorizontalLines<
Board extends BoardLineType,
Accumulator extends number[] = []
> = Accumulator["length"] extends 9
? true
: ValidateHorizontalLine<Board, Accumulator["length"]> extends true
? ValidateHorizontalLines<Board, [...Accumulator, 0]>
: false;
type ValidateVerticalLinesAndFlat<
Board extends BoardType,
Accumulator extends BoardLineType = []
> = Board extends [
infer Current extends BoardLineType,
...infer Rest extends BoardType
]
? [
...Current[0],
...Current[1],
...Current[2]
] extends infer FlatedLine extends BoardLineElementType
? ValidateLine<FlatedLine> extends true
? ValidateVerticalLinesAndFlat<Rest, [...Accumulator, FlatedLine]>
: []
: []
: Accumulator;
type ValidateLine<
BoardLine extends BoardLineElementType,
Accumulator extends ValidationAccumulatorType = {}
> = BoardLine extends [
infer Current extends Reindeer,
...infer Rest extends BoardLineElementType
]
? ValidateLine<Rest, Accumulator & { [Key in Current]: true }>
: Accumulator extends {
"π¨": true;
"π": true;
"π¦": true;
"π": true;
"βοΈ": true;
"β€οΈ": true;
"π©οΈ": true;
"β‘": true;
"π΄": true;
}
? true
: false;
type ValidateAreas<Board extends BoardType> = Board extends [
[
infer H1 extends BoardLineElementType,
infer H2 extends BoardLineElementType,
infer H3 extends BoardLineElementType
],
[
infer H4 extends BoardLineElementType,
infer H5 extends BoardLineElementType,
infer H6 extends BoardLineElementType
],
[
infer H7 extends BoardLineElementType,
infer H8 extends BoardLineElementType,
infer H9 extends BoardLineElementType
],
[
infer H10 extends BoardLineElementType,
infer H11 extends BoardLineElementType,
infer H12 extends BoardLineElementType
],
[
infer H13 extends BoardLineElementType,
infer H14 extends BoardLineElementType,
infer H15 extends BoardLineElementType
],
[
infer H16 extends BoardLineElementType,
infer H17 extends BoardLineElementType,
infer H18 extends BoardLineElementType
],
[
infer H19 extends BoardLineElementType,
infer H20 extends BoardLineElementType,
infer H21 extends BoardLineElementType
],
[
infer H22 extends BoardLineElementType,
infer H23 extends BoardLineElementType,
infer H24 extends BoardLineElementType
],
[
infer H25 extends BoardLineElementType,
infer H26 extends BoardLineElementType,
infer H27 extends BoardLineElementType
]
]
? ValidateLine<[...H1, ...H4, ...H7]> extends true
? ValidateLine<[...H2, ...H5, ...H8]> extends true
? ValidateLine<[...H3, ...H6, ...H9]> extends true
? ValidateLine<[...H10, ...H13, ...H16]> extends true
? ValidateLine<[...H11, ...H14, ...H17]> extends true
? ValidateLine<[...H12, ...H15, ...H18]> extends true
? ValidateLine<[...H19, ...H22, ...H25]> extends true
? ValidateLine<[...H20, ...H23, ...H26]> extends true
? ValidateLine<[...H21, ...H24, ...H27]> extends true
? true
: false
: false
: false
: false
: false
: false
: false
: false
: false
: never;
type Validate<Board extends BoardType> =
ValidateVerticalLinesAndFlat<Board> extends infer FlatedBoard extends BoardLineType
? FlatedBoard["length"] extends 9
? ValidateHorizontalLines<FlatedBoard> extends true
? ValidateAreas<Board>
: false
: false
: false;
false;
// === TESTING ===
type TestedBoard = [
[["π¦", "π΄", "π"], ["π©οΈ", "βοΈ", "π¨"], ["β‘", "β€οΈ", "π"]],
[["π", "β‘", "π¨"], ["β€οΈ", "π", "π΄"], ["βοΈ", "π©οΈ", "π¦"]],
[["βοΈ", "π©οΈ", "β€οΈ"], ["β‘", "π", "π¦"], ["π", "π΄", "π¨"]],
[["π©οΈ", "π", "π΄"], ["π¦", "π¨", "β‘"], ["π", "βοΈ", "β€οΈ"]],
[["β€οΈ", "βοΈ", "β‘"], ["π", "π©οΈ", "π"], ["π¦", "π¨", "π΄"]],
[["π¨", "π", "π¦"], ["βοΈ", "π΄", "β€οΈ"], ["π©οΈ", "π", "β‘"]],
[["π", "π¨", "π"], ["π΄", "π¦", "βοΈ"], ["β€οΈ", "β‘", "π©οΈ"]],
[["π΄", "β€οΈ", "βοΈ"], ["π", "β‘", "π©οΈ"], ["π¨", "π¦", "π"]],
[["β‘", "π¦", "π©οΈ"], ["π¨", "β€οΈ", "π"], ["π΄", "π", "βοΈ"]]
];
type TestType = Validate<TestedBoard>;
export { Validate };