Skip to content

Commit 366e158

Browse files
committed
feat: Day 4: Repose Record (part 2)
1 parent 84d8517 commit 366e158

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

day-04-repose-record/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,13 @@ While this example listed the entries in chronological order, your entries are i
5555

5656
**What is the ID of the guard you chose multiplied by the minute you chose?** (In the above example, the answer would be `10 * 24 = 240`.)
5757

58+
## Part Two
59+
60+
**Strategy 2**: Of all guards, which guard is most frequently asleep on the same minute?
61+
62+
In the example above, Guard #**99** spent minute **45** asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.)
63+
64+
**What is the ID of the guard you chose multiplied by the minute you chose?** (In the above example, the answer would be `99 * 45 = 4455`.)
65+
5866
## References
5967
- https://adventofcode.com/2018/day/4

day-04-repose-record/sleep2.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class Guard {
2+
constructor (id) {
3+
this.id = id;
4+
this.minutesAsleep = Array.from({ length: 60 }).map(() => 0);
5+
this.fellAsleepAt = null;
6+
}
7+
8+
sleep (fellAsleepAt) {
9+
this.fellAsleepAt = fellAsleepAt;
10+
}
11+
12+
awake (awokeAt) {
13+
for (let i = this.fellAsleepAt; i < awokeAt; i++) {
14+
this.minutesAsleep[i] += 1;
15+
}
16+
}
17+
18+
get sleepiestMinute () {
19+
return this.minutesAsleep
20+
.map((value, index) => ({ index, value }))
21+
.sort((a, b) => b.value - a.value)[0].index;
22+
}
23+
}
24+
25+
const sleep = (input) => {
26+
const guards = {};
27+
const schedule = input
28+
.split('\n')
29+
.map((x) => x.trim())
30+
.sort()
31+
.map((x) => {
32+
const parts = x.match(/\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.*)/);
33+
34+
return {
35+
minute: +parts[5],
36+
message: parts[6],
37+
};
38+
});
39+
40+
let currentGuard = null;
41+
42+
for (let i = 0; i < schedule.length; i++) {
43+
const { message, minute } = schedule[i];
44+
45+
if (/Guard/.test(message)) {
46+
const parts = message.match(/Guard #(\d+) begins shift/);
47+
const guardId = +parts[1];
48+
const guard = guards[guardId] ? guards[guardId] : new Guard(guardId);
49+
50+
currentGuard = guards[guardId] = guard;
51+
} else if (message === 'falls asleep') {
52+
currentGuard.sleep(minute);
53+
} else if (message === 'wakes up') {
54+
currentGuard.awake(minute);
55+
}
56+
}
57+
58+
const sleepiestGuard = Object
59+
.keys(guards)
60+
.map((guardId) => guards[guardId])
61+
.sort((a, b) => b.minutesAsleep[b.sleepiestMinute] -
62+
a.minutesAsleep[a.sleepiestMinute])[0];
63+
64+
return sleepiestGuard.id * sleepiestGuard.sleepiestMinute;
65+
};
66+
67+
module.exports = sleep;

day-04-repose-record/test.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const assert = require('assert');
22

33
const sleep = require('./sleep');
4+
const sleep2 = require('./sleep2');
45

56
describe('Day 4: Repose Record', () => {
6-
it('should determine sleepiest guard', () => {
7+
it('should determine sleepiest guard (strategy 1)', () => {
78
const schedule =
89
`[1518-11-01 00:00] Guard #10 begins shift
910
[1518-11-01 00:05] falls asleep
@@ -25,4 +26,29 @@ describe('Day 4: Repose Record', () => {
2526

2627
assert.strictEqual(sleep(schedule), 240);
2728
});
29+
30+
describe('Part Two', () => {
31+
it('should determine sleepiest guard (strategy 2)', () => {
32+
const schedule =
33+
`[1518-11-01 00:00] Guard #10 begins shift
34+
[1518-11-01 00:05] falls asleep
35+
[1518-11-01 00:25] wakes up
36+
[1518-11-01 00:30] falls asleep
37+
[1518-11-01 00:55] wakes up
38+
[1518-11-01 23:58] Guard #99 begins shift
39+
[1518-11-02 00:40] falls asleep
40+
[1518-11-02 00:50] wakes up
41+
[1518-11-03 00:05] Guard #10 begins shift
42+
[1518-11-03 00:24] falls asleep
43+
[1518-11-03 00:29] wakes up
44+
[1518-11-04 00:02] Guard #99 begins shift
45+
[1518-11-04 00:36] falls asleep
46+
[1518-11-04 00:46] wakes up
47+
[1518-11-05 00:03] Guard #99 begins shift
48+
[1518-11-05 00:45] falls asleep
49+
[1518-11-05 00:55] wakes up`;
50+
51+
assert.strictEqual(sleep2(schedule), 4455);
52+
});
53+
});
2854
});

0 commit comments

Comments
 (0)