import Foundation
func solution(_ dartResult:String) -> Int {
let dartResult: [Character] = Array(dartResult)
var scores: [Int] = []
var temp: Character = " "
var index: Int = -1
for c in dartResult {
switch c {
case "0"..."9":
if temp == "1" && c == "0" {
scores[index] = 10
} else {
scores.append(Int(String(c))!)
index += 1
}
case "D":
scores[index] = Int(pow(Double(scores[index]), Double(2)))
case "T":
scores[index] = Int(pow(Double(scores[index]), Double(3)))
case "*":
if index == 0 {
scores[index] = scores[index] * 2
} else {
scores[index] = scores[index] * 2
scores[index - 1] = scores[index - 1] * 2
}
case "#":
scores[index] = scores[index] * -1
default:
break
}
temp = c
}
return scores.reduce(0, +)
}
func solution(_ dartResult:String) -> Int {
let numberList = dartResult.split(whereSeparator: {$0.isLetter || $0 == "#" || $0 == "*"})
let letterList = dartResult.split(whereSeparator: {$0.isNumber})
var totalScore = 0
for (i, (number, letter)) in zip(numberList, letterList).enumerated() {
var score = 0
if let number = Int(number) {
score = letter.contains("D") ? number * number : letter.contains("T") ? number * number * number : number
}
if letter.contains("*") {
score *= 2
} else if letter.contains("#") {
score = -score
}
if i != 2 {
if letterList[i + 1].contains("*") {
score *= 2
}
}
totalScore += score
}
return totalScore
}
zip, split(whereSeparator: )