-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.js
38 lines (29 loc) · 886 Bytes
/
2.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
import input from "./input.js";
const sonarSweep = (numbers) => {
let stackedNumbers = []
let currentSum = 0
let previousSum = 0
let higherThanPrevious = 0
while (0 < numbers.length) {
const currentNumber = numbers.shift()
stackedNumbers.push(currentNumber)
if (3 > stackedNumbers.length) {
continue
}
currentSum = stackedNumbers.reduce((count, currentValue) => count + currentValue)
if (0 !== previousSum && currentSum > previousSum) {
higherThanPrevious++
}
previousSum = currentSum
stackedNumbers.shift()
}
return higherThanPrevious
}
const output = sonarSweep(input)
console.log({
title: 'Sonar Sweep [1.1]',
url: 'https://adventofcode.com/2021/day/1#part2',
inputPreview: input.slice(0, 5),
inputLength: input.length,
output,
})