-
Notifications
You must be signed in to change notification settings - Fork 15
/
encoding.js
40 lines (35 loc) · 870 Bytes
/
encoding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const G = require('generatorics');
const sum = (a, b) => a + b;
const byAscending = (a, b) => a - b;
const hasSum = (num, list) => {
for (let [x, y] of G.combination(list, 2)) {
if (x + y === num) {
return true;
}
}
return false;
};
function partOne(input) {
const JUMP = 25;
for (let i = JUMP; i < input.length - 1; i++) {
let num = input[i];
let prev = input.slice(i - JUMP, i);
if (!hasSum(num, prev)) {
return num;
}
}
}
function partTwo(input, part_one_answer) {
for (let nums = 2; nums < input.length - 1; nums++) {
for (let i = 0; i < input.length - nums; i++) {
let vals = input.slice(i, i + nums);
if (vals.reduce(sum) === part_one_answer) {
vals.sort(byAscending);
let smallest = vals[0];
let largest = vals[vals.length - 1];
return smallest + largest;
}
}
}
}
module.exports = { partOne, partTwo };