Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.5 kB
2
Indexable
Never
package RegularExpressions;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class RaceExe02 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        ArrayList<String> list = Arrays.stream(input.split(", "))
                .collect(Collectors
                        .toCollection(ArrayList::new));
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
        for (String s : list) {
            map.put(s, 0);
        }
        StringBuilder name = new StringBuilder("");



        Pattern regexOne = Pattern.compile("[A-Za-z]");
        Pattern regexTwo = Pattern.compile("[0-9]");

        while (true) {
            String inputLine = sc.nextLine();
            if (inputLine.equals("end of race")) break;
            Matcher matcherOne = regexOne.matcher(inputLine);
            Matcher matcherTwo = regexTwo.matcher(inputLine);
            int sum = 0;
            while (matcherOne.find()) {
                name.append(matcherOne.group());
            }


            while (matcherTwo.find()) {
                String numAsString = matcherTwo.group();
                int numAsInt = Integer.parseInt(numAsString);
                sum += numAsInt;
            }

            
            String participants = String.valueOf(name);
            if (map.containsKey(participants)) {
                int distance = map.get(participants) + sum;
                map.put(participants, distance);
            }

            name.setLength(0);

        }
        ArrayList<String> res = new ArrayList<>();
        map.entrySet().stream()
                .sorted(Map.Entry
                        .<String, Integer>comparingByValue()
                        .reversed()).limit(3)
                .forEach(e -> res.add(e.getKey()));


        if (res.size() >= 3) {
            System.out.printf("1st place: %s\n", res.get(0));
            System.out.printf("2nd place: %s\n", res.get(1));
            System.out.printf("3rd place: %s\n", res.get(2));
        } else if (res.size() == 2) {
            System.out.printf("1st place: %s\n", res.get(0));
            System.out.printf("2nd place: %s\n", res.get(1));
        } else if (res.size() == 1) {
            System.out.printf("1st place: %s\n", res.get(0));
        }

    }
}