본문 바로가기
Problem-solving/프로그래머스

프로그래머스 - (2018 KAKAO BLIND RECRUITMENT) [1차]다트 게임 (Java)

by taehee.kim.dev 2020. 11. 22.

programmers.co.kr/learn/courses/30/lessons/17682

 

코딩테스트 연습 - [1차] 다트 게임

 

programmers.co.kr

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Solution {
    public int solution(String dartResult) {

        double[] scores = new double[3];

        Pattern stagePattern = Pattern.compile("\\d+[SDT][*#]?");
        Matcher stageMatcher = stagePattern.matcher(dartResult);
        int scoreIndex = 0;
        while (stageMatcher.find()){
            String currentStage = stageMatcher.group();
            String score = null;
            String bonus = null;
            String option = null;

            Pattern scorePattern = Pattern.compile("\\d+");
            Matcher scoreMatcher = scorePattern.matcher(currentStage);
            if (scoreMatcher.find()){
                score = scoreMatcher.group();
            }

            Pattern bonusPattern = Pattern.compile("[SDT]");
            Matcher bonusMatcher = bonusPattern.matcher(currentStage);
            if (bonusMatcher.find()){
                bonus = bonusMatcher.group();
            }

            Pattern optionPattern = Pattern.compile("[*#]");
            Matcher optionMatcher = optionPattern.matcher(currentStage);
            if (optionMatcher.find()){
                option = optionMatcher.group();
            }

            switch (bonus){
                case "S":
                    scores[scoreIndex] = Math.pow(Double.parseDouble(score), 1.0);
                    break;
                case "D":
                    scores[scoreIndex] = Math.pow(Double.parseDouble(score), 2.0);
                    break;
                case "T":
                    scores[scoreIndex] = Math.pow(Double.parseDouble(score), 3.0);
                    break;
            }

            if (option != null){
                switch (option){
                    case "*":
                        if (0 < scoreIndex){
                            scores[scoreIndex - 1] *= 2.0;
                        }
                        scores[scoreIndex] *= 2.0;
                        break;
                    case "#":
                        scores[scoreIndex] *= -1.0;
                }

            }

            scoreIndex++;
        }

        return (int) Arrays.stream(scores).sum();
    }
}

댓글