5 minute read


문제 요구사항

1
2
3
4
5
6
7
8
9
10
1. 다트 게임은 총 3번의 기회로 구성된다.
2. 각 기회마다 얻을 수 있는 점수는 0점에서 10점까지이다.
3. 점수와 함께 Single(S), Double(D), Triple(T) 영역이 존재하고 각 영역 당첨 시 점수에서 1제곱, 2제곱, 3제곱 (점수1 , 점수2 , 점수3 )으로 계산된다.
4. 옵션으로 스타상(*) , 아차상(#)이 존재하며 스타상(*) 당첨 시 해당 점수와 바로 전에 얻은 점수를 각 2배로 만든다. 아차상(#) 당첨 시 해당 점수는 마이너스된다.
5. 스타상(*)은 첫 번째 기회에서도 나올 수 있다. 이 경우 첫 번째 스타상(*)의 점수만 2배가 된다. (예제 4번 참고)
6. 스타상(*)의 효과는 다른 스타상(*)의 효과와 중첩될 수 있다. 이 경우 중첩된 스타상(*) 점수는 4배가 된다. (예제 4번 참고)
7. 스타상(*)의 효과는 아차상(#)의 효과와 중첩될 수 있다. 이 경우 중첩된 아차상(#)의 점수는 -2배가 된다. (예제 5번 참고)
8. Single(S), Double(D), Triple(T)은 점수마다 하나씩 존재한다.
9. 스타상(*), 아차상(#)은 점수마다 둘 중 하나만 존재할 수 있으며, 존재하지 않을 수도 있다.
10. 0~10의 정수와 문자 S, D, T, *, #로 구성된 문자열이 입력될 시 총점수를 반환하는 함수를 작성하라.

Input-1
1S2D*3T
Output-1
37

Input-2
1D2S#10S
Output-2
9

Input-3
1S2T3S
Output-3
23

문제 풀이

주어진 문자열을 통해 먼저 점수와 보너스, 옵션들을 어떻게 구분할까?

  • 문자열을 순회하며 보너스 문자열을 만나면 이전 문자열이 점수임을 알 수 있다.
  • 문자열을 순회하며 스타상(*)과 아차상(#)의 인덱스를 통해 첫번째,두번째,세번째 기회마다 옵션을 알 수 있다.

점수는 0~10까지의 범위기에 두자리수인 10도 문자열에 포함될 수 있다.
두자리 수이기에 인덱스로 접근하기 위해 한자리 문자열(본인의 경우는 ^)로 치환하여 사용했다.
문자열 처리 전에 치환해놓고 처리할 반복문을 동작하게끔 작성했다.

1
dartResult = dartResult.replace("10", "^");

거의 40줄 가량을 첫번째, 두번째, 세번째 기회마다 보너스별로 점수를 계산하는 로직을 작성했다.
S, D, T같은 보너스 문자열을 만났을 때 점수(이전 문자열)를 계산하는데 10점일때의 조건을 별도로 추가하였다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
for(int i=1; i<dartResult.length(); i++) {
    if(dartResult.charAt(i) == 'S') {
        if(dartResult.charAt(i-1) == '^') {
            if(i==1) first = (int) Math.pow(10, 1);
            else if(i==3 || i==4) second = (int) Math.pow(10, 1);
            else if(i==5 || i==6 || i==7) third = (int) Math.pow(10, 1);
        }
        else{
            if(i==1) first = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 1);
            else if(i==3 || i==4) second = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 1);
            else if(i==5 || i==6 || i==7) third = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 1);
        }
    }
    else if(dartResult.charAt(i) == 'D') {
        if(dartResult.charAt(i-1) == '^') {
            if(i==1) first = (int) Math.pow(10, 2);
            else if(i==3 || i==4) second = (int) Math.pow(10, 2);
            else if(i==5 || i==6 || i==7) third = (int) Math.pow(10, 2);
        }
        else {
            if(i==1) first = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 2);
            else if(i==3 || i==4) second = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 2);
            else if(i==5 || i==6 || i==7) third = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 2);
        }
    }
    else if(dartResult.charAt(i) == 'T') {
        if(dartResult.charAt(i-1) == '^') {
            if(i==1) first = (int) Math.pow(10, 3);
            else if(i==3 || i==4) second = (int) Math.pow(10, 3);
            else if(i==5 || i==6 || i==7) third = (int) Math.pow(10, 3);
        }
        else {
            if(i==1) first = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 3);
            else if(i==3 || i==4) second = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 3);
            else if(i==5 || i==6 || i==7) third = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 3);
        }
    }
    // 생략
}

