-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
254 lines (218 loc) Β· 5.4 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"bufio"
"fmt"
"os"
"path"
"path/filepath"
"strings"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println(printScannerAsTree(scanner))
}
func printScannerAsTree(s *bufio.Scanner) string {
dummyRoot := &node{
name: ".",
parent: nil,
children: []*node{},
}
for s.Scan() {
path := filepath.Clean(s.Text())
dummyRoot.insert(path)
}
if err := s.Err(); err != nil {
panic(err)
}
nLeafNodes, nInternalNodes := 0, 0
dummyRoot.dfs(func(n *node) {
// don't count the dummy root as a file or a dir
if n == dummyRoot {
return
}
if len(n.children) > 0 {
nInternalNodes = nInternalNodes + 1
} else {
nLeafNodes = nLeafNodes + 1
}
})
root := dummyRoot
// if the dummy root only has a single child, we can use that
// as the root for printing instead
for len(root.children) == 1 {
nextRoot := root.children[0]
nextRoot.parent = nil
nextRoot.name = path.Join(root.name, nextRoot.name)
root = nextRoot
}
tree := root.PrintAsTree()
itemCounts := fmt.Sprintf("%d %s, %d %s", nInternalNodes, directories(nInternalNodes), nLeafNodes, files(nLeafNodes))
return fmt.Sprintf("%s\n%s\n", tree, itemCounts)
}
func directories(n int) string {
if n == 1 {
return "directory"
}
return "directories"
}
func files(n int) string {
if n == 1 {
return "file"
}
return "files"
}
type node struct {
parent *node
children []*node
name string
}
const (
verticalPipe = 'β'
horizontalPipe = 'β'
cornerPipe = 'β'
verticalPipeWithOffshoot = 'β'
)
// PrintAsTree prints the node & all it's children as a `tree`-style tree
func (n *node) PrintAsTree() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s\n", n.name))
n.printAsTreeHelper(&sb)
return sb.String()
}
func (n *node) printAsTreeHelper(sb *strings.Builder) {
for _, child := range n.children {
for _, parent := range child.findParents() {
if parent.isRoot() {
continue
}
var connChar rune
if parent.isLastChild() {
connChar = ' '
} else {
connChar = verticalPipe
}
sb.WriteString(fmt.Sprintf("%c%s", connChar, spaces(3)))
}
var connChar rune
if child.isLastChild() {
connChar = cornerPipe
} else {
connChar = verticalPipeWithOffshoot
}
sb.WriteString(fmt.Sprintf("%c%c%c %s\n", connChar, horizontalPipe, horizontalPipe, child.name))
child.printAsTreeHelper(sb)
}
}
// insert inserts all nodes represented by the supplied path. A node is added for each segment.
func (n *node) insert(path string) {
current := n
segments := strings.Split(path, "/")
for _, segment := range segments {
next := current.findChildWithName(segment)
// if there is no node at this level with a name matching the current
// segment, create a new node and add it as a child of "current"
if next == nil {
next = &node{
name: segment,
parent: current,
children: []*node{},
}
current.children = append(current.children, next)
}
current = next
}
}
func (n *node) dfs(visit func(*node)) {
stack := []*node{n}
pop := func() (next *node) {
next, stack = stack[len(stack)-1], stack[:len(stack)-1]
return next
}
seen := make(map[string]bool)
for len(stack) > 0 {
next := pop()
// a nodes path uniquely identifies it
p := next.printPath()
if seen[p] {
continue
}
visit(next)
stack = append(stack, next.children...)
seen[p] = true
}
}
// findParents returns an array containing all the nodes parents. The parent nodes are
// returned in order of highest to lowest, and the root node is skipped. That is, the
// first node will be the parent node closest to the root.
func (n *node) findParents() []*node {
parents := []*node{}
current := n
for current.parent != nil {
parents = append([]*node{current.parent}, parents...)
current = current.parent
}
return parents
}
// findChildWithName finds a child with a name matching the supplied name inside the node's
// array of child nodes
func (n *node) findChildWithName(name string) *node {
for _, child := range n.children {
if child.name == name {
return child
}
}
return nil
}
// isRoot returns true if the node has no parent
func (n *node) isRoot() bool {
return n.parent == nil
}
// isLastChild returns true if n is the final node in the parent node's array of children
func (n *node) isLastChild() bool {
if n.parent == nil {
return false
}
return n.position() == len(n.parent.children)-1
}
// position returns the index of the current node in the parent node's array of children
func (n *node) position() int {
i := n.parent.indexOf(n)
if i == -1 {
panic("n is not a child of its parent")
}
return i
}
// indexOf returns the index of `target` in the `children` array of `n`, if itexists. Otherwise,
// it returns -1
func (n *node) indexOf(target *node) int {
for i, child := range n.children {
if child == target {
return i
}
}
return -1
}
func (n *node) printPath() string {
parents := n.findParents()
pathParts := make([]string, len(parents)+1)
for _, parent := range parents {
pathParts = append(pathParts, parent.name)
}
pathParts = append(pathParts, n.name)
return path.Join(pathParts...)
}
// spaces returns a string of length n containing only space characters
func spaces(n int) string {
s := make([]byte, n)
for i := 0; i < n; i++ {
s[i] = ' '
}
return string(s)
}
func newNode(name string, parent *node, children []*node) *node {
return &node{
parent,
children,
name,
}
}