Skip to content

Commit 0fe7ed7

Browse files
feat(2018 day-08): parse input to usable data
1 parent 480c620 commit 0fe7ed7

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

2018/day-08/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const loadInput = (callback) => {
66
fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => {
77
if (err) throw err
88

9-
const list = data.split(' ')
9+
const list = data.split(' ').map(Number)
1010
if (typeof callback === 'function') {
1111
callback(list)
1212
}

2018/day-08/license.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1+
/**
2+
* Splits the array of data into lists of the number of children
3+
* @param {Array} input list of data values from license input
4+
* @param {Number} expected Number of children in the data
5+
* @returns {Object} { children: [node], remainder: [data values]}
6+
*/
7+
const findChildren = (input, expected) => {
8+
let results = []
9+
10+
while (results.length < expected) {
11+
let result = {
12+
children: [],
13+
metadata: []
14+
}
15+
let childCount = input.shift()
16+
let metaCount = input.shift()
17+
let childSearchResults = findChildren(input, childCount)
18+
19+
result.children = childSearchResults.children
20+
input = childSearchResults.remainder
21+
if (metaCount > 0) {
22+
for (let r = 0; r < metaCount; r++) {
23+
result.metadata.push(input.shift())
24+
}
25+
}
26+
27+
results.push(result)
28+
}
29+
30+
return {
31+
children: results,
32+
remainder: input
33+
}
34+
}
35+
136
/**
237
* Parses the data stream to generate a structured data object
3-
* @param {Array} data list of data valeus from license input
38+
* @param {Array} input list of data valeus from license input
439
* @returns {Object} the structured data nodes
540
*/
6-
const parseData = (data) => {
7-
return data
41+
const parseData = (input) => {
42+
return findChildren(input, 1).children[0]
843
}
944

1045
/**

2018/day-08/license.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const {
55
sumMetadata
66
} = require('./license')
77

8-
const testData = `2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2`.split(' ')
8+
const testData = `2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2`.split(' ').map(Number)
99

1010
describe('--- Day 8: Memory Maneuver ---', () => {
1111
describe('Part 1:', () => {
1212
describe('parseData', () => {
13-
it.skip('Parses the license data to generate a usable object', () => {
13+
it('Parses the license data to generate a usable object', () => {
1414
const expected = {
1515
children: [
1616
{

0 commit comments

Comments
 (0)