Skip to content

Commit 0b3ed0b

Browse files
feat(2018 day-04): generate bitmap strings based on guard activities
1 parent db8c9a6 commit 0b3ed0b

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

2018/day-04/guards.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,90 @@ const findLaziestGuards = (data) => {
55
return guards
66
}
77

8+
const processActivities = (data) => {
9+
// we'll store asleep as (#) and awake as (.)
10+
const statesMap = {
11+
'begins shift': '.',
12+
'wakes up': '.',
13+
'falls asleep': '#'
14+
}
15+
16+
// store variables iterated through the loop
17+
let store = data[0]
18+
store.state = statesMap[store.state]
19+
20+
// let idx = 0
21+
22+
// Build up the results set
23+
let results = [{
24+
date: store.date,
25+
guard: store.guard,
26+
activity: ''
27+
}]
28+
29+
// Iterate through events to log activities
30+
data.forEach((event, idx) => {
31+
// Crossed into new day
32+
if (event.date !== store.date) {
33+
// Finish out the open pattern
34+
let prevAct = results[results.length - 1].activity
35+
if (prevAct.length < 60) {
36+
results[results.length - 1].activity += store.state.repeat(60 - prevAct.length)
37+
}
38+
39+
// Start a new activity pattern
40+
// The new activity pattern should fill up to the current minute, or completely fill
41+
// when the new event is in a later hour
42+
let len = (event.hour === 0) ? event.minute : 60
43+
results.push({
44+
date: event.date,
45+
guard: event.guard || store.guard,
46+
activity: (len > 0) ? store.state.repeat(len) : ''
47+
})
48+
}
49+
50+
// Event is the same day as the previous event
51+
if (event.date === store.date) {
52+
let act = results[results.length - 1].activity
53+
// Populate the previous state up to the current minute or up to the full hour
54+
// when it's no longer the 0 hour
55+
let len = (event.hour === 0) ? event.minute - act.length : 60 - act.length
56+
57+
if (len > 0 && len < 60) {
58+
results[results.length - 1].activity += store.state.repeat(len)
59+
}
60+
}
61+
62+
// Update tracker with new event
63+
store.state = statesMap[event.activity]
64+
store.guard = event.guard || store.guard
65+
store.date = event.date
66+
store.hour = event.hour
67+
store.minute = event.minute
68+
69+
// Mark the current event in the current activity pattern
70+
if (event.hour === 0) {
71+
results[results.length - 1].activity += store.state
72+
}
73+
})
74+
75+
// Finish up last entry by padding it out to the full width
76+
results[results.length - 1].activity += store.state.repeat(60 - results[results.length - 1].activity.length)
77+
78+
console.log('Resulting activity chart:')
79+
results.forEach((res) => {
80+
console.log(`${res.date} ${res.guard} ${res.activity}`)
81+
})
82+
83+
return results
84+
}
85+
886
const sortActivities = (data) => {
987
return data.sort(helpers.dynamicSortMultiple('date', 'hour', 'minute'))
1088
}
1189

1290
module.exports = {
1391
findLaziestGuards,
92+
processActivities,
1493
sortActivities
1594
}

2018/day-04/guards.test.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const helpers = require('./helpers')
44
const {
55
findLaziestGuard,
66
findTimesGuardLikleyAsleep,
7+
processActivities,
78
sortActivities
89
} = require('./guards')
910

@@ -52,9 +53,35 @@ describe('--- Day 4: Repose Record ---', () => {
5253
})
5354
})
5455

55-
describe('mapRecords()', () => {
56-
it.skip('converts the records into a parseable matrix, logging guards times', () => {
57-
56+
describe('processActivities()', () => {
57+
it('converts the records into day-based activities list, logging guards times awake and asleep in strings', () => {
58+
const expected = [
59+
{
60+
date: '1518-11-01',
61+
guard: 10,
62+
activity: '.....####################.....#########################.....'
63+
}, {
64+
date: '1518-11-02',
65+
guard: 99,
66+
activity: '........................................##########..........'
67+
}, {
68+
date: '1518-11-03',
69+
guard: 10,
70+
activity: '........................#####...............................'
71+
}, {
72+
date: '1518-11-04',
73+
guard: 99,
74+
activity: '....................................##########..............'
75+
}, {
76+
date: '1518-11-05',
77+
guard: 99,
78+
activity: '.............................................##########.....'
79+
}
80+
]
81+
const actual = processActivities(
82+
sortActivities(testActivities)
83+
)
84+
expect(actual).to.deep.equal(expected)
5885
})
5986
})
6087

0 commit comments

Comments
 (0)