Advertisement
Guest User

AdventOfCode/18

a guest
Dec 18th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node {
  2.     constructor(content, parent, is_right) {
  3.         if (content === undefined) return;
  4.         this._parent = parent;
  5.         if (typeof content === 'number') return this._value = content;
  6.         this._left = new Node(content[0], this, false);
  7.         this._right = new Node(content[1], this, true);
  8.     }
  9.  
  10.     get is_right() {
  11.         return this.parent.right === this;
  12.     }
  13.    
  14.     get is_left() {
  15.         return this.parent.left === this;
  16.     }
  17.    
  18.     get parent() {
  19.         return this._parent;
  20.     }
  21.    
  22.     get value() {
  23.         return this._value === undefined ? -1 : this._value;
  24.     }
  25.    
  26.     get left() {
  27.         return this._left;
  28.     }
  29.    
  30.     get right() {
  31.         return this._right;
  32.     }
  33.    
  34.     get depth() {
  35.         if (!this.parent) return 0;
  36.         return this.parent.depth + 1;
  37.     }
  38.    
  39.     _search_number(searched_side) {
  40.         var other_side = searched_side === 'right' ? 'left' : 'right';
  41.         if (this['is_' + other_side] && this.parent[searched_side].value !== -1) return this.parent[searched_side];
  42.         var t = this;
  43.         var switched_edge = false;
  44.         do {
  45.             if (switched_edge) {
  46.                 t = t[other_side];
  47.             } else if (t['is_' + searched_side]) {
  48.                 t = t.parent;
  49.                 if (!t.parent) return;
  50.             } else {
  51.                 switched_edge = true;
  52.                 t = t.parent[searched_side];
  53.             }
  54.         } while (t.value === -1);
  55.         return t;
  56.     }
  57.    
  58.     get number_right() {
  59.         return this._search_number('right');
  60.     }
  61.    
  62.     get number_left() {
  63.         return this._search_number('left');
  64.     }
  65.    
  66.     get magnitude() {
  67.         if (this.value !== -1) return this.value;
  68.         return this.left.magnitude * 3 + this.right.magnitude * 2;
  69.     }
  70.    
  71.     reduce() {
  72.         while (this.explode() || this.split());
  73.         return this;
  74.     }
  75.    
  76.     explode() {
  77.         if (this.value !== -1 || this.depth !== 4) {
  78.             return !!this.left?.explode() || !!this.right?.explode();
  79.         }
  80.         var left = this.left.number_left;
  81.         if (left) left.add(this.left.value);
  82.         var right = this.right.number_right;
  83.         if (right) right.add(this.right.value);
  84.         this._right = null;
  85.         this._left = null;
  86.         this._value = 0;
  87.         return true;
  88.     }
  89.    
  90.     split() {
  91.         if (this.value < 10) {
  92.             return !!this.left?.split() || !!this.right?.split();
  93.         }
  94.         this._left = new Node(Math.floor(this.value / 2), this, false);
  95.         this._right = new Node(Math.ceil(this.value / 2), this, true);
  96.         this._value = undefined;
  97.         return true;
  98.     }
  99.    
  100.     add(amount) {
  101.         this._value += amount;
  102.     }
  103.    
  104.     toString() {
  105.         return this.toArray().toString();
  106.     }
  107.    
  108.     toArray() {
  109.         return this.value !== -1 ? this.value : [ this.left.toArray(), this.right.toArray() ];
  110.     }
  111.    
  112.     concat(node) {
  113.         this._parent = new Node();
  114.         node._parent = this._parent;
  115.         this._parent._left = this;
  116.         this._parent._right = node;
  117.         return this._parent;
  118.     }
  119. }
  120.  
  121. export function execute(input_data) {
  122.     var input = input_data.split('\n').map(t => new Node(JSON.parse(t)));
  123.     var max = [];
  124.     for (var i = 0; i < input.length; i++) {
  125.         for (var j = 0; j < input.length; j++) {
  126.             if (i !== j) {
  127.                 var t1 = new Node(input[i].toArray());
  128.                 var t2 = new Node(input[j].toArray());
  129.                 var r = t1.concat(t2).reduce().magnitude;
  130.                 if (r > max) max = r;
  131.             }
  132.         }
  133.     }
  134.     return [
  135.         input.slice(1).reduce((a, b) => a.concat(b).reduce(), input[0]).magnitude,
  136.         max
  137.     ];
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement