-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.js
33 lines (25 loc) · 976 Bytes
/
1.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
import input from "./input.js";
class AlignSubmarineCrabs {
fuelRequiredForPositions = []
constructor (crabsXPositions) {
const minXPosition = Math.min(...crabsXPositions)
const maxXPosition = Math.max(...crabsXPositions)
this.fuelRequiredForPositions = Array(maxXPosition - minXPosition + 1).fill(0)
for (let currentPosition = minXPosition; currentPosition <= maxXPosition; currentPosition++) {
crabsXPositions.forEach(crabXPosition => {
this.fuelRequiredForPositions[currentPosition] += Math.abs(currentPosition - crabXPosition)
})
}
}
getResult = () => {
return Math.min(...this.fuelRequiredForPositions)
}
}
const output = new AlignSubmarineCrabs(input).getResult()
console.log({
title: 'The Treachery of Whales [7.1]',
url: 'https://adventofcode.com/2021/day/7',
inputPreview: input.slice(0, 10),
inputLength: input.length,
output,
})