V1
public class DartGameV1 {
public static void main(String[] args) {
String dartResult = "1D2S#10S";
int answer = 0;
int answer_arr[] = new int[3];
int index=0;
int intCnt =0;
for(int i=0; i<dartResult.length(); i++){
char dartChar = dartResult.charAt(i);
int dartInt = Character.getNumericValue(dartChar);
if(dartInt >= 0 && dartInt <=10){
if(dartInt == 1){
if(Character.getNumericValue(dartResult.charAt(i+1)) == 0){
dartInt= 10;
i++;
}
}
answer_arr[index] = dartInt;
intCnt++;
}else{
switch (dartChar){
case 'S'://x1
answer_arr[index] =(int)(Math.pow(answer_arr[index],1));
index++;
break;
case 'D' ://x2
answer_arr[index] = (int)(Math.pow(answer_arr[index],2));
index++;
break;
case 'T' ://x2
answer_arr[index] = (int)(Math.pow(answer_arr[index],3));
index++;
break;
case '*' :
index = index -2 < 0? 0:index-2;
while(index < intCnt){
answer_arr[index] = answer_arr[index]*2;
index++;
}
break;
case '#' :
answer_arr[index-1] = answer_arr[index-1]*(-1);
break;
}
}
}
for (int i : answer_arr) {
answer += i;
}
System.out.println(answer);
}
}
V2
public class DartGameV2 {
public static void main(String[] args) {
String dartResult = "1D2S0T";
int answer =0;
int[] dart = new int[3];
int n=0;
int idx=0;
StringBuilder sb = new StringBuilder();
for(int i=0; i<dartResult.length(); i++){
char c = dartResult.charAt(i);
if(c>='0' && c<= '9'){
sb.append(c);
} else if (c=='S' || c =='D' || c=='T') {
n = Integer.parseInt(sb.toString());
if(c=='S'){
n = (int)(Math.pow(n,1));
}else if(c=='D'){
n = (int)(Math.pow(n,2));
}else{
n = (int) (Math.pow(n,3));
}
dart[idx] =n;
idx++;
sb.setLength(0);
}else{
if(c == '#'){
dart[idx-1] *= -1;
}else{
dart[idx-1] *= 2;
if(idx-2 >= 0){
dart[idx-2] *= 2;
}
}
}
}
for (int i : dart) {
answer += i;
}
System.out.println(answer);
}
}