Skip to content

Commit e62aa22

Browse files
Merge pull request #205 from amclin/feat/2021-day-07
Feat/2021 day 07
2 parents 62cd42a + b3a27cc commit e62aa22

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

2021/day-07/crabs.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const sum = (x, y) => x + y
2+
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)
22+
}
23+
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+
}
34+
return fuel
35+
}
36+
37+
module.exports = {
38+
getFuel,
39+
getLeastFuel
40+
}

2021/day-07/crabs.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { getFuel, getLeastFuel } = require('./crabs')
4+
5+
const testCrabs = [16, 1, 2, 0, 4, 2, 7, 1, 2, 14]
6+
7+
describe('--- Day 7: The Treachery of Whales ---', () => {
8+
describe('Part 1', () => {
9+
describe('getFuel()', () => {
10+
it('counts how much fuel is needed to position all the crabs', () => {
11+
expect(getFuel(testCrabs, 2)).to.equal(37)
12+
expect(getFuel(testCrabs, 1)).to.equal(41)
13+
expect(getFuel(testCrabs, 3)).to.equal(39)
14+
expect(getFuel(testCrabs, 10)).to.equal(71)
15+
})
16+
})
17+
describe('getLeastFuel()', () => {
18+
it('determine the fuel spent for the least costly position', () => {
19+
expect(getLeastFuel(testCrabs)).to.equal(37)
20+
})
21+
})
22+
})
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+
})
36+
})

2021/day-07/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const console = require('../helpers')
3+
require('./solution')

