Advertisement
dzocesrce

[CodeChem] Power Marbles

May 27th, 2025 (edited)
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 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.     public int multiplyMostOfEveryMarbleType(){
  29.         return this.yellowMarbles.stream().max(Comparator.naturalOrder()).orElse(0)
  30.                 * this.greenMarbles.stream().max(Comparator.naturalOrder()).orElse(0)
  31.                 * this.blueMarbles.stream().max(Comparator.naturalOrder()).orElse(0);
  32.     }
  33.  
  34.     public static Game create(String line){
  35.         List<Integer> greenMarbles = new ArrayList<>();
  36.         List<Integer> yellowMarbles = new ArrayList<>();
  37.         List<Integer> blueMarbles = new ArrayList<>();
  38.         String[] parts = line.split(": ");
  39.         String[] idData = parts[0].split("\\s+");
  40.         int id = Integer.parseInt(idData[1]);
  41.         parts = parts[1].split("; ");
  42.         for(String part : parts){
  43.             String[] marblesData = part.split(", ");
  44.             for(String marble : marblesData){
  45.                 String[] marbleParts = marble.split(" ");
  46.                 int marbleNum = Integer.parseInt(marbleParts[0]);
  47.                 MarbleColor marbleColor = MarbleColor.valueOf(marbleParts[1]);
  48.                 if(marbleColor == MarbleColor.green)
  49.                     greenMarbles.add(marbleNum);
  50.                 else if(marbleColor == MarbleColor.yellow)
  51.                     yellowMarbles.add(marbleNum);
  52.                 else
  53.                     blueMarbles.add(marbleNum);
  54.             }
  55.         }
  56.         return new Game(id,greenMarbles,yellowMarbles,blueMarbles);
  57.     }
  58.  
  59.  
  60.     public int getId() {
  61.         return id;
  62.     }
  63. }
  64.  
  65.  
  66. public class Main {
  67.     public static void solution(String input) {
  68.  
  69.         List<String> lines = Arrays.asList(input.split("\n"));
  70.         //System.out.println(lines.size());
  71.         List<Game> games = lines.stream().map(i->Game.create(i)).collect(Collectors.toList());
  72.         System.out.println(games.stream()
  73.                 .filter(Objects::nonNull)
  74.                 .mapToInt(i->i.multiplyMostOfEveryMarbleType()).sum());
  75.  
  76.     }
  77.  
  78.     // do not modify the code bellow this line
  79.     public static void main(String[] args) {
  80.         Scanner scanner = new Scanner(System.in);
  81.         String input = "";
  82.  
  83.         int numberOfLines = Integer.parseInt(scanner.nextLine());
  84.  
  85.         for (int i=0; i<numberOfLines; i++) {
  86.             input += scanner.nextLine();
  87.             if (i < numberOfLines-1) {
  88.                 input += "\n";
  89.             }
  90.         }
  91.  
  92.         solution(input);
  93.         scanner.close();
  94.     }
  95. }
  96.  
  97. //input
  98. 5
  99. Game 1: 2 blue, 4 yellow; 3 yellow, 2 green, 6 blue; 1 green
  100. Game 2: 2 blue, 1 green; 3 green, 4 blue, 1 yellow; 1 green, 3 blue
  101. Game 3: 3 green, 6 blue, 20 yellow; 5 blue, 6 yellow, 13 green; 5 green, 1 yellow
  102. Game 4: 2 green, 3 yellow, 9 blue; 3 green, 9 yellow; 3 green, 15 blue, 14 yellow
  103. Game 5: 6 yellow, 2 blue, 3 green; 2 blue, 3 yellow, 1 green
  104. //output
  105. 2286
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement