diff options
Diffstat (limited to '2020/09/solve.js')
-rw-r--r-- | 2020/09/solve.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/2020/09/solve.js b/2020/09/solve.js new file mode 100644 index 0000000..394bd4b --- /dev/null +++ b/2020/09/solve.js @@ -0,0 +1,45 @@ +const fs = require("fs"); +const data = fs + .readFileSync("input", "utf8") + .split("\n") + .map((x) => parseInt(x)); + +function partOne() { + for (var i = 25; i < data.length; i++) { + const prev = data.slice(i - 25, i); + let correct = false; + for (let x = 0; x < prev.length; x++) { + for (let y = 0; y < prev.length; y++) { + if (prev[x] + prev[y] === data[i]) { + correct = true; + break; + } + } + if (correct) break; + } + + if (!correct) return data[i]; + } + return 0; +} + +function partTwo(num) { + for (let i = 0; i < data.length; i++) { + let cnt = 0; + let j = i; + for (; j < data.length; j++) { + cnt += data[j]; + if (cnt >= num) break; + } + if (cnt == num) { + const range = data.slice(i, j + 1); + return Math.min(...range) + Math.max(...range); + } else { + cnt = 0; + } + } +} + +const num = partOne(); +console.log(num); +console.log(partTwo(num)); |