-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_7.js
More file actions
43 lines (33 loc) · 1.03 KB
/
Copy pathday_7.js
File metadata and controls
43 lines (33 loc) · 1.03 KB
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
const fs = require('fs')
const findLeastMove = (numbers, expensive) => {
const fuels = []
const min = Math.min(...numbers)
const max = Math.max(...numbers)
const fillers = filler(min, max)
fillers.forEach((value) => {
const result = numbers.map((num) => {
const distingo = value >= num ? value - num : num - value
return expensive ? taxes(distingo) : distingo
})
const fuel = result.reduce((acc, curr) => acc+curr)
fuels.push(fuel)
})
return Math.min(...fuels)
}
const filler = (min, max) => {
let fillers = []
for (let i=min; i<max; i++) {
fillers.push(i)
}
return fillers
}
const taxes = (n) => n*(n+1)/2
fs.readFile('./inputs/7.txt', 'utf8', (err, response) => {
if (err) throw err
const data = response.split(',').map(num => parseInt(num, 10))
// PART ONE
const partOne = findLeastMove(data, false)
// PART TWO
const partTwo = findLeastMove(data, true)
console.log({partOne, partTwo})
})