2021/day-07/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,767,559,240,1779,740,550,84,819,108,728,212,650,264,899,1231,90,127,46,219,1494,192,473,163,1456,1048,22,4,1121,9,37,180,531,1167,395,456,952,1807,199,606,293,1003,236,235,1294,102,4,724,672,458,511,431,162,48,900,229,4,570,316,111,520,720,343,1064,314,261,1102,397,1585,610,509,1374,157,128,353,440,435,864,1489,425,762,320,1369,233,903,1181,1203,598,9,938,659,214,2,141,468,1485,631,265,156,165,987,2,856,564,120,325,16,743,849,822,51,18,1153,1100,143,301,402,717,126,937,391,36,802,1399,512,461,250,679,646,303,556,574,948,362,395,402,253,1631,1,203,76,48,246,115,117,15,953,926,210,1257,96,47,147,1058,1129,1166,183,375,1404,697,237,191,760,431,38,1778,159,698,411,160,289,23,836,554,841,289,892,276,877,216,751,1273,388,392,289,78,324,1142,896,767,416,780,373,117,1,71,235,302,307,906,321,480,991,1586,1491,1617,653,499,1323,156,455,19,669,169,415,284,743,439,705,980,1350,210,118,797,81,67,894,183,10,439,950,1126,576,828,85,681,517,19,872,119,164,882,31,244,195,640,41,313,888,303,224,433,462,389,329,254,488,570,286,945,1377,32,1101,206,457,584,662,1176,9,1164,227,467,239,1094,131,420,871,942,52,1276,14,72,7,1099,702,53,118,263,645,181,60,105,780,1102,550,85,225,1159,250,1424,8,1060,458,79,299,744,76,1513,338,68,179,323,644,65,293,66,153,699,819,966,678,207,538,372,284,101,224,305,103,1276,577,316,350,647,314,1256,595,1332,133,83,654,1072,63,446,46,5,92,939,608,940,257,851,1715,742,96,1497,240,1154,30,69,803,13,47,380,97,349,742,581,768,94,454,25,330,899,584,1425,447,207,1621,329,429,22,361,3,24,534,361,695,61,680,517,43,129,1686,301,1090,211,680,362,855,700,392,354,871,154,485,654,203,1417,208,1228,243,317,899,106,307,62,157,186,291,475,616,137,113,1367,24,778,431,1563,36,651,131,259,165,765,226,28,1410,456,1601,11,21,323,214,208,1444,11,108,49,1182,89,564,1266,478,1324,538,1572,488,1546,434,1168,615,285,507,561,100,1092,30,866,946,840,322,625,106,101,157,209,531,63,133,103,715,666,1655,81,1439,1016,32,441,86,1597,1273,443,732,160,162,528,727,150,107,21,111,10,502,302,1315,643,84,318,1488,315,150,5,248,675,167,691,101,412,584,992,1317,18,1046,164,359,111,1105,96,16,301,463,680,443,433,477,420,1141,362,1840,12,57,1094,806,23,708,243,1060,894,403,941,958,240,903,497,1342,1068,35,399,381,19,499,339,0,226,108,292,1607,281,72,283,316,182,224,33,488,786,1456,25,104,201,549,827,890,1520,931,70,763,25,633,464,822,751,327,144,62,1205,78,1007,216,324,316,289,682,1359,198,204,199,29,580,10,338,45,150,217,290,734,985,1654,201,934,0,793,956,549,230,1337,183,115,229,31,122,90,1264,122,1292,278,78,256,919,365,444,455,1235,484,45,1646,21,895,218,179,1311,141,238,1330,40,593,518,95,466,233,125,777,150,315,606,265,935,13,89,961,394,341,88,485,57,725,665,616,889,577,100,154,686,842,772,581,1311,604,41,62,1439,313,320,225,1115,279,176,995,12,70,739,96,4,2,37,252,1164,1243,899,856,10,219,233,1430,443,1011,30,378,81,39,167,1060,9,601,663,89,718,1192,1579,511,85,180,236,1079,556,496,215,192,718,300,1282,475,984,535,1760,1137,439,759,221,125,1298,542,1119,446,204,90,16,84,63,176,26,123,1157,140,518,1115,514,701,1207,547,39,970,240,584,77,66,44,858,560,21,648,309,1096,618,220,28,75,1442,233,1,86,325,244,161,218,6,229,1104,275,754,60,186,882,232,133,1288,42,697,152,252,396,345,38,672,980,1514,468,102,563,871,313,358,97,28,1018,830,182,32,1335,525,490,419,1182,946,362,57,496,799,194,504,1615,440,566,481,283,1422,133,919,185,695,871,1422,1372,250,96,438,743,954,1363,349,814,1235,642,461,160,135,131,61,250,188,125,698,346,470,603,1391,460,578,404,3,14,1715,1271,856,1334,28,739,274,628,70,456,393,5,326,382,70,244,101,560,424,1521,25,1441,147,851,1207,747,84,703,172,101,87,357,421,91,939,595,581,149,626,797,1485,419,192,828,1031,1283,333,614,479,1344,520,1434,1422,877

2021/day-07/solution.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const filePath = path.join(__dirname, 'input.txt')
4+
const { parseData } = require('../../2018/inputParser')
5+
const { getLeastFuel } = require('./crabs')
6+
7+
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
8+
if (err) throw err
9+
10+
initData = parseData(initData.trim())
11+
12+
const resetInput = () => {
13+
// Deep copy to ensure we aren't mutating the original data
14+
return JSON.parse(JSON.stringify(initData))
15+
}
16+
17+
const part1 = () => {
18+
const data = resetInput()
19+
const result = getLeastFuel(data)
20+
return result
21+
}
22+
23+
const part2 = () => {
24+
const data = resetInput()
25+
const result = getLeastFuel(data, true)
26+
return result
27+
}
28+
const answers = []
29+
answers.push(part1())
30+
answers.push(part2())
31+
32+
answers.forEach((ans, idx) => {
33+
console.info(`-- Part ${idx + 1} --`)
34+
console.info(`Answer: ${ans}`)
35+
})
36+
})

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('./2021/day-06/solution')
1+
require('./2021/day-07/solution')

0 commit comments

Comments
 (0)