#!/usr/bin/env node const printTree = require('print-tree'); function split2trees(lst) { ix = [...lst.keys()].find(i => 2*lst.slice(0,i+1).filter(x => x === ".").length > i+1 ); return [lst.slice(0,ix+1), lst.slice(ix+1)] } function mkTree(lst) { if (lst[0] === ".") { return { name: "" }; }; const nm = lst[0]; const [lft,rght] = split2trees(lst.slice(1)); return { name: nm, children: [mkTree(lft),mkTree(rght)], }; } const inTreeList = (process.argv.length > 2) ? (process.argv.slice(2)) : (["1", "2", "4", ".", "7", ".", ".", ".", "3", "5", ".", ".", "6", ".", "."]); const sm = inTreeList.reduce((acc,el) => {return acc + (parseInt(el) || 0)}, 0); const outTreeList = inTreeList.map(x => (parseInt(x)) ? (sm - parseInt(x)) : x); printTree( mkTree(outTreeList), node => node.name.toString(), node => node.children, ); /* run