Skip to content

Commit 7aded4b

Browse files
feat(2018 day-08): tally the values for part 2
1 parent f3d57c1 commit 7aded4b

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

2018/day-08/license.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ const findChildren = (input, expected) => {
2424
}
2525
}
2626

27+
// Calculate the value of the node (Part 2)
28+
result.value = 0
29+
if (result.children.length < 1) {
30+
// No children, total the metadata
31+
result.value = result.metadata.reduce((acc, curr) => { return acc + curr }, 0)
32+
} else {
33+
// Has children, use the metadata as off-by-one keys to total children's values
34+
// metadata = 0 means no child, 1 is first child, 2 is second, etc
35+
// We iterate instead of map() because metadata keys can be repeated
36+
result.metadata.forEach((key) => {
37+
if (typeof result.children[key - 1] !== 'undefined') {
38+
result.value += result.children[key - 1].value
39+
}
40+
})
41+
}
42+
2743
results.push(result)
2844
}
2945

2018/day-08/license.test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ describe('--- Day 8: Memory Maneuver ---', () => {
1818
children: [
1919
{
2020
children: [],
21-
metadata: [10, 11, 12]
21+
metadata: [10, 11, 12],
22+
value: 33
2223
}, {
2324
children: [
2425
{
2526
children: [],
26-
metadata: [99]
27+
metadata: [99],
28+
value: 99
2729
}
2830
],
29-
metadata: [2]
31+
metadata: [2],
32+
value: 0
3033
}
3134
],
32-
metadata: [1, 1, 2]
35+
metadata: [1, 1, 2],
36+
value: 66
3337
}
3438
const actual = parseData(testData)
3539
expect(actual).to.deep.equal(expected)
@@ -43,4 +47,7 @@ describe('--- Day 8: Memory Maneuver ---', () => {
4347
})
4448
})
4549
})
50+
describe('Part 2:', () => {
51+
describe('values on each node covered in data parsing in Part 1. Nothing additional to test.', () => {})
52+
})
4653
})

2018/day-08/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
const init = (data) => {
88
data = parseData(data)
99
const answer = sumMetadata(data)
10-
const answer2 = ''
10+
const answer2 = data.value
1111
console.log(`-- Part 1 --`)
1212
console.log(`Answer: ${answer}`)
1313
console.log(`-- Part 2 --`)

0 commit comments

Comments
 (0)