-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from amclin/feat/2022-day-01
Feat/2022 day 01
- Loading branch information
Showing
15 changed files
with
2,403 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ class Plants { | |
|
||
return { | ||
position: pot.position, | ||
state: state | ||
state | ||
} | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Takes the provided list of items and breaks | ||
* it up into the list of individual elves with | ||
* the items they carry | ||
* @param {string} data List of items split by lines | ||
* @returns array List of elves' payloads | ||
*/ | ||
const parseCalorieData = (data) => { | ||
const pattern = /\r?\n/g | ||
let results = data.replace(pattern, ',') // switch to commas to avoid OS newline character discrepancies | ||
results = results.split(',,') // double commas indicate where one elf stops and the next starts | ||
const parseElf = (elfData) => { | ||
return elfData.split(',') // each elf can carry a varying number of items | ||
.map((cal) => parseInt(cal)) // make sure we're working with numbers | ||
} | ||
return results.map(parseElf) | ||
} | ||
|
||
const findElfWithMost = (data) => { | ||
return sortElvesByCalories(data)[0] // Sort for the elf with the most calories | ||
} | ||
|
||
const sortElvesByCalories = (data) => { | ||
const sum = (a, b) => { return a + b } | ||
const compare = (a, b) => { | ||
// compare sums of array values for sum-based sorting | ||
return b.reduce( | ||
sum, 0 | ||
) - a.reduce( | ||
sum, 0 | ||
) | ||
} | ||
data.sort(compare) | ||
return data | ||
} | ||
|
||
module.exports = { | ||
findElfWithMost, | ||
parseCalorieData, | ||
sortElvesByCalories | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-env mocha */ | ||
const { expect } = require('chai') | ||
const { findElfWithMost, parseCalorieData, sortElvesByCalories } = require('./calories') | ||
|
||
const calorieData = `1000 | ||
2000 | ||
3000 | ||
4000 | ||
5000 | ||
6000 | ||
7000 | ||
8000 | ||
9000 | ||
10000` | ||
const parsedCalorieData = [ | ||
[1000, 2000, 3000], | ||
[4000], | ||
[5000, 6000], | ||
[7000, 8000, 9000], | ||
[10000] | ||
] | ||
|
||
describe('--- Day 1: Calorie Counting ---', () => { | ||
describe('Part 1', () => { | ||
describe('parseCalorieData', () => { | ||
it('Splits data into a list of elves with provisions', () => { | ||
expect(parseCalorieData(calorieData)) | ||
.to.deep.equal(parsedCalorieData) | ||
}) | ||
}) | ||
describe('findElfWithMost()', () => { | ||
it('Identifies the elf with the most total calories', () => { | ||
expect(findElfWithMost(parsedCalorieData) | ||
.reduce((a, b) => a + b)) | ||
.to.equal(24000) | ||
}) | ||
}) | ||
describe('sortElvesByCalories()', () => { | ||
it('Sorts the list of elves by calories carried, maximum first', () => { | ||
expect(sortElvesByCalories(parsedCalorieData)) | ||
.to.deep.equal([ | ||
[7000, 8000, 9000], | ||
[5000, 6000], | ||
[10000], | ||
[1000, 2000, 3000], | ||
[4000] | ||
]) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const console = require('../helpers') | ||
require('./solution') |
Oops, something went wrong.