Advertisement
Guest User

advent of code day 10 java

a guest
Dec 10th, 2021
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package top.kret11.day10;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.util.*;
  7.  
  8. public class Main {
  9.  
  10.   private static Map<String, String> pairs = Map.of("(", ")", "{", "}", "[", "]", "<", ">");
  11.   private static Map<String, Integer> part1Points = Map.of(")", 3, "]", 57, "}", 1197, ">", 25137);
  12.   private static Map<String, Integer> part2Points = Map.of(")", 1, "]", 2, "}", 3, ">", 4);
  13.  
  14.   public static void main(String[] args) throws IOException {
  15.     if (args == null || args.length == 0) {
  16.       throw new RuntimeException("Need to provide file path");
  17.     }
  18.     String filePath = args[0];
  19.     List<String> lines = Files.readAllLines(Path.of(filePath));
  20.     List<Stack<String>> goodLines = new ArrayList<>();
  21.     long part1Points = 0l;
  22.     l:
  23.     for (String line : lines) {
  24.       Stack<String> stack = new Stack<>();
  25.       for (char c : line.toCharArray()) {
  26.         String character = String.valueOf(c);
  27.         if (pairs.keySet().contains(character)) {
  28.           stack.push(character);
  29.         } else {
  30.           String op = stack.pop();
  31.           if (!pairs.get(op).equals(character)) {
  32.             int pts = Main.part1Points.get(character);
  33.             part1Points += pts;
  34.             continue l;
  35.           }
  36.         }
  37.       }
  38.       goodLines.add(stack);
  39.     }
  40.     System.out.println(part1Points);
  41.  
  42.     List<Long> scores = new ArrayList<>();
  43.     for (Stack<String> stack : goodLines) {
  44.       long part2Pnts = 0;
  45.       while (!stack.empty()) {
  46.         String op = stack.pop();
  47.         String cl = pairs.get(op);
  48.         part2Pnts *= 5;
  49.         part2Pnts += part2Points.get(cl);
  50.       }
  51.       scores.add(part2Pnts);
  52.     }
  53.     Collections.sort(scores);
  54.     System.out.println(scores.get(scores.size() / 2));
  55.   }
  56.  
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement