-
Notifications
You must be signed in to change notification settings - Fork 3
/
Solution.java
69 lines (63 loc) · 1.81 KB
/
Solution.java
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
66
67
68
69
package dev.idion.programmers.dartgame;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public int solution(String dartResult) {
Pattern pattern = Pattern.compile("(\\d+\\w[#|*]?)(\\d+\\w[#|*]?)(\\d+\\w[#|*]?)");
Matcher matcher = pattern.matcher(dartResult);
int answer = 0;
while (matcher.find()) {
String s1 = matcher.group(1);
String s2 = matcher.group(2);
String s3 = matcher.group(3);
int c1 = calculate(s1);
int c2 = calculate(s2);
int c3 = calculate(s3);
if (s1.contains("*")) {
c1 *= 2;
}
if (s1.contains("#")) {
c1 *= -1;
}
if (s2.contains("*")) {
c1 *= 2;
c2 *= 2;
}
if (s2.contains("#")) {
c2 *= -1;
}
if (s3.contains("*")) {
c2 *= 2;
c3 *= 2;
}
if (s3.contains("#")) {
c3 *= -1;
}
answer += c1 + c2 + c3;
}
return answer;
}
private int calculate(String data) {
Pattern pattern = Pattern.compile("(\\d+)(\\w)");
Matcher matcher = pattern.matcher(data);
int score = 0;
int power = 0;
while (matcher.find()) {
score = Integer.parseInt(matcher.group(1));
power = calculatePower(matcher.group(2));
}
return (int) Math.pow(score, power);
}
private int calculatePower(String data) {
if ("S".equals(data)) {
return 1;
}
if ("D".equals(data)) {
return 2;
}
if ("T".equals(data)) {
return 3;
}
return 0;
}
}