From 91610024c96a8912063c8167b7e42c8038d56561 Mon Sep 17 00:00:00 2001 From: Tristan Date: Sun, 9 Dec 2018 16:38:13 +0000 Subject: [PATCH] 2018 d8 p2 --- 2018/8/part2.js | 24 ++++++++++++++++++++++++ 2018/README.md | 6 ++++-- README.md | 7 ++++--- 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 2018/8/part2.js diff --git a/2018/8/part2.js b/2018/8/part2.js new file mode 100644 index 0000000..9813639 --- /dev/null +++ b/2018/8/part2.js @@ -0,0 +1,24 @@ +const fs = require('fs'); +const input = fs.readFileSync('input.txt', 'utf-8').split('\n')[0].split(' ').map(Number); + +function parse(arr) { + if (arr[0] == 0) { + return ([2 + arr[1], arr.slice(2, 2 + arr[1]).reduce((a, b) => a + b)]); + } + let children = []; + let pointer = 2; + for (let i = 0; i < arr[0]; i++) { + let child = parse(arr.slice(pointer, arr.length)); + pointer += child[0]; + children.push(child[1]); + } + let meta = 0; + let indexes = arr.slice(pointer, pointer + arr[1]) + for (let i of indexes) { + meta += children[i - 1] || 0; + } + return [pointer + arr[1], meta]; +} + +let v = parse(input)[1]; +console.log(v); diff --git a/2018/README.md b/2018/README.md index 5672e26..d10ca05 100644 --- a/2018/README.md +++ b/2018/README.md @@ -6,7 +6,8 @@ 1. [js :star: :star:](https://adventofcode.com/2015/day/5 "see puzzle") 1. [js :star: :star:](https://adventofcode.com/2015/day/6 "see puzzle") 1. [js :star: :star:](https://adventofcode.com/2015/day/7 "see puzzle") -1. [js :star:](https://adventofcode.com/2015/day/8 "see puzzle") +1. [js :star: :star:](https://adventofcode.com/2015/day/8 "see puzzle") +1. [js :star: :star:](https://adventofcode.com/2015/day/9 "see puzzle") # Languages Used ### Python @@ -23,4 +24,5 @@ 1. [:star: :star:](https://adventofcode.com/2018/day/5 "see puzzle") 1. [:star: :star:](https://adventofcode.com/2018/day/6 "see puzzle") 1. [:star: :star:](https://adventofcode.com/2018/day/7 "see puzzle") -1. [:star:](https://adventofcode.com/2018/day/8 "see puzzle") +1. [:star: :star:](https://adventofcode.com/2018/day/8 "see puzzle") +1. [:star: :star:](https://adventofcode.com/2018/day/9 "see puzzle") diff --git a/README.md b/README.md index a49d8bd..704eaa6 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,9 @@ if (advent) { 1. [js :star: :star:](https://adventofcode.com/2015/day/5 "see puzzle") 1. [js :star: :star:](https://adventofcode.com/2015/day/6 "see puzzle") 1. [js :star: :star:](https://adventofcode.com/2015/day/7 "see puzzle") -1. [js :star:](https://adventofcode.com/2015/day/8 "see puzzle") +1. [js :star: :star:](https://adventofcode.com/2015/day/8 "see puzzle") +1. [js :star: :star:](https://adventofcode.com/2015/day/9 "see puzzle") ## Languages Used -* Python 8×:star: -* Node JS 14×:star: \ No newline at end of file +* Python +* Node JS \ No newline at end of file