Advertisement
Guest User

Advent of Code 2021 (day 18)

a guest
Dec 18th, 2021
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4.  
  5. public class Aoc2118 {
  6.     private static boolean explode(List<Node> list) {
  7.         for (var i = 0; i < list.size(); i++) {
  8.             if (list.get(i).depth != 4) continue;
  9.             var left = list.get(i);
  10.             var newDepth = left.depth - 1;
  11.             if (i != 0) {
  12.                 list.get(i - 1).value += left.value;
  13.             }
  14.             var right = list.get(i + 1);
  15.             if (i < list.size() - 2) {
  16.                 list.get(i + 2).value += right.value;
  17.             }
  18.             list.remove(i + 1);
  19.             list.remove(i);
  20.             list.add(i, new Node(newDepth));
  21.             return true;
  22.         }
  23.         return false;
  24.     }
  25.  
  26.     private static boolean split(List<Node> list) {
  27.         for (var i = 0; i < list.size(); i++) {
  28.             if (list.get(i).value > 9) {
  29.                 var nodeToSplit = list.get(i);
  30.                 var splitDepth = nodeToSplit.depth + 1;
  31.                 var splitValue = (float)nodeToSplit.value;
  32.                 list.remove(i);
  33.                 list.add(i, new Node(splitDepth, (int)Math.floor(splitValue / 2)));
  34.                 list.add(i + 1, new Node(splitDepth, (int)Math.ceil(splitValue / 2)));
  35.                 return true;
  36.             }
  37.         }
  38.         return false;
  39.     }
  40.  
  41.     public static void main(String[] args) {
  42.         var lines = getInput();
  43.  
  44.         var largestMagnitude = Integer.MIN_VALUE;
  45.  
  46.         for (var lineIdx1 = 0; lineIdx1 < lines.length; lineIdx1++) {
  47.             for (var lineIdx2 = 0; lineIdx2 < lines.length; lineIdx2++) {
  48.                 if (lineIdx1 == lineIdx2) continue;
  49.  
  50.                 List<Node> sumResult = performSum(parseLine(lines[lineIdx1]), parseLine(lines[lineIdx2]));
  51.  
  52.                 var reduced = false;
  53.                 do {
  54.                     reduced = explode(sumResult);
  55.                     if (reduced) continue;
  56.                     reduced = split(sumResult);
  57.                 } while (reduced);
  58.  
  59.                 var magnitude = magnitude(sumResult);
  60.  
  61.                 if (magnitude > largestMagnitude) largestMagnitude = magnitude;
  62.             }
  63.         }
  64.  
  65.         System.out.println(largestMagnitude);
  66.     }
  67.  
  68.     private static List<Node> performSum(List<Node> operandA, List<Node> operandB) {
  69.         if (operandA.isEmpty()) return operandB;
  70.         operandA.addAll(operandB);
  71.         operandA = operandA.stream().peek(e -> e.depth++).collect(Collectors.toList());
  72.         return operandA;
  73.     }
  74.  
  75.     private static int magnitude(List<Node> list) {
  76.         for (var depth = 3; depth >= 0; depth--) {
  77.             var reduced = false;
  78.             do {
  79.                 reduced = false;
  80.                 for (var j = 0; j < list.size() - 1; j++) {
  81.                     var left = list.get(j);
  82.                     var right = list.get(j + 1);
  83.                     if (left.depth != depth) continue;
  84.                     list.remove(j + 1);
  85.                     list.remove(j);
  86.                     list.add(j, new Node(depth - 1, 3 * left.value + 2 * right.value));
  87.                     reduced = true;
  88.                     break;
  89.                 }
  90.             } while (reduced);
  91.         }
  92.         return list.get(0).value;
  93.     }
  94.  
  95.     private static List<Node> parseLine(String line) {
  96.         var list = new ArrayList<Node>();
  97.         var depth = -1;
  98.         for (var cc : line.toCharArray()) {
  99.             switch (cc) {
  100.                 case '[':
  101.                     depth++;
  102.                     break;
  103.                 case ']':
  104.                     depth--;
  105.                     break;
  106.                 case ',':
  107.                     break;
  108.                 default:
  109.                     var value = Integer.parseInt(String.valueOf(cc));
  110.                     var node = new Node();
  111.                     node.depth = depth;
  112.                     node.value = value;
  113.                     list.add(node);
  114.                     break;
  115.             }
  116.         }
  117.         return list;
  118.     }
  119.  
  120.     static class Node {
  121.         int value = 0;
  122.         int depth;
  123.  
  124.         public Node() {
  125.         }
  126.         public Node(int depth) {
  127.             this.depth = depth;
  128.         }
  129.         public Node(int depth, int value) {
  130.             this.depth = depth;
  131.             this.value = value;
  132.         }
  133.  
  134.         @Override
  135.         public String toString() {
  136.             return value + "";
  137.         }
  138.     }
  139.  
  140.     private static String[] getInput() {
  141.         return ("...").split("\n");
  142.     }
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement