Skip to content

Commit b3a27cc

Browse files
feat(2021-day-07): find least fuel usage using complex expenditure
Crabs have an exponential (logarithmic?) cost for movement. Solves part 2
1 parent 1849294 commit b3a27cc

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

2021/day-07/crabs.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
const sum = (x, y) => x + y
22

3-
const getFuel = (crabs, destination) => {
4-
return crabs.map((crab) => Math.abs(crab - destination))
5-
.reduce(sum)
3+
const getFuel = (crabs, destination, exponential = false) => {
4+
const simpleCalc = (crab) => {
5+
const distance = Math.abs(crab - destination)
6+
return distance
7+
}
8+
9+
const expoCalc = (crab) => {
10+
const distance = Math.abs(crab - destination)
11+
let fuel = 0
12+
for (let x = 1; x <= distance; x++) {
13+
fuel += x
14+
}
15+
return fuel
16+
}
17+
18+
if (exponential) {
19+
return crabs.map(expoCalc).reduce(sum)
20+
}
21+
return crabs.map(simpleCalc).reduce(sum)
622
}
723

8-
const getLeastFuel = (crabs) => {
9-
const positions = crabs
10-
let fuel = crabs.length * crabs.length // assume a cluster of crabs super far away is the initial worst case
11-
positions.forEach((position) => {
12-
fuel = Math.min(fuel, getFuel(crabs, position))
13-
})
24+
const getLeastFuel = (crabs, exponential = false) => {
25+
const positions = JSON.parse(JSON.stringify(crabs)) // Deep copy to ensure we aren't mutating the original data
26+
let fuel = 100000000 // assume a stupid high fuel count to start
27+
const highest = positions.sort((a, b) => b - a)[0] // Find the largest position
28+
for (let x = 0; x <= highest; x++) {
29+
console.debug(`Checking position ${x}`)
30+
const proposed = getFuel(crabs, x, exponential)
31+
console.debug(`Needed fuel would be ${proposed}`)
32+
fuel = Math.min(fuel, proposed)
33+
}
1434
return fuel
1535
}
1636

2021/day-07/crabs.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@ describe('--- Day 7: The Treachery of Whales ---', () => {
2020
})
2121
})
2222
})
23+
describe('Part 2', () => {
24+
describe('getFuel() exponentially', () => {
25+
it('counts how much fuel is exponentially needed to position all the crabs', () => {
26+
expect(getFuel(testCrabs, 5, true)).to.equal(168)
27+
expect(getFuel(testCrabs, 2, true)).to.equal(206)
28+
})
29+
})
30+
describe('getLeastFuel() exponentially', () => {
31+
it('determine the fuel exponentially spent for the least costly position', () => {
32+
expect(getLeastFuel(testCrabs, true)).to.equal(168)
33+
})
34+
})
35+
})
2336
})

2021/day-07/solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
2222

2323
const part2 = () => {
2424
const data = resetInput()
25-
console.debug(data)
26-
return 'No answer yet'
25+
const result = getLeastFuel(data, true)
26+
return result
2727
}
2828
const answers = []
2929
answers.push(part1())

0 commit comments

Comments
 (0)