Skip to content

Commit 1da18c4

Browse files
feat(2018 day-04): stack-rank the guards by laziness
1 parent 28fd721 commit 1da18c4

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

2018/day-04/guards.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,25 @@ let _data = {}
55
const getData = (key) => _data[key]
66
const setData = (key) => _data[key]
77

8-
const findLaziestGuards = (data) => {
9-
let guards = []
10-
return guards
8+
/**
9+
* Searches the list of daily activity to rank lazy guards
10+
* @param {Array} days List of day sleeping patterns as returned by processActivities()
11+
*/
12+
const findLaziestGuards = (days) => {
13+
// Get a list of guards with their sleeping times
14+
let guards = days.filter((day, idx, arr) => {
15+
return (arr.indexOf(day) === idx) // filters a list of unique guard IDs
16+
}).map((day) => {
17+
return { id: day.guard } // Makes a list of guard objects
18+
}).map((guard) => {
19+
guard.asleep = days.filter((day, idx, arr) => {
20+
return (day.guard === guard.id) // find the days this guard
21+
}).reduce((acc, day) => acc + day.activity.count('#')) // count the time the guard was asleep
22+
return guard
23+
})
24+
25+
// sort the list with the laziest guard first
26+
return guards.sort(helpers.dynamicSort('-asleep'))
1127
}
1228

1329
const processActivities = (data) => {

2018/day-04/guards.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ describe('--- Day 4: Repose Record ---', () => {
8888
describe('findLaziestGuard()', () => {
8989
it.skip('locates the guard who sleeps the most minutes', () => {
9090
const expected = 10
91-
const actual = findLaziestGuard()
91+
const actual = findLaziestGuard(
92+
processActivities(
93+
sortActivities(testActivities)
94+
)
95+
)
9296
expect(actual).to.equal(expected)
9397
})
9498
})

0 commit comments

Comments
 (0)