-
Notifications
You must be signed in to change notification settings - Fork 0
/
codejamfilefixit.go
255 lines (166 loc) · 5.54 KB
/
codejamfilefixit.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
255
//File: codejamfilefixit.go
//Author: Gary Bezet
//Date: 2016-06-08
//Desc: This program is designed to solve Google Code Jam "File Fix-it"
//Problem: https://code.google.com/codejam/contest/635101/dashboard
//Results: (native linux) SmallProblem:8.099313ms LargeProblem:46.189754ms
ms
package main
//imports
import (
"fmt"
"flag"
"os"
"bufio"
"strconv"
"strings"
"time"
)
//global variables
var infileopt, outfileopt string //input and output filenames
var infile, outfile *os.File //input and output file pointers
var totalcases int //number of cases
var testcases []Testcase
//structures
type Testcase struct {
num int
curdirs []string
newdirs []string
solution int
}
//program entry point
func main() {
starttime := time.Now() //start time for stats
defer infile.Close()
defer outfile.Close()
initflags() //initialize the command line args
openFiles() //open the files
processFile()
for _, v := range testcases { //solve all cases
fmt.Fprintf(outfile, "Case #%d: %d\n", v.num, v.solve())
}
printErrln("done! Processed", totalcases, " in ", time.Now().Sub(starttime))
}
//get the flags from command line
func initflags() {
flag.StringVar(&infileopt, "if", "", "Input file (required)")
flag.StringVar(&outfileopt, "of", "-", "Output file, defaults to stdout" )
flag.Parse()
if infileopt == "" {
printErrln("You must supply an input file\n")
flag.PrintDefaults()
os.Exit(1)
}
}
//print error to console
func printErrln( line ...interface{} ) {
fmt.Fprintln( os.Stderr, line... )
}
func openFiles() {
var err error
infile, err = os.Open(infileopt)
if err != nil {
printErrln( "Error: Could not open: ", infileopt)
printErrln( "\tError: ", err )
os.Exit(2)
}
if outfileopt == "-" {
outfile = os.Stdout
outfileopt = "Stdout"
} else {
outfile, err = os.Create(outfileopt)
if err != nil {
printErrln( "Error: Could not create: ", outfileopt)
printErrln( "\tError: ", err )
os.Exit(3)
}
}
printErrln("InFile:\t", infileopt)
printErrln("OutFile:\t", outfileopt, "\n")
}
func processFile() { //process the input file into data structure
starttime := time.Now()
var err error
var line string
reader := bufio.NewReader(infile)
line, err = reader.ReadString('\n')
if err != nil {
printErrln("Couldn't read first line from: ", infileopt)
printErrln("\tError: ", err )
os.Exit(4)
}
totalcases, err = strconv.Atoi( strings.TrimSpace( line ) )
if err != nil { //if error reading number of cases
printErrln("Couldn't read case numbers from: ", infileopt)
printErrln("\tError: ", err )
os.Exit(5)
}
printErrln("Total cases: ", totalcases)
testcases = make( []Testcase, totalcases ) //allocate structure for cases
for i := 0; i < totalcases; i++ { //increment through the cases
testcases[i].num = i+1
var curdirnum, newdirnum int //varables for storing current cases directories on computer and directories to be created
line, err = reader.ReadString('\n')
dirnums := strings.Split(strings.TrimSpace( line ), " ")
curdirnum, err = strconv.Atoi( dirnums[0] ) //get number of current directories
if err != nil {
printErrln("Error converting cpudirnum: ", err)
os.Exit(6)
}
newdirnum, err = strconv.Atoi( dirnums[1] ) //get number of new directories
if err != nil {
printErrln("Error converting newdirnum: ", err)
os.Exit(7)
}
//printErrln("Reading Case#", i + 1, "CpuDirs=", curdirnum, " NewDirs=", newdirnum) //for testing
testcases[i].curdirs = make( []string, curdirnum)
for c := 0; c < curdirnum; c++ { //read all directories on CPU
line, err = reader.ReadString('\n')
if( err != nil ) {
printErrln("Error reading case#", i+1, " cpudirnum#", c+1)
os.Exit(8)
}
testcases[i].curdirs[c] = strings.TrimSpace(line)
}
testcases[i].newdirs = make( []string, newdirnum )
for n := 0; n < newdirnum; n++ { //read all new directories to be creaed
line, err = reader.ReadString('\n')
if( err != nil ) {
printErrln("Error reading case#", i+1, " newdirnum#", n+1)
os.Exit(9)
}
testcases[i].newdirs[n] = strings.TrimSpace( line )
}
}
printErrln("Read", totalcases, "in", time.Now().Sub(starttime) ,"\n") //output time taken to read all cases
}
type Dir struct {
dirs map[string]*Dir
}
//solve the cases here!!! Store solution in self.solution
func (self Testcase) solve() int {
var root Dir //root directory always created
root.dirs = make(map[string]*Dir) //root directory
var created, curdirnum int = 0, 0 //created directories
for _, v := range self.curdirs { //go through current dir lines
dirstructure := strings.Split(v, "/") //split directories
createdir(&curdirnum, &root, dirstructure[1:]) //ignore first dir because null
}
for _, v := range self.newdirs { //go through current dir lines
dirstructure := strings.Split(v, "/") //split directories
createdir(&created, &root, dirstructure[1:]) //ignore first dir because null
}
printErrln("Solved#", self.num, " answer=", created)
return created
}
func createdir( created *int, root *Dir, structure []string ) { //create a directory if it exists and increment created otherwise move on to next level
if _, ok := root.dirs[structure[0]]; ok == false { //create directory if null and increment created
var newdir Dir
newdir.dirs = make(map[string]*Dir)
root.dirs[structure[0]] = &newdir
(*created)++
}
if len( structure) > 1 {
createdir( created, root.dirs[structure[0]], structure[1:]) //go to next level
}
}