Skip to content

Commit 75259c8

Browse files
feat(2022-day-01): solution for part two, getting the top 3 elves' calories
1 parent ebb9f34 commit 75259c8

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Diff for: 2022/day-01/solution.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
3-
const { findElfWithMost, parseCalorieData } = require('./calories')
3+
const { findElfWithMost, parseCalorieData, sortElvesByCalories } = require('./calories')
44
const filePath = path.join(__dirname, 'input.txt')
55
// const { inputToArray } = require('../../2018/inputParser')
66

@@ -11,19 +11,23 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
1111

1212
const resetInput = () => {
1313
// Deep copy to ensure we aren't mutating the original data
14-
return JSON.parse(JSON.stringify(initData))
14+
return parseCalorieData(JSON.parse(JSON.stringify(initData)))
1515
}
1616

1717
const part1 = () => {
1818
const data = resetInput()
19-
const elfWithMost = findElfWithMost(parseCalorieData(data))
19+
const elfWithMost = findElfWithMost(data)
2020
return elfWithMost.reduce((a, b) => a + b) // Tally calories for elf with most
2121
}
2222

2323
const part2 = () => {
2424
const data = resetInput()
25-
console.debug(data)
26-
return 'No answer yet'
25+
const elves = sortElvesByCalories(data)
26+
let results = 0
27+
for (let x = 0; x < 3; x++) { // iterate through the top 3 results
28+
results += elves[x].reduce((a, b) => a + b, 0) // sum the calorie counts
29+
}
30+
return results
2731
}
2832
const answers = []
2933
answers.push(part1())

0 commit comments

Comments
 (0)