Advertisement
dzocesrce

[CodeChem] Marbles

May 27th, 2025
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. enum MarbleColor{
  7.     green,
  8.     yellow,
  9.     blue
  10. }
  11. class Game{
  12.     private List<Integer> greenMarbles;
  13.     private List<Integer> yellowMarbles;
  14.     private List<Integer> blueMarbles;
  15.     private int id;
  16.  
  17.     public static int MOST_YELLOWS = 12;
  18.     public static int MOST_GREENS = 13;
  19.     public static int MOST_BLUES = 14;
  20.  
  21.     public Game(int id, List<Integer> greenMarbles, List<Integer> yellowMarbles, List<Integer> blueMarbles) {
  22.         this.id = id;
  23.         this.greenMarbles = greenMarbles;
  24.         this.yellowMarbles = yellowMarbles;
  25.         this.blueMarbles = blueMarbles;
  26.     }
  27.  
  28.  
  29.     public static Game create(String line){
  30.         List<Integer> greenMarbles = new ArrayList<>();
  31.         List<Integer> yellowMarbles = new ArrayList<>();
  32.         List<Integer> blueMarbles = new ArrayList<>();
  33.         String[] parts = line.split(": ");
  34.         String[] idData = parts[0].split("\\s+");
  35.         int id = Integer.parseInt(idData[1]);
  36.         parts = parts[1].split("; ");
  37.         for(String part : parts){
  38.             String[] marblesData = part.split(", ");
  39.             for(String marble : marblesData){
  40.                 String[] marbleParts = marble.split(" ");
  41.                 int marbleNum = Integer.parseInt(marbleParts[0]);
  42.                 MarbleColor marbleColor = MarbleColor.valueOf(marbleParts[1]);
  43.                 if(!checkGameValidity(marbleNum,marbleColor))
  44.                     return null;
  45.                 if(marbleColor == MarbleColor.green)
  46.                     greenMarbles.add(marbleNum);
  47.                 else if(marbleColor == MarbleColor.yellow)
  48.                     yellowMarbles.add(marbleNum);
  49.                 else
  50.                     blueMarbles.add(marbleNum);
  51.             }
  52.         }
  53.         return new Game(id,greenMarbles,yellowMarbles,blueMarbles);
  54.     }
  55.  
  56.     private static boolean checkGameValidity(int marbleNum, MarbleColor marbleColor) {
  57.         if(marbleColor.equals(MarbleColor.green)&&marbleNum > MOST_GREENS)
  58.             return false;
  59.         else if(marbleColor.equals(MarbleColor.yellow)&&marbleNum > MOST_YELLOWS)
  60.             return false;
  61.         else if(marbleColor.equals(MarbleColor.blue)&&marbleNum > MOST_BLUES)
  62.             return false;
  63.         return true;
  64.     }
  65.  
  66.     public int getId() {
  67.         return id;
  68.     }
  69. }
  70.  
  71.  
  72. public class Main {
  73.     public static void solution(String input) {
  74.  
  75.         List<String> lines = Arrays.asList(input.split("\n"));
  76.         //System.out.println(lines.size());
  77.         List<Game> games = lines.stream().map(i->Game.create(i)).collect(Collectors.toList());
  78.         int result = games.stream().filter(Objects::nonNull).mapToInt(i->i.getId()).sum();
  79.         System.out.println(result);
  80.     }
  81.  
  82.     // do not modify the code bellow this line
  83.     public static void main(String[] args) {
  84.         Scanner scanner = new Scanner(System.in);
  85.         String input = "";
  86.  
  87.         int numberOfLines = Integer.parseInt(scanner.nextLine());
  88.  
  89.         for (int i=0; i<numberOfLines; i++) {
  90.             input += scanner.nextLine();
  91.             if (i < numberOfLines-1) {
  92.                 input += "\n";
  93.             }
  94.         }
  95.  
  96.         solution(input);
  97.         scanner.close();
  98.     }
  99. }
  100.  
  101. //input
  102. 5
  103. Game 1: 2 blue, 4 yellow; 3 yellow, 2 green, 6 blue; 1 green
  104. Game 2: 2 blue, 1 green; 3 green, 4 blue, 1 yellow; 1 green, 3 blue
  105. Game 3: 3 green, 6 blue, 20 yellow; 5 blue, 6 yellow, 13 green; 5 green, 1 yellow
  106. Game 4: 2 green, 3 yellow, 9 blue; 3 green, 9 yellow; 3 green, 15 blue, 14 yellow
  107. Game 5: 6 yellow, 2 blue, 3 green; 2 blue, 3 yellow, 1 green
  108.  
  109. //output
  110. 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement