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

30430 - Tower of hanoi #30479

Open
Sun79 opened this issue Oct 20, 2023 · 2 comments
Open

30430 - Tower of hanoi #30479

Sun79 opened this issue Oct 20, 2023 · 2 comments
Labels
30430 answer Share answers/solutions to a question en in English

Comments

@Sun79
Copy link

Sun79 commented Oct 20, 2023

type Hanoi<N extends number, From = 'A', To = 'B', Intermediate = 'C'> = Helper<N, [], From, To, Intermediate>
type Helper<N extends number, C extends 1[], From extends unknown, To extends unknown, Intermediate extends unknown> = C['length'] extends N
  ? []
  : [...Helper<N, [...C, 1], From, Intermediate, To>, [From, To], ...Helper<N, [...C, 1], Intermediate, To, From>]
@Sun79 Sun79 added answer Share answers/solutions to a question en in English labels Oct 20, 2023
@github-actions github-actions bot added the 30430 label Oct 20, 2023
@lvjiaxuan
Copy link
Contributor

crazy

@Grothendieck42
Copy link

Grothendieck42 commented Nov 26, 2023

Similar solution

type Hanoi<
  N extends number,
  From = "A",
  To = "B",
  Intermediate = "C",
  CurrentIndex extends 1[] = []
> = CurrentIndex["length"] extends N
  ? []
  : [
      ...Hanoi<N, From, Intermediate, To, [...CurrentIndex, 1]>,
      [From, To],
      ...Hanoi<N, Intermediate, To, From, [...CurrentIndex, 1]>
    ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
30430 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

3 participants