Skip to content

Instantly share code, notes, and snippets.

@Sammons
Created December 8, 2018 05:45
Show Gist options
  • Save Sammons/79d21d2b12ed863ec01f95a785cc27fa to your computer and use it in GitHub Desktop.
Save Sammons/79d21d2b12ed863ec01f95a785cc27fa to your computer and use it in GitHub Desktop.
const fs = require('fs')
const input = fs.readFileSync('./input8.txt');
let mapOfValues = {}
function readChild(offset, input) {
let metaSum = 0;
let current = offset;
const numchildren = input[current];
console.log('NumChildren', numchildren);
current++;
const nummeta = input[current];
console.log('NumMeta', nummeta);
current++;
let currentChildren = [];
for (let i = 0; i < numchildren; i++) {
let { value, current: next } = readChild(current, input)
current = next;
currentChildren.push(value);
}
for (let j = 0; j < nummeta; j++) {
if (numchildren === 0) {
metaSum += input[current];
} else {
console.log(currentChildren, input[current])
metaSum += currentChildren[input[current] - 1] || 0;
}
current++;
}
console.log('ChildVal', metaSum)
return { value: metaSum, current };
}
function solve(input) {
const { value, current } = readChild(0, input);
console.log(value)
}
// solve('2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2'.toString().split(/ /gm).filter(s => s.trim() !== '').map(e => Number(e)))
solve(input.toString().split(/ /gm).filter(s => s.trim() !== '').map(e => Number(e)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment