프로그래머스/Java
프로그래머스 [1차] 다트 게임[Java]
RunTimeException
2021. 8. 10. 12:11
문제
다트 게임
카카오톡에 뜬 네 번째 별! 심심할 땐? 카카오톡 게임별~
카카오톡 게임별의 하반기 신규 서비스로 다트 게임을 출시하기로 했다. 다트 게임은 다트판에 다트를 세 차례 던져 그 점수의 합계로 실력을 겨루는 게임으로, 모두가 간단히 즐길 수 있다.
갓 입사한 무지는 코딩 실력을 인정받아 게임의 핵심 부분인 점수 계산 로직을 맡게 되었다. 다트 게임의 점수 계산 로직은 아래와 같다.
- 다트 게임은 총 3번의 기회로 구성된다.
- 각 기회마다 얻을 수 있는 점수는 0점에서 10점까지이다.
- 점수와 함께 Single(S), Double(D), Triple(T) 영역이 존재하고 각 영역 당첨 시 점수에서 1제곱, 2제곱, 3제곱 (점수1 , 점수2 , 점수3 )으로 계산된다.
- 옵션으로 스타상(*) , 아차상(#)이 존재하며 스타상(*) 당첨 시 해당 점수와 바로 전에 얻은 점수를 각 2배로 만든다. 아차상(#) 당첨 시 해당 점수는 마이너스된다.
- 스타상(*)은 첫 번째 기회에서도 나올 수 있다. 이 경우 첫 번째 스타상(*)의 점수만 2배가 된다. (예제 4번 참고)
- 스타상(*)의 효과는 다른 스타상(*)의 효과와 중첩될 수 있다. 이 경우 중첩된 스타상(*) 점수는 4배가 된다. (예제 4번 참고)
- 스타상(*)의 효과는 아차상(#)의 효과와 중첩될 수 있다. 이 경우 중첩된 아차상(#)의 점수는 -2배가 된다. (예제 5번 참고)
- Single(S), Double(D), Triple(T)은 점수마다 하나씩 존재한다.
- 스타상(*), 아차상(#)은 점수마다 둘 중 하나만 존재할 수 있으며, 존재하지 않을 수도 있다.
0~10의 정수와 문자 S, D, T, *, #로 구성된 문자열이 입력될 시 총점수를 반환하는 함수를 작성하라.
입력 형식
"점수|보너스|[옵션]"으로 이루어진 문자열 3세트.
예) 1S2D*3T
- 점수는 0에서 10 사이의 정수이다.
- 보너스는 S, D, T 중 하나이다.
- 옵선은 *이나 # 중 하나이며, 없을 수도 있다.
출력 형식
3번의 기회에서 얻은 점수 합계에 해당하는 정수값을 출력한다.
예) 37
입출력 예제
예제dartResultanswer설명
1 | 1S2D*3T | 37 | 11 * 2 + 22 * 2 + 33 |
2 | 1D2S#10S | 9 | 12 + 21 * (-1) + 101 |
3 | 1D2S0T | 3 | 12 + 21 + 03 |
4 | 1S*2T*3S | 23 | 11 * 2 * 2 + 23 * 2 + 31 |
5 | 1D#2S*3S | 5 | 12 * (-1) * 2 + 21 * 2 + 31 |
6 | 1T2D3D# | -4 | 13 + 22 + 32 * (-1) |
7 | 1D2S3T* | 59 | 12 + 21 * 2 + 33 * 2 |
답안
class Solution {
public int solution(String dartResult) {
int number = 0;
int point[] = new int[3];
int index = 0;
int now = 0;
int answer = 0;
for (int i = 0; i < dartResult.length(); i++) {
char c = dartResult.charAt(i);
// 숫자 문자 검사
if (Character.isDigit(c)) {
// 숫자 10 검사
if (c == '1' && dartResult.charAt(i + 1) == '0') {
number = 10;
i++;
} else {
number = Character.getNumericValue(c);
}
now++;
} else {
switch (c) {
case 'S':
point[index++] = (int)Math.pow(number, 1);
break;
case 'D':
point[index++] = (int)Math.pow(number, 2);
break;
case 'T':
point[index++] = (int)Math.pow(number, 3);
break;
case '*':
index = index -2 < 0 ? 0 : index -2;
while(index < now) {
point[index++]*=2;
}
break;
case '#':
point[index-1] *= -1;
break;
}
}
}
for(int i=0; i<point.length; i++) {
answer+= point[i];
}
return answer;
}
}
class Solution {
//세분화 기법
public int solution(String dartResult) {
int answer = 0;
int len = dartResult.length(); //다트결과 길이
int lenPos = 0;
int step = 1; //1.점수|2.보너스|3.옵션
int score [] = new int[3];
int scorePos = 0;
while(lenPos < len) {
String target = dartResult.substring(lenPos, lenPos+1);
if(step == 1) {
try {
//1.1 점수 처리
score[scorePos] = Integer.parseInt(target);
}catch(Exception e) {
if(target.equals("*")) {
score[scorePos-1]*=2;
//3.1 스타상 처리
if(scorePos>1) score[scorePos-2]*=2;
}
else if(target.equals("#")) {
score[scorePos-1]*=(-1);
//3.2 아차상 처리
}
else {
System.out.println("3.유효하지 않은 데이터입니다.");
break;
}
step--;
}
step++;
}
else if(step == 2) {
if(target.equals("S")) {
score[scorePos] = (int) Math.pow(score[scorePos], 1);
//2.1 Single 처리
}
else if(target.equals("D")) {
score[scorePos] = (int) Math.pow(score[scorePos], 2);
//2.2 Double 처리
}
else if(target.equals("T")) {
score[scorePos] = (int) Math.pow(score[scorePos], 3);
//2.3 Triple 처리
}
else if(target.equals("0")) {
score[scorePos] = 10;
step++;
scorePos--;
}
else {
System.out.println("2.유효하지 않은 데이터입니다.");
break;
}
scorePos++;
step--;
}
lenPos++;
}
return score[0] + score[1] + score[2];
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//정규화 이용
class Solution {
int score[] = new int[3];
int scorePos = 0;
public int solution(String dartResult) {
String regex = "(\\d{1,2}[S|D|T][*|#]?)";
Pattern p = Pattern.compile(regex + regex + regex);
Matcher m = p.matcher(dartResult);
m.find();
for(int index=1; index<=m.groupCount(); index++) {
Pattern pat = Pattern.compile("(\\d{1,2})([S|D|T])([*|#]?)");
Matcher mat = pat.matcher(m.group(index));
mat.find();
score[scorePos] = Integer.parseInt(mat.group(1));
getPow(mat.group(2));
getOption(mat.group(3));
}
return score[0] + score[1] + score[2];
}
//제곱
public void getPow(String squ) {
if(squ.equals("S")) {
score[scorePos] = (int) Math.pow(score[scorePos], 1);
}
else if(squ.equals("D")) {
score[scorePos] = (int) Math.pow(score[scorePos], 2);
}
else if(squ.equals("T")) {
score[scorePos] = (int) Math.pow(score[scorePos], 3);
}
scorePos++;
}
public void getOption(String option) {
if(option.equals("*")) {
score[scorePos-1]*=2;
if(scorePos>1) score[scorePos-2]*=2;
}
else if(option.equals("#")) {
score[scorePos-1]*=(-1);
}
}
}