-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (133 loc) · 2.98 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
type stack []string
func (s *stack) Push(a string) {
*s = append(*s, a)
}
func (s *stack) Peek() string {
return (*s)[len(*s)-1]
}
func (s *stack) Pop() string {
last := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return last
}
func (s stack) List() {
for _, line := range s {
fmt.Print("[", line, "]")
}
fmt.Println()
}
func main() {
step1("small.txt")
step1("input.txt")
step2("small.txt")
step2("input.txt")
}
func step1(filename string) {
stacks, moveLines := initStacks(filename)
for _, moveLine := range moveLines {
newMoves := strings.Split(moveLine, " ")
times := atoi(newMoves[1])
from := atoi(newMoves[3]) - 1
to := atoi(newMoves[5]) - 1
for i := 0; i < times; i++ {
stacks[to].Push(stacks[from].Pop())
}
}
result := ""
for i := 0; i < len(stacks); i++ {
result += stacks[i].Peek()
}
fmt.Println("step1 (", filename, ")-->", result)
}
func step2(filename string) {
stacks, moveLines := initStacks(filename)
for _, moveLine := range moveLines {
newMoves := strings.Split(moveLine, " ")
times := atoi(newMoves[1])
from := atoi(newMoves[3]) - 1
to := atoi(newMoves[5]) - 1
pivotIndex := len(stacks[from]) - times
slice := stacks[from][pivotIndex:]
stacks[from] = stacks[from][:pivotIndex]
for i := 0; i < len(slice); i++ {
stacks[to].Push(slice[i])
}
}
result := ""
for i := 0; i < len(stacks); i++ {
result += stacks[i].Peek()
}
fmt.Println("step2 (", filename, ")-->", result)
}
func initStacks(filename string) ([]stack, []string) {
foundEmptyLine := false
stackLines := []string{}
moveLines := []string{}
for _, line := range strings.Split(load(filename), "\n") {
if line == "" {
foundEmptyLine = true
continue
}
if foundEmptyLine {
moveLines = append(moveLines, line)
} else {
stackLines = append(stackLines, line)
}
}
// really, no way to easily reverse an array in go... come on...
for i, j := 0, len(stackLines)-1; i < j; i, j = i+1, j-1 {
stackLines[i], stackLines[j] = stackLines[j], stackLines[i]
}
firstLine := stackLines[0]
indexes := []int{}
for i := 0; i < len(firstLine); i++ {
if string(firstLine[i]) != " " {
indexes = append(indexes, i)
}
}
maxStack := len(indexes)
stacks := []stack{}
for i := 0; i < maxStack; i++ {
stacks = append(stacks, stack{})
}
stackLines = stackLines[1:]
for j := 0; j < len(stackLines); j++ {
line := stackLines[j]
for i := 0; i < len(indexes); i++ {
if string(line[indexes[i]]) != " " {
stacks[i].Push(string(line[indexes[i]]))
}
}
}
return stacks, moveLines
}
func printStack(stacks []stack) {
for i := 0; i < len(stacks); i++ {
fmt.Println("stack", i)
stacks[i].List()
}
fmt.Println()
}
func load(filename string) string {
text, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return string(text)
}
func atoi(line string) int {
number, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return number
}