File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -5,9 +5,25 @@ let _data = {}
55const getData = ( key ) => _data [ key ]
66const 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
1329const processActivities = ( data ) => {
Original file line number Diff line number Diff 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 } )
You can’t perform that action at this time.
0 commit comments