Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require("fs");
- const internal = require("stream");
- var fileName =__filename.slice(__dirname.length + 1, -3)+ ".txt";
- let input = fs.readFileSync(fileName, 'utf8').toString();
- let data = input.split('\n').map(s => s.split(',').join("").trim());
- data = data.map(s => {
- return s.split('').map(s =>isNaN(s)?s:Number(s));
- });
- function add(a,b){
- let c = ["[",...a,...b,"]"];
- while(explode(c) || split(c));
- return c;
- }
- function explode(a){
- let open = 0;
- for (let i = 0; i < a.length; i++) {
- const e = a[i];
- if(e == "[")open++;
- if(e == "]")open--;
- if(open == 5){
- let buf = a.splice(i, 4);
- let up=i-1;
- let down=i;
- while(down--){
- if(!isNaN(a[down])){
- a[down]+= buf[1];
- break;
- }
- }
- while(up++<a.length){
- if(!isNaN(a[up])){
- a[up]+= buf[2];
- break;
- }
- }
- a.splice(i, 0,0);
- explode(a);
- return true;
- }
- }
- return false;
- }
- function split(a){
- for (let i = 0; i < a.length; i++) {
- const e = a[i];
- if(!isNaN(e) && e>9){
- let l = Math.floor(e/2);
- let h = Math.ceil(e/2);
- let s = ['[',l,h,']'];
- a.splice(i,1,...s);
- return true;
- }
- }
- return false;
- }
- function magnitude (a){
- for (let i = 0; i < a.length; i++) {
- const e = a[i];
- if(e == "]"){
- let buf = a.splice(i-3, 4);
- let sum = buf[1]*3 + buf[2]*2;
- a.splice(i-3,0,sum);
- i-=3;
- }
- }
- return a;
- }
- let sum = data[0];
- for (let i = 0; i < data.length-1; i++) {
- sum = add(sum,data[i+1]);
- }
- let mag = magnitude(sum);
- console.log(1,mag[0]);
- let max = 0;
- for (let i = 0; i < data.length-1; i++) {
- for (let j = 0; j < data.length; j++) {
- if(i != j){
- max = Math.max( max, magnitude( add(data[i],data[j])));
- max = Math.max( max, magnitude( add(data[j],data[i])))
- }
- }
- }
- console.log(2,max);
Advertisement
Add Comment
Please, Sign In to add comment