Skip to content

Feat/2022 day 01 #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16
- run: npm ci
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: ['14']
node-version: ['16']
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
2 changes: 1 addition & 1 deletion 2018/day-04/guards.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const findSleepiestTimes = (guard, data) => {
const getTimesAsleep = (activity) => {
return activity.split('')
.map((state, idx) => {
return { id: idx, state: state } // parse the activity stream into data
return { id: idx, state } // parse the activity stream into data
}).filter((minute) => minute.state === '#') // get only the times the guard is asleep
.map((minute) => minute.id) // convert into a list of times
}
Expand Down
4 changes: 2 additions & 2 deletions 2018/day-06/coordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ const blankGrid = () => {
grid[x] = grid[x] || []
for (let y = minY; y < maxY; y++) {
grid[x][y] = grid[x][y] || {
x: x,
y: y
x,
y
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion 2018/day-09/marbles.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const playGame = (playerCount, highMarble, showBoard) => {

// tally the results
const results = {
players: players,
players,
highScore: players.sort((a, b) => b - a)[0]
}

Expand Down
2 changes: 1 addition & 1 deletion 2018/day-10/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const init = (data) => {
const best = beaconTracker.frameMeta.reduce((acc, curr, idx) => {
return (curr.focus < acc.focus)
? {
idx: idx,
idx,
focus: curr.focus,
dims: curr.dims
}
Expand Down
2 changes: 1 addition & 1 deletion 2018/day-11/fuel-cells.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Rack {
findMaxSquare (size) {
return this.cells.reduce((acc, cell, idx) => {
const squarePower = this._tallySquare(idx, size)
return (squarePower !== null && squarePower > acc.power) ? { power: squarePower, idx: idx } : acc
return (squarePower !== null && squarePower > acc.power) ? { power: squarePower, idx } : acc
}, { power: -99999, idx: -1 })
}
}
Expand Down
2 changes: 1 addition & 1 deletion 2018/day-12/plants.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Plants {

return {
position: pot.position,
state: state
state
}
})

Expand Down
41 changes: 41 additions & 0 deletions 2022/day-01/calories.js
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
}
55 changes: 55 additions & 0 deletions 2022/day-01/calories.test.js
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]
])
})
})
})
})
3 changes: 3 additions & 0 deletions 2022/day-01/index.js
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')
Loading