-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanoi.go
executable file
·165 lines (149 loc) · 5.4 KB
/
hanoi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// The way to solve this is quite simple but does differ slightly for N = odd or even numbers of rings.
//
// At each move you do either a) or b):
//
// a) move the "1" value to the peg to the right, wrapping around to the first peg if needed
//
// b) make the only other legal move
//
// And then repeat either a) or b) for (2 ^ numrings) - 1.
//
// So for N=3, you would do the above steps 7 times.
//
// The catch that I alluded to earlier is that for N == odd (3,5,...), you will need to repeat this
// entire algorithm one more time as the above will only move the rings one peg to the right.
//
package main
import (
"fmt"
)
//
// Print the tower so we can check our progress
//
func print_tower(pegs [][]int, nrings int) {
npegs := len(pegs)
for y := 0; y < nrings; y++ {
h := nrings - y
for x := 0; x < npegs; x++ {
if len(pegs[x]) >= h {
fmt.Printf("%d ", pegs[x][len(pegs[x]) - h])
} else {
fmt.Printf("| ")
}
}
fmt.Println("")
}
fmt.Println("-----")
}
func solve_tower(nrings int, npegs int) {
pegs := make([][]int, npegs)
//
// Create empty slices for the pegs
//
for i := 0; i < nrings; i++ {
pegs[0] = make([]int, 0)
}
//
// push the rings on
//
for i := 0; i < nrings; i++ {
pegs[0] = append(pegs[0], i + 1)
}
//
// For N == odd numbers we will need to repeat this twice
//
for tries := 0; tries < 1 + nrings % 2; tries++ {
print_tower(pegs, nrings)
move_peg_one_right := true
//
// Repeat the steps a) or b) for 2^N-1 times
//
for moves := 0; moves < (1 << nrings) - 1; moves++ {
//
// step a)
//
if move_peg_one_right {
for peg := 0; peg < npegs; peg++ {
if len(pegs[peg]) > 0 {
if (pegs[peg][0] == 1) {
next_peg := (peg + 1) % npegs
popped := pegs[peg][0]
pegs[peg] = pegs[peg][1:]
pegs[next_peg] = append([]int{popped}, pegs[next_peg]...)
fmt.Printf("Moving value 1 from peg %d to peg %d\n\n", peg + 1, next_peg + 1)
break
}
}
}
} else {
//
// step b)
//
moved_a_ring := false
for peg := 0; peg < npegs; peg++ {
//
// Look for a ring on a peg to move
//
if len(pegs[peg]) > 0 {
value := pegs[peg][0]
//
// Don't move the ring value "1" as we move that in a)
//
if (value != 1) {
for n := 0; n < npegs; n++ {
//
// The next peg is the one to the right of this peg. If we reach the last peg then we
// need to move to the first peg.
//
next_peg := (peg + n) % npegs
//
// Don't move to the same peg; that would be silly
//
if (next_peg == peg) {
continue
}
//
// If the destination peg is empty, move there
//
if len(pegs[next_peg]) == 0 {
pegs[peg] = pegs[peg][1:]
pegs[next_peg] = append([]int{value}, pegs[next_peg]...)
moved_a_ring = true
fmt.Printf("Moving value %d from peg %d to empty peg %d\n\n", value, peg + 1, next_peg + 1)
break
} else if (value < pegs[next_peg][0]) {
//
// Else if the destination peg has a lower value, move there
//
pegs[peg] = pegs[peg][1:]
pegs[next_peg] = append([]int{value}, pegs[next_peg]...)
moved_a_ring = true
fmt.Printf("Moving < value %d from peg %d to peg %d dest %d\n\n", value, peg + 1, next_peg + 1, pegs[next_peg][0])
break
}
}
}
}
if (moved_a_ring) {
break
}
}
if ! moved_a_ring {
panic("Error, failed to move")
}
}
print_tower(pegs, nrings)
//
// Alternate between a) and b)
//
move_peg_one_right = ! move_peg_one_right
}
fmt.Printf("Finished pass\n\n")
}
}
func main() {
nrings := 3
npegs := 3
solve_tower(nrings, npegs)
}