Advertisement
Guest User

Stranger forms

a guest
Feb 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.72 KB | None | 0 0
  1. extension String {
  2.     subscript (i: Int) -> Character {
  3.         return self[index(startIndex, offsetBy: i)]
  4.     }
  5.     subscript (bounds: CountableRange<Int>) -> Substring {
  6.         let start = index(startIndex, offsetBy: bounds.lowerBound)
  7.         let end = index(startIndex, offsetBy: bounds.upperBound)
  8.         return self[start ..< end]
  9.     }
  10.     subscript (bounds: CountableClosedRange<Int>) -> Substring {
  11.         let start = index(startIndex, offsetBy: bounds.lowerBound)
  12.         let end = index(startIndex, offsetBy: bounds.upperBound)
  13.         return self[start ... end]
  14.     }
  15.     subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {
  16.         let start = index(startIndex, offsetBy: bounds.lowerBound)
  17.         let end = index(endIndex, offsetBy: -1)
  18.         return self[start ... end]
  19.     }
  20.     subscript (bounds: PartialRangeThrough<Int>) -> Substring {
  21.         let end = index(startIndex, offsetBy: bounds.upperBound)
  22.         return self[startIndex ... end]
  23.     }
  24.     subscript (bounds: PartialRangeUpTo<Int>) -> Substring {
  25.         let end = index(startIndex, offsetBy: bounds.upperBound)
  26.         return self[startIndex ..< end]
  27.     }
  28. }
  29. extension Substring {
  30.     subscript (i: Int) -> Character {
  31.         return self[index(startIndex, offsetBy: i)]
  32.     }
  33.     subscript (bounds: CountableRange<Int>) -> Substring {
  34.         let start = index(startIndex, offsetBy: bounds.lowerBound)
  35.         let end = index(startIndex, offsetBy: bounds.upperBound)
  36.         return self[start ..< end]
  37.     }
  38.     subscript (bounds: CountableClosedRange<Int>) -> Substring {
  39.         let start = index(startIndex, offsetBy: bounds.lowerBound)
  40.         let end = index(startIndex, offsetBy: bounds.upperBound)
  41.         return self[start ... end]
  42.     }
  43.     subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {
  44.         let start = index(startIndex, offsetBy: bounds.lowerBound)
  45.         let end = index(endIndex, offsetBy: -1)
  46.         return self[start ... end]
  47.     }
  48.     subscript (bounds: PartialRangeThrough<Int>) -> Substring {
  49.         let end = index(startIndex, offsetBy: bounds.upperBound)
  50.         return self[startIndex ... end]
  51.     }
  52.     subscript (bounds: PartialRangeUpTo<Int>) -> Substring {
  53.         let end = index(startIndex, offsetBy: bounds.upperBound)
  54.         return self[startIndex ..< end]
  55.     }
  56. }
  57.  
  58. class Seat {
  59.    
  60.     let x: Int!
  61.     let y: Int!
  62.     let symbol: Character!
  63.    
  64.     var taken: Bool {
  65.         get {
  66.             return self.symbol == "*"
  67.         }
  68.     }
  69.    
  70.     init(x: Int, y: Int, symbol: Character) {
  71.         self.x = x
  72.         self.y = y
  73.         self.symbol = symbol
  74.     }
  75. }
  76.  
  77. let input = "..*...*.**.....**...*.*...*..*.**....*.*...*..*.*..***...*..*......*.*.....**..*..*.*.*..****.*.**.."
  78. let width = 10
  79. let stride = 10
  80.  
  81. var seats: [Seat] = []
  82.  
  83. for i in 0...width - 1 {
  84.     for j in 0...stride - 1 {
  85.         let seat = Seat(x: i, y: j, symbol: input[i + j * stride])
  86.         seats.append(seat)
  87.     }
  88. }
  89.  
  90. let symbols = seats.map { return $0.symbol! }
  91. //print(symbols)
  92.  
  93. let coordinates = seats.map { return ($0.x!, $0.y!) }
  94. //print(coordinates)
  95.  
  96. let instructions = ["A", "BAA", "BLC"]
  97. let free = seats.filter { return !$0.taken }.map { return ($0.x!, $0.y!) }
  98.  
  99. func doesSatisfy(lhs: (Int, Int), rhs: (Int, Int), instruction: String) -> Bool {
  100.    
  101.     var res = false
  102.     if (instruction == "A") {
  103.         res = lhs.0 < rhs.0
  104.     }
  105.     else if (instruction == "L") {
  106.         res = lhs.1 < rhs.1
  107.     }
  108.     else if (instruction == "R") {
  109.         res = lhs.1 > rhs.1
  110.     }
  111.    
  112.     return res
  113. }
  114.  
  115. free.forEach { (pair) in
  116.     instructions.forEach({ (instruction) in
  117.         print(doesSatisfy(lhs: pair, rhs: pair, instruction: instruction))
  118.     })
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement