Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package top.kret11.day10;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.*;
- public class Main {
- private static Map<String, String> pairs = Map.of("(", ")", "{", "}", "[", "]", "<", ">");
- private static Map<String, Integer> part1Points = Map.of(")", 3, "]", 57, "}", 1197, ">", 25137);
- private static Map<String, Integer> part2Points = Map.of(")", 1, "]", 2, "}", 3, ">", 4);
- public static void main(String[] args) throws IOException {
- if (args == null || args.length == 0) {
- throw new RuntimeException("Need to provide file path");
- }
- String filePath = args[0];
- List<String> lines = Files.readAllLines(Path.of(filePath));
- List<Stack<String>> goodLines = new ArrayList<>();
- long part1Points = 0l;
- l:
- for (String line : lines) {
- Stack<String> stack = new Stack<>();
- for (char c : line.toCharArray()) {
- String character = String.valueOf(c);
- if (pairs.keySet().contains(character)) {
- stack.push(character);
- } else {
- String op = stack.pop();
- if (!pairs.get(op).equals(character)) {
- int pts = Main.part1Points.get(character);
- part1Points += pts;
- continue l;
- }
- }
- }
- goodLines.add(stack);
- }
- System.out.println(part1Points);
- List<Long> scores = new ArrayList<>();
- for (Stack<String> stack : goodLines) {
- long part2Pnts = 0;
- while (!stack.empty()) {
- String op = stack.pop();
- String cl = pairs.get(op);
- part2Pnts *= 5;
- part2Pnts += part2Points.get(cl);
- }
- scores.add(part2Pnts);
- }
- Collections.sort(scores);
- System.out.println(scores.get(scores.size() / 2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement