Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Created March 28, 2021 16:46
Show Gist options
  • Save woodycatliu/1846b6148007b26770f13420b3813144 to your computer and use it in GitHub Desktop.
Save woodycatliu/1846b6148007b26770f13420b3813144 to your computer and use it in GitHub Desktop.
import Foundation
//let input = """
//4
//2 3 CJ?CC?
//4 2 CJCJ
//1 3 C?J
//2 5 ??J???
//"""
let stringSet = "1000\n"
let array = Array(repeating: "2 3 CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?CJ?CC?\n", count: 1000)
let arrayString = array.joined()
let input = stringSet + arrayString
var lines = input.components(separatedBy: "\n")
func readLine() -> String? {
let firstStr = lines.first
lines.remove(at: 0)
return firstStr!
}
func getIntegersOf(line: String, separatedBy separator: String) -> [String] {
let stringArray = line.components(separatedBy: separator)
return stringArray.map { $0 }
}
var cas: Int = 0
let n: Int = Int(readLine()!)!
while cas < n {
cas += 1
let line = readLine()!
let arr = getIntegersOf(line: line, separatedBy: " ")
let x = Int(arr[0])!
let y = Int(arr[1])!
let art = arr[2]
let total: Int = (y * compute(art, target: "JC")) + (x * compute(art, target: "CJ"))
let output = "Case #\(cas): \(total)"
print(output)
}
func compute(_ string: String, target: String)-> Int {
let replaced = string.replacingOccurrences(of: "?", with: "").replacingOccurrences(of: target, with: "$").replacingOccurrences(of: "C", with: "").replacingOccurrences(of: "J", with: "")
return replaced.count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment