Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Created February 14, 2020 20:39
Show Gist options
  • Select an option

  • Save turnipsoup/18d0cce0e0dd773c55ed03aa33107f1d to your computer and use it in GitHub Desktop.

Select an option

Save turnipsoup/18d0cce0e0dd773c55ed03aa33107f1d to your computer and use it in GitHub Desktop.
The hanoi problem recursion solver from this computerfile video (but ported to Go): https://www.youtube.com/watch?v=8lhxIOAfDss&t=629s
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
total_discs, _ := strconv.Atoi(os.Args[1])
hanoi(total_discs, "A", "B", "C")
}
func move(pole_one string, pole_two string) {
fmt.Printf("Move one disc from %s to %s\n", pole_one, pole_two)
}
func hanoi(total_starting_discs int, start_pole string, helper_pole string, target_pole string) {
if total_starting_discs == 0 {
total_starting_discs = total_starting_discs // Do nothing essentially
} else {
hanoi(total_starting_discs - 1, start_pole, target_pole, helper_pole)
move(start_pole, target_pole)
hanoi(total_starting_discs - 1, helper_pole, start_pole, target_pole)
}
}
@turnipsoup
Copy link
Author

go build hanoi.go
./hanoi 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment