문제 설명
다트 게임
카카오톡에 뜬 네 번째 별! 심심할 땐? 카카오톡 게임별~
Game Star
카카오톡 게임별의 하반기 신규 서비스로 다트 게임을 출시하기로 했다. 다트 게임은 다트판에 다트를 세 차례 던져 그 점수의 합계로 실력을 겨루는 게임으로, 모두가 간단히 즐길 수 있다.
갓 입사한 무지는 코딩 실력을 인정받아 게임의 핵심 부분인 점수 계산 로직을 맡게 되었다. 다트 게임의 점수 계산 로직은 아래와 같다.
다트 게임은 총 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
import java.util.Stack;
class Solution {
public int solution(String dartResult) {
Stack<Integer> stack = new Stack<>();
stack.push(0);
for (int a = 0; a < dartResult.length(); a++) {
if (dartResult.charAt(a) == '#') {
int num = stack.pop();
stack.push(num*-1);
}
else if (dartResult.charAt(a) == '*') {
int num = stack.pop();
int num2 = stack.pop();
stack.push(num2*2);
stack.push(num*2);
}
else if (dartResult.charAt(a) == 'S') {
int num = stack.pop();
stack.push(num);
}
else if (dartResult.charAt(a) == 'D') {
int num = stack.pop();
stack.push(num*num);
}
else if (dartResult.charAt(a) == 'T') {
int num = stack.pop();
stack.push(num*num*num);
}else {
int num = Integer.parseInt(String.valueOf(dartResult.charAt(a)));
char num2 = dartResult.charAt(a+1);
if(num==1 && num2=='0') {
stack.push(10);
a=a+1;
}else {
stack.push(Integer.parseInt(String.valueOf(dartResult.charAt(a))));
}
}
}
int answer = stack.pop()+stack.pop()+stack.pop();
return answer;
}
}
import java.util.*;
import java.util.regex.*;
public class Solution {
static int[] sum = new int[3];
static int solution(String msg){
String reg = "([0-9]{1,2}[S|T|D][*|#]{0,1})";
Pattern p = Pattern.compile(reg+reg+reg);
Matcher m = p.matcher(msg);
m.find();
for(int i=1; i<=m.groupCount(); i++){
Pattern p1 = Pattern.compile("([0-9]{1,2})([S|T|D])([*|#]{0,1})");
Matcher m1 = p1.matcher(m.group(i));
m1.find();
sum[i-1] = (int)Math.pow(Integer.parseInt(m1.group(1)), getpow(m1.group(2)));
setting(i,m1.group(3));
}
return(sum[0]+ sum[1]+ sum[2]);
}
static void setting(int idx, String msg){
if(msg.equals("*")){
sum[idx - 1] *= 2;
if(idx > 1 ){
sum[idx - 2] *=2;
}
}
else if(msg.equals("#")){
sum[idx - 1] *=-1 ;
}
}
static int getpow(String mag){
if(mag.equals("S")){
return 1;
}
else if(mag.equals("D")){
return 2;
}
else {
return 3;
}
}
}
import java.util.Hashtable;
class Solution {
public int solution(String dartResult) {
int answer = 0;
Hashtable<String, Integer> table = new Hashtable<>();
table.put("S",1);
table.put("D",2);
table.put("T",3);
int[] temp = new int[3];
int num = 0;
int count = 0;
for(int i=0; i<dartResult.length(); i++) {
String key = dartResult.charAt(i) + "";
if(table.containsKey(key)) {
num = (int)Math.pow(num, table.get(key));
temp[count++] = num;
num = 0;
} else if(key.equals("*")) {
if(count-2 >= 0) temp[count-2] *= 2;
temp[count-1] *= 2;
} else if(key.equals("#")) {
temp[count-1] *= -1;
} else {
if(num > 0) key = "10";
num = Integer.parseInt(key);
}
}
for(int i=0; i<temp.length; i++) {
answer += temp[i];
}
return answer;
}
}
def solution(dartResult):
answer = ''
score =[]
for i in dartResult:
if i.isnumeric():
answer += i;
elif i =='S':
score.append(int(answer)**1)
answer =''
elif i =='D':
score.append(int(answer)**2)
answer =''
elif i =='T':
score.append(int(answer)**3)
answer =''
elif i == '*':
if len(score) >1:
score[-2] = score[-2]*2
score[-1] = score[-1]*2
else :
score[-1] = score[-1]*2
elif i == '#':
score[-1] = score[-1]*-1
return sum(score)
def solution(dartResult):
point = []
answer = []
dartResult = dartResult.replace('10','k')
point = ['10' if i == 'k' else i for i in dartResult]
print(point)
i = -1
sdt = ['S', 'D', 'T']
for j in point:
if j in sdt :
answer[i] = answer[i] ** (sdt.index(j)+1)
elif j == '*':
answer[i] = answer[i] * 2
if i != 0 :
answer[i - 1] = answer[i - 1] * 2
elif j == '#':
answer[i] = answer[i] * (-1)
else:
answer.append(int(j))
i += 1
return sum(answer)
import re
def solution(dartResult):
bonus = {'S' : 1, 'D' : 2, 'T' : 3}
option = {'' : 1, '*' : 2, '#' : -1}
p = re.compile('(\d+)([SDT])([*#]?)')
dart = p.findall(dartResult)
for i in range(len(dart)):
if dart[i][2] == '*' and i > 0:
dart[i-1] *= 2
dart[i] = int(dart[i][0]) ** bonus[dart[i][1]] * option[dart[i][2]]
answer = sum(dart)
return answer