Skip to content

Commit ebb9f34

Browse files
feat(2022-day-01): exposes calorie-based elf sorting
1 parent 6a42287 commit ebb9f34

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

2022/day-01/calories.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const parseCalorieData = (data) => {
1717
}
1818

1919
const findElfWithMost = (data) => {
20+
return sortElvesByCalories(data)[0] // Sort for the elf with the most calories
21+
}
22+
23+
const sortElvesByCalories = (data) => {
2024
const sum = (a, b) => { return a + b }
2125
const compare = (a, b) => {
2226
// compare sums of array values for sum-based sorting
@@ -27,10 +31,11 @@ const findElfWithMost = (data) => {
2731
)
2832
}
2933
data.sort(compare)
30-
return data[0] // Sort for the elf with the most calories
34+
return data
3135
}
3236

3337
module.exports = {
3438
findElfWithMost,
35-
parseCalorieData
39+
parseCalorieData,
40+
sortElvesByCalories
3641
}

2022/day-01/calories.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { findElfWithMost, parseCalorieData } = require('./calories')
3+
const { findElfWithMost, parseCalorieData, sortElvesByCalories } = require('./calories')
44

55
const calorieData = `1000
66
2000
@@ -39,5 +39,17 @@ describe('--- Day 1: Calorie Counting ---', () => {
3939
.to.equal(24000)
4040
})
4141
})
42+
describe('sortElvesByCalories()', () => {
43+
it('Sorts the list of elves by calories carried, maximum first', () => {
44+
expect(sortElvesByCalories(parsedCalorieData))
45+
.to.deep.equal([
46+
[7000, 8000, 9000],
47+
[5000, 6000],
48+
[10000],
49+
[1000, 2000, 3000],
50+
[4000]
51+
])
52+
})
53+
})
4254
})
4355
})

0 commit comments

Comments
 (0)