스타상의 경우 첫번째, 두번째, 세번째 기회마다 적용되어야할 점수 계산이 다르다.

  • 첫번째 기회일 경우 현재 점수 2배
  • 두번째, 세번째 기회일 경우 이전 점수와 현재 점수 2배

스타상은 기회별로 2배씩 곱할 수 있도록, 아차상은 기회별로 -1를 곱하여 뺼 수 있도록 작성했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(dartResult.charAt(i) == '*') {
    if(i == 2) first *= 2;
    else if(i == 4 || i == 5) {
        first *= 2;
        second *= 2;
    }
    else if(i==6 || i == 7 || i == 8) {
        second *= 2;
        third *= 2;
    }
}
else if(dartResult.charAt(i) == '#') {
    if(i == 2) first *= -1;
    else if(i == 4 || i == 5) second *= -1;
    else if(i==6 || i == 7 || i == 8) third *= -1;
}


작성 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
    public int solution(String dartResult) {
        int answer = 0;
        int first = 0;
        int second = 0;
        int third = 0;
        dartResult = dartResult.replace("10", "^");
        for(int i=1; i<dartResult.length(); i++) {
            if(dartResult.charAt(i) == 'S') {
                if(dartResult.charAt(i-1) == '^') {
                    if(i==1) first = (int) Math.pow(10, 1);
                    else if(i==3 || i==4) second = (int) Math.pow(10, 1);
                    else if(i==5 || i==6 || i==7) third = (int) Math.pow(10, 1);
                }
                else{
                    if(i==1) first = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 1);
                    else if(i==3 || i==4) second = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 1);
                    else if(i==5 || i==6 || i==7) third = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 1);
                }
            }
            else if(dartResult.charAt(i) == 'D') {
                if(dartResult.charAt(i-1) == '^') {
                    if(i==1) first = (int) Math.pow(10, 2);
                    else if(i==3 || i==4) second = (int) Math.pow(10, 2);
                    else if(i==5 || i==6 || i==7) third = (int) Math.pow(10, 2);
                }
                else {
                    if(i==1) first = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 2);
                    else if(i==3 || i==4) second = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 2);
                    else if(i==5 || i==6 || i==7) third = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 2);
                }
            }
            else if(dartResult.charAt(i) == 'T') {
                if(dartResult.charAt(i-1) == '^') {
                    if(i==1) first = (int) Math.pow(10, 3);
                    else if(i==3 || i==4) second = (int) Math.pow(10, 3);
                    else if(i==5 || i==6 || i==7) third = (int) Math.pow(10, 3);
                }
                else {
                    if(i==1) first = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 3);
                    else if(i==3 || i==4) second = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 3);
                    else if(i==5 || i==6 || i==7) third = (int) Math.pow(Integer.parseInt(dartResult.substring(i-1, i)), 3);
                }
            }
            if(dartResult.charAt(i) == '*') {
                if(i == 2) first *= 2;
                else if(i == 4 || i == 5) {
                    first *= 2;
                    second *= 2;
                }
                else if(i==6 || i == 7 || i == 8) {
                    second *= 2;
                    third *= 2;
                }
            }
            else if(dartResult.charAt(i) == '#') {
                if(i == 2) first *= -1;
                else if(i == 4 || i == 5) second *= -1;
                else if(i==6 || i == 7 || i == 8) third *= -1;
            }
        }
        answer = first + second + third;
        return answer;
    }
}

회고

  • 조건들을 그룹화하여 같은 코드를 줄일 수 있는 능력이 정말 중요하다고 느낀 문제였다. 거진 60줄의 반복문 코드를 작성했는데 더 간결하고 중복코드 없이 작성할 수 있도록 노력해야겠다고 느꼈다.