Skip to content

Commit 1357693

Browse files
committed
feature: JSAbacusFramework.io
1 parent c400d45 commit 1357693

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Collect stars by helping Santa solve puzzles. Two puzzles will be made available
1717
- [Day 9: All in a Single Night](day-09-all-in-a-single-night/)
1818
- [Day 10: Elves Look, Elves Say](day-10-elves-look-elves-say/)
1919
- [Day 11: Corporate Policy](day-11-corporate-policy/)
20+
- [Day 12: JSAbacusFramework.io](day-12-js-abacus-framework-io/)
2021

2122
## Running Tests
2223

Diff for: day-12-js-abacus-framework-io/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Day 12: JSAbacusFramework.io
2+
3+
Santa's Accounting-Elves need help balancing the books after a recent order. Unfortunately, their accounting software uses a peculiar storage format. That's where you come in.
4+
5+
They have a [JSON](http://json.org/) document which contains a variety of things: arrays (`[1,2,3]`), objects (`{"a":1, "b":2}`), numbers, and strings. Your first job is to simply find all of the **numbers** throughout the document and add them together.
6+
7+
For example:
8+
9+
- `[1,2,3]` and `{"a":2,"b":4}` both have a sum of `6`.
10+
- `[[[3]]]` and `{"a":{"b":4},"c":-1}` both have a sum of `3`.
11+
- `{"a":[-1,1]}` and `[-1,{"a":1}]` both have a sum of `0`.
12+
- `[]` and `{}` both have a sum of `0`.
13+
14+
You will not encounter any strings containing numbers.
15+
16+
What is the **sum of all numbers** in the document?
17+
18+
## References
19+
- https://adventofcode.com/2015/day/12

Diff for: day-12-js-abacus-framework-io/abacus.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const traverse = (node) => {
2+
if (Number.isInteger(node)) {
3+
return node;
4+
} else if (typeof node === 'string') {
5+
return 0;
6+
} else if (Array.isArray(node)) {
7+
const sum = node
8+
.map((x) => traverse(x))
9+
.reduce((a, b) => a + b, 0);
10+
11+
return sum;
12+
} else if (typeof node === 'object') {
13+
return Object
14+
.keys(node)
15+
.map((k) => {
16+
return traverse(node[k]);
17+
})
18+
.reduce((a, b) => a + b, 0);
19+
}
20+
};
21+
22+
const abacus = (input) => {
23+
return traverse(input);
24+
};
25+
26+
module.exports = abacus;

Diff for: day-12-js-abacus-framework-io/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require('assert');
2+
3+
const abacus = require('./abacus');
4+
5+
describe('Day 12: JSAbacusFramework.io', () => {
6+
it('should calculate total number', () => {
7+
assert.strictEqual(abacus([1, 2, 3]), 6);
8+
assert.strictEqual(abacus({ 'a': 2, 'b': 4 }), 6);
9+
assert.strictEqual(abacus([[[3]]]), 3);
10+
assert.strictEqual(abacus({ 'a': { 'b': 4 }, 'c': -1 }), 3);
11+
assert.strictEqual(abacus({ 'a': [-1, 1] }), 0);
12+
assert.strictEqual(abacus([-1, { 'a': 1 }]), 0);
13+
assert.strictEqual(abacus([]), 0);
14+
assert.strictEqual(abacus({}), 0);
15+
});
16+
});

0 commit comments

Comments
 (0)