Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. import Foundation
  2.  
  3. func kakaoSolution1(n: Int, arr1: [Int], arr2: [Int]) -> [String] {
  4. func convertBinary(number: Int) -> String {
  5. var number = number
  6. var result = String(repeating: "0", count: n-1)
  7. var binary = String()
  8.  
  9. while number >= 2 {
  10. binary.insert(Character(String(number % 2)), at: binary.startIndex)
  11. result.remove(at: result.startIndex)
  12. number = number / 2
  13. }
  14. binary.insert(Character(String(number)), at: binary.startIndex)
  15. result += binary
  16. return result
  17. }
  18.  
  19. var result = [String]()
  20. for i in 0..<n {
  21. let binary1 = convertBinary(number: arr1[i]).characters.map { $0 }
  22. let binary2 = convertBinary(number: arr2[i]).characters.map { $0 }
  23.  
  24. var str = String()
  25. for j in 0..<n {
  26. if binary1[j] == "1" || binary2[j] == "1" {
  27. str += "#"
  28. } else {
  29. str += " "
  30. }
  31. }
  32. result.append(str)
  33. }
  34. return result
  35. }
  36.  
  37. func kakaoSolution2(score: String) -> Int {
  38. func splitScores(score: String) -> [[String?]] {
  39. var scores = [String]()
  40. var bonus = [String]()
  41. var option = [String?]()
  42. var one = String()
  43. for char in score.characters {
  44. if let num = Int(String(char)) {
  45. if num == 1 {
  46. one = String(char)
  47. } else {
  48. if one == "1" && char == "0" {
  49. one += String(char)
  50. scores.append(one)
  51. } else {
  52. if one == "1" {
  53. scores.append(one)
  54. }
  55. scores.append(String(char))
  56. }
  57. one = String()
  58. }
  59. } else {
  60. if char == "#" || char == "*" {
  61. option.removeLast()
  62. option.append(String(char))
  63. } else {
  64. option.append(nil)
  65. bonus.append(String(char))
  66. }
  67. }
  68. }
  69. return [scores, bonus, option]
  70. }
  71.  
  72. let splitResult = splitScores(score: score)
  73. var sum = [Int]()
  74.  
  75. for i in 0..<splitResult.count {
  76. guard let score = Int(splitResult[0][i]!),
  77. let bonus = splitResult[1][i] else { break }
  78. var tempScore = score
  79.  
  80. switch bonus {
  81. case "D":
  82. tempScore *= tempScore
  83. case "T":
  84. tempScore *= tempScore * tempScore
  85. default:
  86. break
  87. }
  88.  
  89. if let option = splitResult[2][i] {
  90. switch option {
  91. case "*":
  92. if !sum.isEmpty {
  93. sum[sum.count-1] *= 2
  94. }
  95. tempScore *= 2
  96. default:
  97. tempScore = tempScore - (tempScore * 2)
  98. }
  99. }
  100. sum.append(tempScore)
  101. }
  102. return sum.reduce(0) { $0 + $1 }
  103. }
  104.  
  105. func kakaoSolution3(cacheSize: Int, cities: [String]) -> Int {
  106. let cities = cities.map { $0.uppercased() }
  107. var cache = [String]()
  108. var runningTime = Int()
  109.  
  110. for city in cities {
  111. if cache.contains(city) {
  112. runningTime += 1
  113. } else {
  114. runningTime += 5
  115. }
  116. cache.append(city)
  117. if cache.count > cacheSize {
  118. cache.removeFirst()
  119. }
  120. }
  121. return runningTime
  122. }
  123.  
  124. func kakaoSolution6(m: Int, n: Int, board: [String]) -> Int {
  125. func removeBlock(board: [String]) -> [String] {
  126. var boardCopy = board
  127.  
  128. for i in 1..<m {
  129. let top = board[i-1]
  130. let bottom = board[i]
  131. for j in 1..<n {
  132. let top1 = top.characters.index(top.startIndex, offsetBy: j-1)
  133. let top2 = top.characters.index(top.startIndex, offsetBy: j)
  134. let bottom1 = bottom.characters.index(top.startIndex, offsetBy: j-1)
  135. let bottom2 = bottom.characters.index(top.startIndex, offsetBy: j)
  136. if top[top1] == top[top2] && bottom[bottom1] == bottom[bottom2] && top[top2] == bottom[bottom2] {
  137. boardCopy[i-1].replaceSubrange(top1...top1, with: " ")
  138. boardCopy[i-1].replaceSubrange(top2...top2, with: " ")
  139. boardCopy[i].replaceSubrange(bottom1...bottom1, with: " ")
  140. boardCopy[i].replaceSubrange(bottom2...bottom2, with: " ")
  141. }
  142. }
  143. }
  144. return boardCopy
  145. }
  146.  
  147. var board = removeBlock(board: board)
  148. for i in 1..<m {
  149. for j in 0..<n {
  150. let index = board[i].index(board[i].startIndex, offsetBy: j)
  151. if board[i][index] == " " {
  152. board[i].replaceSubrange(index...index, with: String(board[i-1][index]))
  153. board[i-1].replaceSubrange(index...index, with: " ")
  154. board = removeBlock(board: board)
  155. }
  156. }
  157. }
  158. return board.map{ $0.characters.filter { $0 == " " }.count }.reduce(0) { $0 + $1 }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement