diff options
Diffstat (limited to 'challenge-100/dave-jacoby/node/ch-2.js')
| -rw-r--r-- | challenge-100/dave-jacoby/node/ch-2.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-100/dave-jacoby/node/ch-2.js b/challenge-100/dave-jacoby/node/ch-2.js new file mode 100644 index 0000000000..5f023375c6 --- /dev/null +++ b/challenge-100/dave-jacoby/node/ch-2.js @@ -0,0 +1,45 @@ +"use strict"; + +var input = []; +input.push([[1], [2, 4], [6, 4, 9], [5, 1, 7, 2]]); +input.push([[3], [3, 1], [5, 2, 3], [4, 3, 1, 3]]); + +for (let i = 0; i < input.length; i++) { + triangle_sum(input[i]); +} + +function triangle_sum(input) { + let results = triangle(input, 0, 0, []).sort(function(a, b) { return a - b }); + console.log(input); + console.log(results[0]); + console.log(""); +} + +function triangle(input, x, y, path) { + if (x > 5) { + return; + } + let output = []; + + if ("undefined" === typeof input[x]) { + output.push(path.reduce((a, b) => a + b, 0)); + } else { + let v = input[x][y]; + let next_path = [...path]; + next_path.push(v); + + output.push(...triangle(input, x + 1, y, next_path)); + output.push(...triangle(input, x + 1, y + 1, next_path)); + } + return output; +} + + +// [ [ 1 ], [ 2, 4 ], [ 6, 4, 9 ], [ 5, 1, 7, 2 ] ] +// 8 +// +// [ [ 3 ], [ 3, 1 ], [ 5, 2, 3 ], [ 4, 3, 1, 3 ] ] +// 7 + + + |
