Skip to content

Commit b880e67

Browse files
feat(2022-day-01): parse and sort elves by calories carried
1 parent 77f3910 commit b880e67

File tree

6 files changed

+122
-1
lines changed

6 files changed

+122
-1
lines changed

2022/day-01/calories.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Takes the provided list of items and breaks
3+
* it up into the list of individual elves with
4+
* the items they carry
5+
* @param {string} data List of items split by lines
6+
* @returns array List of elves' payloads
7+
*/
8+
const parseCalorieData = (data) => {
9+
const pattern = /\r?\n/g
10+
let results = data.replace(pattern, ',') // switch to commas to avoid OS newline character discrepancies
11+
results = results.split(',,') // double commas indicate where one elf stops and the next starts
12+
const parseElf = (elfData) => {
13+
return elfData.split(',') // each elf can carry a varying number of items
14+
.map((cal) => parseInt(cal)) // make sure we're working with numbers
15+
}
16+
return results.map(parseElf)
17+
}
18+
19+
const findElfWithMost = (data) => {
20+
const sum = (a, b) => { return a + b }
21+
const compare = (a, b) => {
22+
// compare sums of array values for sum-based sorting
23+
return b.reduce(
24+
sum, 0
25+
) - a.reduce(
26+
sum, 0
27+
)
28+
}
29+
data.sort(compare)
30+
return data[0] // Sort for the elf with the most calories
31+
}
32+
33+
module.exports = {
34+
findElfWithMost,
35+
parseCalorieData
36+
}

2022/day-01/calories.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { findElfWithMost, parseCalorieData } = require('./calories')
4+
5+
const calorieData = `1000
6+
2000
7+
3000
8+
9+
4000
10+
11+
5000
12+
6000
13+
14+
7000
15+
8000
16+
9000
17+
18+
10000`
19+
const parsedCalorieData = [
20+
[1000, 2000, 3000],
21+
[4000],
22+
[5000, 6000],
23+
[7000, 8000, 9000],
24+
[10000]
25+
]
26+
27+
describe('--- Day 1: Calorie Counting ---', () => {
28+
describe('Part 1', () => {
29+
describe('parseCalorieData', () => {
30+
it('Splits data into a list of elves with provisions', () => {
31+
expect(parseCalorieData(calorieData))
32+
.to.deep.equal(parsedCalorieData)
33+
})
34+
})
35+
describe('findElfWithMost()', () => {
36+
it('Identifies the elf with the most total calories', () => {
37+
expect(findElfWithMost(parsedCalorieData)
38+
.reduce((a, b) => a + b))
39+
.to.equal(24000)
40+
})
41+
})
42+
})
43+
})

2022/day-01/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')

2022/day-01/input.txt

Whitespace-only changes.

2022/day-01/solution.js

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

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
},
2020
"keywords": [
2121
"advent-of-code",
22-
"advent-of-code-2018"
22+
"advent-of-code-2018",
23+
"advent-of-code-2019",
24+
"advent-of-code-2020",
25+
"advent-of-code-2021",
26+
"advent-of-code-2022"
2327
],
2428
"author": "Anthony McLin",
2529
"license": "MIT",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy