Created
February 14, 2020 20:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
Author
turnipsoup
commented
Feb 14, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment