-
Notifications
You must be signed in to change notification settings - Fork 1
/
07.js
44 lines (41 loc) · 844 Bytes
/
07.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
'use strict';
/**
* @param {string} d
*/
export const part1 = async d => {
const data = d.split(',').map(e => parseInt(e, 10));
const max = Math.max(...data);
const min = Math.min(...data);
let targetF = 0xffffff;
for (let t = min; t <= max; t++) {
let fuel = 0;
data.forEach(e=> {
fuel += Math.abs(t - e);
});
if (fuel < targetF) {
targetF = fuel;
}
}
return targetF;
};
/**
* @param {string} d
*/
export const part2 = async d => {
const data = d.split(',').map(e => parseInt(e, 10));
const max = Math.max(...data);
const min = Math.min(...data);
let targetF = 9007199254740991;
for (let t = min; t <= max; t++) {
let fuel = 0;
data.forEach(e=> {
const f = Math.abs(t - e);
fuel += f * (f + 1) / 2;
});
if (fuel < targetF) {
targetF = fuel;
}
}
return targetF;
// 16777215 too low
};