알고리즘

[프로그래머스][Swift] 다트 게임

moving 2022. 7. 14. 14:33
728x90

 

func solution(_ dartResult:String) -> Int {
    
    let arr = dartResult.map { $0 }
    var arr2: [Int] = []
    var result: Int = 0
    
    for i in 0..<arr.count {
        
        if arr[i].isNumber == true {
            continue
        } else if arr[i] == "S" || arr[i] == "D" || arr[i] == "T" {
            if arr[i] == "S" {
                if i > 1 {
                    if arr[i - 2].isNumber == true {
                        result += 10
                    }
                }
                result += Int(String(arr[i - 1]))!
            } else if arr[i] == "D" {
                if i > 1 {
                    if arr[i - 2].isNumber == true {
                        result += 10 * 10                    }
                }
                result += Int(String(arr[i - 1]))! * Int(String(arr[i - 1]))!
            } else {
                if i > 1 {
                    if arr[i - 2].isNumber == true {
                        result += 10 * 10 * 10
                    }
                }
                result += Int(String(arr[i - 1]))! * Int(String(arr[i - 1]))! * Int(String(arr[i - 1]))!
            }
            arr2.append(result)
        } else {
            if arr[i] == "*" {
                if arr2.count == 1 {
                    result = arr2[0] * 2
                    arr2.removeLast()
                    arr2.append(result)
                } else if arr2.count == 2 {
                    result = arr2[1] * 2
                    arr2.removeLast()
                    arr2.append(result)
                    result = 0
                    result = arr2[0] * 2
                    arr2.removeFirst()
                    arr2.insert(result, at: 0)
                } else {
                    result = arr2[2] * 2
                    arr2.removeLast()
                    arr2.append(result)
                    result = 0
                    result = arr2[1] * 2
                    arr2.remove(at: 1)
                    arr2.insert(result, at: 1)
                }
            } else {
                result = arr2[arr2.index(before: arr2.endIndex)] * (-1)
                arr2.removeLast()
                arr2.append(result)
            }
        }
        result = 0
    }
    return arr2.reduce(0) { $0 + $1 }
}

 

 

다른 사람의 풀이

func solution2(_ 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
}

어렵다...