Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

27133 - Square #27391

Open
jjswifty opened this issue May 14, 2023 · 0 comments
Open

27133 - Square #27391

jjswifty opened this issue May 14, 2023 · 0 comments
Labels
27133 answer Share answers/solutions to a question en in English

Comments

@jjswifty
Copy link

jjswifty commented May 14, 2023

type TupLen<T extends number, U extends number[] = []> = T extends 0 ? 
  [] : 
    U['length'] extends T ?
      U : 
        TupLen<T, [T, ...U]>;

type Abs<T extends number> = T extends 0 ? 
  T : 
  `${T}` extends `-${infer U extends number}` ? 
    U : 
      T;

type NRange<T extends number, U extends any[] = []> = U['length'] extends T ? U : NRange<T, [...U, 0]>

type Flatten<T, U extends any[] = []> = T extends []
    ? U
    : T extends [infer F, ...infer R]
    ? Flatten<R, [...U, ...Flatten<F>]>
    : [...U, T]

type TupleSum<T extends number[]> = Flatten<{ [K in keyof T]: NRange<T[K]> }>['length']

type Square<T extends number> = Abs<T> extends 100 ? 10000 : TupleSum<TupLen<Abs<T>>>;

My implementation only works with numbers between -100 and 100

@jjswifty jjswifty added answer Share answers/solutions to a question en in English labels May 14, 2023
@github-actions github-actions bot added the 27133 label May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
27133 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant