-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschmovi.swift
executable file
·253 lines (197 loc) · 6.32 KB
/
schmovi.swift
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
#!/usr/bin/env swift
import Foundation
// Properties
struct Hours {
var open = Double()
var close = Double()
}
struct Movie {
var title = String()
var year = Int()
var rating = String()
var length = String()
}
var weekDay = Hours()
weekDay.open = 11 * 60 + 60
weekDay.close = 23 * 60
var weekEnd = Hours()
weekEnd.open = 10.5 * 60.0 + 60.0
weekEnd.close = 23.5 * 60
var movies = [Movie]()
let fileManager = NSFileManager.defaultManager()
let path = fileManager.currentDirectoryPath
let scheduleFileName = "MovieSchedule.txt"
// Functions
func firstArgument() -> String {
let arguments = Process.arguments
var inputFileName = String()
var count = 0
for arg in arguments {
if count == 1 {
inputFileName = arg
} else if count != 0 {
print("argument '\(arg)' out of range")
}
++count
}
return inputFileName
}
func fileFound() -> Bool {
let directoryItems = try! fileManager.contentsOfDirectoryAtPath(path)
var fileFound = false
for item in directoryItems {
if firstArgument() == item {
fileFound = true
}
}
return fileFound
}
func inputFileText() {
let inputFileDirectoryURL = NSURL(fileURLWithPath: path).URLByAppendingPathComponent(firstArgument())
var inputFileText = try! String(contentsOfURL: inputFileDirectoryURL, encoding: NSASCIIStringEncoding)
inputFileText = inputFileText.stringByReplacingOccurrencesOfString("\n", withString: ", ")
let inputArray = inputFileText.componentsSeparatedByString(", ")
var movie = Movie()
var count = 4
for _ in 4..<inputArray.count-1 {
if count % 4 == 0 {
movie.title = inputArray[count]
} else if count % 4 == 1 {
if let year = Int(inputArray[count]) {
movie.year = year
}
} else if count % 4 == 2 {
movie.rating = inputArray[count]
} else {
movie.length = inputArray[count]
movies.append(movie)
}
++count
}
}
extension Double {
var chopped: String {
return self % 1 == 0 ? String(format: "%.0f", self) : String(self)
}
}
func getMovieTimesAtIndex(index: Int, open: Double, close: Double) -> [String] {
func convertHoursToMinutes(time: String) -> Double {
let time = time.characters.split(":")
.flatMap { Int(String($0)) }
.reduce(0) { $0 * 60 + $1 }
return Double(time)
}
func convertTimeFromMinutesToStandard(minutes: Double) -> String {
var isAM = false
if minutes < 720 {
isAM = true
}
var hours = floor(minutes/60)
let minutes = minutes%60
if hours > 12 {
hours = hours - 12
}
let hoursString = "\(hours.chopped)"
var minutesString = ""
if minutes < 10 {
minutesString = "0\(minutes.chopped)"
} else {
minutesString = minutes.chopped
}
return "\(hoursString):\(minutesString)\(amOrpm(isAM))"
}
func amOrpm(isAM: Bool) -> String {
if isAM == true {
return "am"
} else {
return "pm"
}
}
func roundToFives(x : Double) -> Double {
return 5 * round(x / 5.0)
}
var movieTimes = [String]()
var lastMovieTime = 0.0
let length = convertHoursToMinutes(movies[index].length)
var lastMovieStartTime = 0.0
repeat {
if lastMovieTime == 0.0 {
lastMovieTime = roundToFives(close - length)
let movieEndTimeDouble = lastMovieTime + length
if movieEndTimeDouble > close {
lastMovieTime = lastMovieTime-5
}
lastMovieStartTime = lastMovieTime
let movieStartTime = convertTimeFromMinutesToStandard(lastMovieTime)
let movieEndTime = convertTimeFromMinutesToStandard(lastMovieTime + length)
movieTimes.insert("\(movieStartTime) - \(movieEndTime)", atIndex: 0)
} else {
lastMovieTime = roundToFives(lastMovieTime - length - 35)
if lastMovieTime + length + 35 > lastMovieStartTime {
lastMovieTime = lastMovieTime-5
}
lastMovieStartTime = lastMovieTime
let movieStartTime = convertTimeFromMinutesToStandard(lastMovieTime)
let movieEndTime = convertTimeFromMinutesToStandard(lastMovieTime + length)
movieTimes.insert("\(movieStartTime) - \(movieEndTime)", atIndex: 0)
}
} while (open) <= (lastMovieTime - length - 35)
return movieTimes
}
func createSchedule() {
var scheduleText = ""
let daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
func convertArrayToString(times: [String]) -> String {
var timeString = ""
for index in 0..<times.count {
timeString += " " + times[index] + "\n"
}
return timeString
}
for day in daysOfWeek {
let movieTitles = movies.map { $0.title }
let movieRatings = movies.map { $0.rating }
let movieLengths = movies.map { $0.length }
switch day {
case "Monday", "Tuesday", "Wednesday", "Thursday":
scheduleText += day + "\n\n"
for index in 0..<movies.count {
let movieTimes = getMovieTimesAtIndex(index, open: weekDay.open, close: weekDay.close)
let title = movieTitles[index]
let rating = movieRatings[index]
let length = movieLengths[index]
scheduleText += "\(title) - Rated \(rating), \(length)\n\(convertArrayToString(movieTimes))\n"
if index == movies.count-1 {
scheduleText += "\n"
}
}
case "Friday", "Saturday", "Sunday":
scheduleText += day + "\n\n"
for index in 0..<movies.count {
let movieTimes = getMovieTimesAtIndex(index, open: weekEnd.open, close: weekEnd.close)
let title = movieTitles[index]
let rating = movieRatings[index]
let length = movieLengths[index]
scheduleText += "\(title) - Rated \(rating), \(length)\n\(convertArrayToString(movieTimes))\n"
if index == movies.count-1 {
scheduleText += "\n"
}
}
default: break
}
}
let scheduleFileDirectoryURL = NSURL(fileURLWithPath: path).URLByAppendingPathComponent("\(scheduleFileName)")
print("writing 'MovieSchedule.txt'")
try! scheduleText.writeToURL(scheduleFileDirectoryURL, atomically: true, encoding: NSUTF8StringEncoding)
print("MovieSchedule complete")
}
// Main
do {
if fileFound() {
print("'\(firstArgument())' found")
inputFileText()
createSchedule()
} else {
print("file '\(firstArgument())' not found")
}
}