Last active
April 17, 2018 02:11
-
-
Save jacknie84/6159d820bc8773119d968f84adfb19bd to your computer and use it in GitHub Desktop.
카카오신입공채 코딩테스트 1차(JAVA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jacknie.doodle.kakao; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class KakaoTest2 { | |
static final Pattern DART_RESULT_PATTERN = Pattern.compile("(\\d{1,2})([SDT])([*#]?)"); | |
public static void main(String[] args) { | |
System.out.println(calculateDartPoint("1S2D*3T")); | |
System.out.println(calculateDartPoint("1D2S#10S")); | |
System.out.println(calculateDartPoint("1D2S0T")); | |
System.out.println(calculateDartPoint("1S*2T*3S")); | |
System.out.println(calculateDartPoint("1D#2S*3S")); | |
System.out.println(calculateDartPoint("1T2D3D#")); | |
System.out.println(calculateDartPoint("1D2S3T*")); | |
} | |
static int calculateDartPoint(String dartResult) { | |
int sumPoint = 0; | |
Matcher matcher = DART_RESULT_PATTERN.matcher(dartResult); | |
int beforePoint = 0; | |
while (matcher.find()) { | |
int point = Integer.parseInt(matcher.group(1)); | |
String bonus = matcher.group(2); | |
String option = matcher.group(3); | |
if ("*".equals(option)) { | |
sumPoint += calculateOption(beforePoint, option); | |
} | |
else { | |
sumPoint += beforePoint; | |
} | |
int bonusPoint = calculateBonus(point, bonus); | |
beforePoint = calculateOption(bonusPoint, option); | |
} | |
return sumPoint + beforePoint; | |
} | |
static int calculateBonus(int point, String bonus) { | |
if ("S".equals(bonus)) { | |
return point; | |
} | |
if ("D".equals(bonus)) { | |
return (int) Math.pow(point, 2); | |
} | |
if ("T".equals(bonus)) { | |
return (int) Math.pow(point, 3); | |
} | |
throw new IllegalStateException(); | |
} | |
static int calculateOption(int point, String option) { | |
if (option.isEmpty()) { | |
return point; | |
} | |
if ("*".equals(option)) { | |
return point * 2; | |
} | |
if ("#".equals(option)) { | |
return point * -1; | |
} | |
throw new IllegalStateException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment