Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.stream.Collectors;
- enum MarbleColor{
- green,
- yellow,
- blue
- }
- class Game{
- private List<Integer> greenMarbles;
- private List<Integer> yellowMarbles;
- private List<Integer> blueMarbles;
- private int id;
- public static int MOST_YELLOWS = 12;
- public static int MOST_GREENS = 13;
- public static int MOST_BLUES = 14;
- public Game(int id, List<Integer> greenMarbles, List<Integer> yellowMarbles, List<Integer> blueMarbles) {
- this.id = id;
- this.greenMarbles = greenMarbles;
- this.yellowMarbles = yellowMarbles;
- this.blueMarbles = blueMarbles;
- }
- public static Game create(String line){
- List<Integer> greenMarbles = new ArrayList<>();
- List<Integer> yellowMarbles = new ArrayList<>();
- List<Integer> blueMarbles = new ArrayList<>();
- String[] parts = line.split(": ");
- String[] idData = parts[0].split("\\s+");
- int id = Integer.parseInt(idData[1]);
- parts = parts[1].split("; ");
- for(String part : parts){
- String[] marblesData = part.split(", ");
- for(String marble : marblesData){
- String[] marbleParts = marble.split(" ");
- int marbleNum = Integer.parseInt(marbleParts[0]);
- MarbleColor marbleColor = MarbleColor.valueOf(marbleParts[1]);
- if(!checkGameValidity(marbleNum,marbleColor))
- return null;
- if(marbleColor == MarbleColor.green)
- greenMarbles.add(marbleNum);
- else if(marbleColor == MarbleColor.yellow)
- yellowMarbles.add(marbleNum);
- else
- blueMarbles.add(marbleNum);
- }
- }
- return new Game(id,greenMarbles,yellowMarbles,blueMarbles);
- }
- private static boolean checkGameValidity(int marbleNum, MarbleColor marbleColor) {
- if(marbleColor.equals(MarbleColor.green)&&marbleNum > MOST_GREENS)
- return false;
- else if(marbleColor.equals(MarbleColor.yellow)&&marbleNum > MOST_YELLOWS)
- return false;
- else if(marbleColor.equals(MarbleColor.blue)&&marbleNum > MOST_BLUES)
- return false;
- return true;
- }
- public int getId() {
- return id;
- }
- }
- public class Main {
- public static void solution(String input) {
- List<String> lines = Arrays.asList(input.split("\n"));
- //System.out.println(lines.size());
- List<Game> games = lines.stream().map(i->Game.create(i)).collect(Collectors.toList());
- int result = games.stream().filter(Objects::nonNull).mapToInt(i->i.getId()).sum();
- System.out.println(result);
- }
- // do not modify the code bellow this line
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = "";
- int numberOfLines = Integer.parseInt(scanner.nextLine());
- for (int i=0; i<numberOfLines; i++) {
- input += scanner.nextLine();
- if (i < numberOfLines-1) {
- input += "\n";
- }
- }
- solution(input);
- scanner.close();
- }
- }
- //input
- 5
- Game 1: 2 blue, 4 yellow; 3 yellow, 2 green, 6 blue; 1 green
- Game 2: 2 blue, 1 green; 3 green, 4 blue, 1 yellow; 1 green, 3 blue
- Game 3: 3 green, 6 blue, 20 yellow; 5 blue, 6 yellow, 13 green; 5 green, 1 yellow
- Game 4: 2 green, 3 yellow, 9 blue; 3 green, 9 yellow; 3 green, 15 blue, 14 yellow
- Game 5: 6 yellow, 2 blue, 3 green; 2 blue, 3 yellow, 1 green
- //output
- 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement