-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day7.js
46 lines (32 loc) · 1.47 KB
/
Day7.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
require('./utils.js');
module.exports = {
this:part1 = function () {
var positions = []
readFile('../data/day7.txt')[0].split(",").forEach((num_s) => {
positions.push(parseInt(num_s));
});
var min_cost = Number.MAX_VALUE;
for (var position_end_id = 0; position_end_id < positions.length; position_end_id++) {
var position_cost = 0
for (var position_crab_id = 0; position_crab_id < positions.length; position_crab_id++)
position_cost += Math.abs(positions[position_end_id] - positions[position_crab_id]);
min_cost = Math.min(min_cost, position_cost);
}
console.info("Day: 7 | Part: 1 | Result:", min_cost)
},
this:part2 = function () {
var positions = []
readFile('../data/day7.txt')[0].split(",").forEach((num_s) => {
positions.push(parseInt(num_s));
});
var min_cost = Number.MAX_VALUE;
for (var position_end = 0; position_end < Math.max(...positions); position_end++) {
var position_cost = 0
for (var position_crab_id = 0; position_crab_id < positions.length; position_crab_id++)
for (var step = 1; step <= Math.abs(position_end - positions[position_crab_id]); step++)
position_cost += step
min_cost = Math.min(min_cost, position_cost);
}
console.info("Day: 7 | Part: 2 | Result:", min_cost)
}
}