From 72eda129d2c8dfb53773561927205113fccb2687 Mon Sep 17 00:00:00 2001 From: Tristan Date: Sun, 9 Dec 2018 00:37:33 +0000 Subject: [PATCH] 2018 d8 p1 --- 2018/8/part1.js | 21 +++++++++++++++++++++ 2018/8/test.txt | 4 ++++ 2018/8/test1.txt | 4 ++++ 2018/README.md | 5 +++-- README.md | 4 ++-- 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 2018/8/part1.js create mode 100644 2018/8/test.txt create mode 100644 2018/8/test1.txt diff --git a/2018/8/part1.js b/2018/8/part1.js new file mode 100644 index 0000000..5b58b11 --- /dev/null +++ b/2018/8/part1.js @@ -0,0 +1,21 @@ +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) { + debugger; + return ([2 + arr[1], arr.slice(2, 2 + arr[1])]); + } + let meta = [] + let pointer = 2; + for (let i = 0; i < arr[0]; i++) { + let child = parse(arr.slice(pointer, arr.length)); + pointer += child[0]; + meta = meta.concat(child[1]); + } + meta = meta.concat(arr.slice(pointer, pointer + arr[1])) + return [pointer + arr[1], meta]; +} + +let v = parse(input)[1]; +console.log(v.reduce((a, b) => a + b)); diff --git a/2018/8/test.txt b/2018/8/test.txt new file mode 100644 index 0000000..cc22edc --- /dev/null +++ b/2018/8/test.txt @@ -0,0 +1,4 @@ +2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2 +A-----------------------------1-1-2 + B---10-11-12 C----------2 + D---99 \ No newline at end of file diff --git a/2018/8/test1.txt b/2018/8/test1.txt new file mode 100644 index 0000000..29b650e --- /dev/null +++ b/2018/8/test1.txt @@ -0,0 +1,4 @@ +3 2 1 1 0 0 5 0 3 1 2 3 1 0 0 0 67 67 +A - - - - - - - - - - - - - - - 67 67 + B - - - 5 C - 1 2 3 D - - - + C - E - \ No newline at end of file diff --git a/2018/README.md b/2018/README.md index 4f73a68..5672e26 100644 --- a/2018/README.md +++ b/2018/README.md @@ -5,8 +5,8 @@ 1. [js :star: :star:](https://adventofcode.com/2015/day/4 "see puzzle") 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:](https://adventofcode.com/2015/day/7 "see puzzle") -1. [-](https://adventofcode.com/2015/day/8 "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") # Languages Used ### Python @@ -23,3 +23,4 @@ 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") diff --git a/README.md b/README.md index 3b4b7b4..a49d8bd 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ if (advent) { 1. [js :star: :star:](https://adventofcode.com/2015/day/2 "see puzzle") 1. [py :star: :star:](https://adventofcode.com/2015/day/3 "see puzzle") 1. [py :star: :star:](https://adventofcode.com/2015/day/5 "see puzzle") -1. [-](https://adventofcode.com/2015/day/1 "see puzzle") +1. [-](https://adventofcode.com/2015/day/6 "see puzzle") #### [2018](https://adventofcode.com/2018 "2018 puzzle calendar") 1. [js :star: :star:](https://adventofcode.com/2015/day/1 "see puzzle") @@ -24,7 +24,7 @@ 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. [-](https://adventofcode.com/2015/day/8 "see puzzle") +1. [js :star:](https://adventofcode.com/2015/day/8 "see puzzle") ## Languages Used * Python 8×:star: