-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.js
54 lines (45 loc) · 1.1 KB
/
day01.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict' // Sonar Sweep
const { loadData, parseInt } = require('./core/utils')
const rawInput = [loadData(module.filename)]
// Count the values bigger than previous.
const puzzle1 = (readings) => {
let prev = undefined
return readings.reduce((result, value) => {
result += value > prev ? 1 : 0
prev = value
return result
}, 0)
}
// Count the averages of 3 bigger than previous.
const puzzle2 = (readings) => {
const sums = new Array(readings.length - 2)
for (let i = 0; i < readings.length - 2; ++i) {
sums[i] = readings[i] + readings[i + 1] + readings[i + 2]
}
return puzzle1(sums)
}
const parse = (dsn) => {
let data = rawInput[dsn]
if (data && (data = data.split('\n').filter(v => Boolean(v))).length) {
return data.map(parseInt) // .sort((a, b) => a - b)
}
}
rawInput[1] = `199
200
208
210
200
207
240
269
260
263`
module.exports = { parse, puzzles: [puzzle1, puzzle2] }
/*
day01, set #1
puzzle-1 ( 73 µsecs): 7
puzzle-2 ( 43 µsecs): 5
day01, set #0
puzzle-1 ( 51 µsecs): 1759
puzzle-2 ( 252 µsecs): 1805
*/