Skip to content

Commit e2510dd

Browse files
Merge pull request #228 from amclin/feat/2022-day-01
Feat/2022 day 01
2 parents 77f3910 + ef66f7d commit e2510dd

File tree

15 files changed

+2403
-11
lines changed

15 files changed

+2403
-11
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: actions/checkout@v2
2222
- uses: actions/setup-node@v2
2323
with:
24-
node-version: 14
24+
node-version: 16
2525
- run: npm ci
2626
- name: Semantic Release
2727
uses: cycjimmy/semantic-release-action@v2

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

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

2222
steps:

2018/day-04/guards.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const findSleepiestTimes = (guard, data) => {
4444
const getTimesAsleep = (activity) => {
4545
return activity.split('')
4646
.map((state, idx) => {
47-
return { id: idx, state: state } // parse the activity stream into data
47+
return { id: idx, state } // parse the activity stream into data
4848
}).filter((minute) => minute.state === '#') // get only the times the guard is asleep
4949
.map((minute) => minute.id) // convert into a list of times
5050
}

2018/day-06/coordinates.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ const blankGrid = () => {
7676
grid[x] = grid[x] || []
7777
for (let y = minY; y < maxY; y++) {
7878
grid[x][y] = grid[x][y] || {
79-
x: x,
80-
y: y
79+
x,
80+
y
8181
}
8282
}
8383
}

2018/day-09/marbles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ const playGame = (playerCount, highMarble, showBoard) => {
160160

161161
// tally the results
162162
const results = {
163-
players: players,
163+
players,
164164
highScore: players.sort((a, b) => b - a)[0]
165165
}
166166

2018/day-10/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const init = (data) => {
2121
const best = beaconTracker.frameMeta.reduce((acc, curr, idx) => {
2222
return (curr.focus < acc.focus)
2323
? {
24-
idx: idx,
24+
idx,
2525
focus: curr.focus,
2626
dims: curr.dims
2727
}

2018/day-11/fuel-cells.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Rack {
9595
findMaxSquare (size) {
9696
return this.cells.reduce((acc, cell, idx) => {
9797
const squarePower = this._tallySquare(idx, size)
98-
return (squarePower !== null && squarePower > acc.power) ? { power: squarePower, idx: idx } : acc
98+
return (squarePower !== null && squarePower > acc.power) ? { power: squarePower, idx } : acc
9999
}, { power: -99999, idx: -1 })
100100
}
101101
}

2018/day-12/plants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Plants {
6363

6464
return {
6565
position: pot.position,
66-
state: state
66+
state
6767
}
6868
})
6969

2022/day-01/calories.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
return sortElvesByCalories(data)[0] // Sort for the elf with the most calories
21+
}
22+
23+
const sortElvesByCalories = (data) => {
24+
const sum = (a, b) => { return a + b }
25+
const compare = (a, b) => {
26+
// compare sums of array values for sum-based sorting
27+
return b.reduce(
28+
sum, 0
29+
) - a.reduce(
30+
sum, 0
31+
)
32+
}
33+
data.sort(compare)
34+
return data
35+
}
36+
37+
module.exports = {
38+
findElfWithMost,
39+
parseCalorieData,
40+
sortElvesByCalories
41+
}

2022/day-01/calories.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { findElfWithMost, parseCalorieData, sortElvesByCalories } = 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+
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+
})
54+
})
55+
})

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