Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension String {
- subscript (i: Int) -> Character {
- return self[index(startIndex, offsetBy: i)]
- }
- subscript (bounds: CountableRange<Int>) -> Substring {
- let start = index(startIndex, offsetBy: bounds.lowerBound)
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[start ..< end]
- }
- subscript (bounds: CountableClosedRange<Int>) -> Substring {
- let start = index(startIndex, offsetBy: bounds.lowerBound)
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[start ... end]
- }
- subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {
- let start = index(startIndex, offsetBy: bounds.lowerBound)
- let end = index(endIndex, offsetBy: -1)
- return self[start ... end]
- }
- subscript (bounds: PartialRangeThrough<Int>) -> Substring {
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[startIndex ... end]
- }
- subscript (bounds: PartialRangeUpTo<Int>) -> Substring {
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[startIndex ..< end]
- }
- }
- extension Substring {
- subscript (i: Int) -> Character {
- return self[index(startIndex, offsetBy: i)]
- }
- subscript (bounds: CountableRange<Int>) -> Substring {
- let start = index(startIndex, offsetBy: bounds.lowerBound)
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[start ..< end]
- }
- subscript (bounds: CountableClosedRange<Int>) -> Substring {
- let start = index(startIndex, offsetBy: bounds.lowerBound)
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[start ... end]
- }
- subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {
- let start = index(startIndex, offsetBy: bounds.lowerBound)
- let end = index(endIndex, offsetBy: -1)
- return self[start ... end]
- }
- subscript (bounds: PartialRangeThrough<Int>) -> Substring {
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[startIndex ... end]
- }
- subscript (bounds: PartialRangeUpTo<Int>) -> Substring {
- let end = index(startIndex, offsetBy: bounds.upperBound)
- return self[startIndex ..< end]
- }
- }
- class Seat {
- let x: Int!
- let y: Int!
- let symbol: Character!
- var taken: Bool {
- get {
- return self.symbol == "*"
- }
- }
- init(x: Int, y: Int, symbol: Character) {
- self.x = x
- self.y = y
- self.symbol = symbol
- }
- }
- let input = "..*...*.**.....**...*.*...*..*.**....*.*...*..*.*..***...*..*......*.*.....**..*..*.*.*..****.*.**.."
- let width = 10
- let stride = 10
- var seats: [Seat] = []
- for i in 0...width - 1 {
- for j in 0...stride - 1 {
- let seat = Seat(x: i, y: j, symbol: input[i + j * stride])
- seats.append(seat)
- }
- }
- let symbols = seats.map { return $0.symbol! }
- //print(symbols)
- let coordinates = seats.map { return ($0.x!, $0.y!) }
- //print(coordinates)
- let instructions = ["A", "BAA", "BLC"]
- let free = seats.filter { return !$0.taken }.map { return ($0.x!, $0.y!) }
- func doesSatisfy(lhs: (Int, Int), rhs: (Int, Int), instruction: String) -> Bool {
- var res = false
- if (instruction == "A") {
- res = lhs.0 < rhs.0
- }
- else if (instruction == "L") {
- res = lhs.1 < rhs.1
- }
- else if (instruction == "R") {
- res = lhs.1 > rhs.1
- }
- return res
- }
- free.forEach { (pair) in
- instructions.forEach({ (instruction) in
- print(doesSatisfy(lhs: pair, rhs: pair, instruction: instruction))
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement