Sum
提出詳細
type AddLUT = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], [2, 3, 4, 5, 6, 7, 8, 9, 0, 1], [3, 4, 5, 6, 7, 8, 9, 0, 1, 2], [4, 5, 6, 7, 8, 9, 0, 1, 2, 3], [5, 6, 7, 8, 9, 0, 1, 2, 3, 4], [6, 7, 8, 9, 0, 1, 2, 3, 4, 5], [7, 8, 9, 0, 1, 2, 3, 4, 5, 6], [8, 9, 0, 1, 2, 3, 4, 5, 6, 7], [9, 0, 1, 2, 3, 4, 5, 6, 7, 8], ] type CarryLUT = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] type Digit = AddLUT[0][number] type CarryDigit = 0 | 1 type AddResult = [Digit, CarryDigit] type ValueOf<R extends AddResult> = R[0] type CarryOf<R extends AddResult> = R[1] type OR<A extends CarryDigit, B extends CarryDigit> = A extends 1 ? 1 : B extends 1 ? 1 : 0 type AddDigitHalf<A extends Digit, B extends Digit> = [AddLUT[A][B], CarryLUT[A][B]] type AddDigit<A extends Digit, B extends Digit, C extends CarryDigit = 0, _AB extends AddResult = AddDigitHalf<A, B>, _ABC extends AddResult = AddDigitHalf<ValueOf<_AB>, C>> = [ValueOf<_ABC>, OR<CarryOf<_AB>, CarryOf<_ABC>>] type Add<A extends string, B extends string, C extends CarryDigit = 0, _Res extends string = ""> = A extends `${infer A1 extends Digit}${infer ARest}` ? B extends `${infer B1 extends Digit}${infer BRest}` ? Add<ARest, BRest, CarryOf<AddDigit<A1, B1, C>>, `${_Res}${AddDigit<A1, B1, C>[0]}`> : Add<ARest, "", AddDigit<A1, 0, C>[1], `${_Res}${AddDigit<A1, 0, C>[0]}`> : B extends `${infer B1 extends Digit}${infer BRest}` ? Add<"", BRest, AddDigit<0, B1, C>[1], `${_Res}${AddDigit<0, B1, C>[0]}`> : C extends 1 ? `${_Res}1` : _Res type Reverse<T extends string, O extends string = ""> = T extends `${infer L}${infer R}` ? Reverse<R, `${L}${O}`> : T extends string ? `${T}${O}` : O type Sum<A extends string | number | bigint, B extends string | number | bigint> = Reverse<Add<Reverse<`${A}`>, Reverse<`${B}`>>>
提出日時 | 2024-09-13 07:59:19 |
---|---|
問題 | Sum |
ユーザー | ookkoouu |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<Sum<2, 3>, '5'>>, Expect<Equal<Sum<'13', '21'>, '34'>>, Expect<Equal<Sum<'328', 7>, '335'>>, Expect<Equal<Sum<1_000_000_000_000n, '123'>, '1000000000123'>>, Expect<Equal<Sum<9999, 1>, '10000'>>, Expect<Equal<Sum<4325234, '39532'>, '4364766'>>, Expect<Equal<Sum<728, 0>, '728'>>, Expect<Equal<Sum<'0', 213>, '213'>>, Expect<Equal<Sum<0, '0'>, '0'>>, ]