Sum
提出詳細
type GetFirst<A extends string[]> = A extends [infer Head, ...infer _] ? Head extends string ? Head : null : null; type GetLast<A extends string[]> = A extends [...infer _, infer Last] ? Last extends string ? Last : null : null type PopLast<A extends string[]> = A extends [...infer Rest, infer _] ? Rest extends string[] ? Rest : [] : [] /** * type sumOf1Add9 = AddTable[1][9] // 10 */ type AddTable = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [4, 5, 6, 7, 8, 9, 10, 11, 12, 13], [5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [7, 8, 9, 10, 11, 12, 13, 14, 15, 16], [8, 9, 10, 11, 12, 13, 14, 15, 16, 17], [9, 10, 11, 12, 13, 14, 15, 16, 17, 18], ] type StringOneDigitMap = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} type StringOneDigit = keyof StringOneDigitMap type StringOneDigitToNum<A extends string | null> = A extends StringOneDigit ? StringOneDigitMap[A]: 0; type ToString<A extends number | string | bigint> = `${A}` /** * type splitStr = SplitString<'2034'> // ['2', '0', '3', '4'] */ type SplitString<A extends string, Result extends string[] = []> = A extends `${infer Head}${infer Tail}` ? SplitString<Tail, [...Result, Head]> : Result type AddStringOneDigits<A extends string | null, B extends string | null> = A extends null ? B extends null ? null : B :ToString<AddTable[StringOneDigitToNum<A>][StringOneDigitToNum<B>]>; type addStringOneDigits = AddStringOneDigits<'1', '9'> type Carry<A extends string | null, Split = A extends string ? SplitString<A>: null> = Split extends StringOneDigit[] ? Split['length'] extends 2 ? `${Split[0]}${AddStringOneDigits<Split[1], '1'>}` : `${AddStringOneDigits<Split[0], '1'>}` : A type _Add< A extends string[], B extends string[], Result extends string = '', Carrying extends boolean = false, res2 extends any[] = [], _Sum extends string | null = AddStringOneDigits<GetLast<A>, GetLast<B>>, Sum extends string | null = Carrying extends true ? Carry<_Sum> : _Sum, SplitSum extends StringOneDigit[] | null = Sum extends string ? SplitString<Sum>: null > = SplitSum extends StringOneDigit[] ? _Add<PopLast<A>, PopLast<B>, `${GetLast<SplitSum>}${Result}`, SplitSum['length'] extends 2 ? true : false, [...res2, {A:A,B:B,Result:Result,Carrying:Carrying}]> : Carrying extends true ? `1${Result}` : Result type Sum<A extends number | string | bigint, B extends number | string | bigint> = _Add<SplitString<ToString<A>>, SplitString<ToString<B>>>
提出日時 | 2022-06-28 10:38:29 |
---|---|
問題 | Sum |
ユーザー | waki285 |
ステータス | Wrong Answer |
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'>>, ]