From 75bfcd3838d4d2e98b9382975e27fefa86c98113 Mon Sep 17 00:00:00 2001 From: Tristan Date: Sat, 1 Dec 2018 14:12:01 +0000 Subject: [PATCH] 2018 d1 p2 --- 2018/1/input.txt | 3 +-- 2018/1/part2.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 2018/1/part2.js diff --git a/2018/1/input.txt b/2018/1/input.txt index 8855f9d..444c50b 100644 --- a/2018/1/input.txt +++ b/2018/1/input.txt @@ -956,5 +956,4 @@ -3 -18 -7 -+127054 - ++127054 \ No newline at end of file diff --git a/2018/1/part2.js b/2018/1/part2.js new file mode 100644 index 0000000..5bdf32b --- /dev/null +++ b/2018/1/part2.js @@ -0,0 +1,21 @@ +const fs = require('fs'); + +let input = fs.readFileSync('input.txt', 'utf-8'); +let freq = 0; +let history = [0]; +found = false; +while (!found) { + for (let line of input.split('\n')) { + num = parseInt(line); + if (num) { + freq += num; + } + if (history.includes(freq)) { + found = true; + break; + } else { + history.push(freq); + } + } +} +console.log(freq)