import Foundation
func solution(_ dartResult:String) -> Int {
var dartResult = dartResult.map{ $0 }
var pointStack: [Int] = []
var tmpPoint = ""
var optionCheck = false
var savePoint = 0
while !dartResult.isEmpty {
let current = dartResult.first
func saveAtStack() {
pointStack.append(savePoint)
savePoint = 0
tmpPoint = ""
}
if current!.isNumber {
while dartResult.first!.isNumber {
tmpPoint += String(dartResult.removeFirst())
}
} else {
if current == "S" {
savePoint = Int(tmpPoint)!
saveAtStack()
} else if current == "D" {
savePoint = Int(tmpPoint)! * Int(tmpPoint)!
saveAtStack()
} else if current == "T" {
savePoint = Int(tmpPoint)! * Int(tmpPoint)! * Int(tmpPoint)!
saveAtStack()
} else if current == "*" {
if !pointStack.isEmpty {
pointStack[pointStack.count - 1] *= 2
}
if pointStack.count > 1 {
pointStack[pointStack.count - 2] *= 2
}
} else if current == "#" {
let lastPoint = pointStack.removeLast()
pointStack.append(-lastPoint)
savePoint = -savePoint
}
dartResult.removeFirst()
}
}
return pointStack.reduce(0, +)
}