Skip to content

Commit 84d8517

Browse files
committed
feat: Day 4: Repose Record (part 1)
1 parent 07b773c commit 84d8517

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
1414
- [Day 1: Chronal Calibration](day-01-chronal-calibration/)
1515
- [Day 2: Inventory Management System](day-02-inventory-management-system/)
1616
- [Day 3: No Matter How You Slice It](day-03-no-matter-how-you-slice-it/)
17+
- [Day 4: Repose Record](day-04-repose-record/)
1718

1819
## Running Tests
1920

day-04-repose-record/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Day 4: Repose Record
2+
3+
You've sneaked into another supply closet - this time, it's across from the prototype suit manufacturing lab. You need to sneak inside and fix the issues with the suit, but there's a guard stationed outside the lab, so this is as close as you can safely get.
4+
5+
As you search the closet for anything that might help, you discover that you're not the first person to want to sneak in. Covering the walls, someone has spent an hour starting every midnight for the past few months secretly observing this guard post! They've been writing down the ID of **the one guard on duty that night** - the Elves seem to have decided that one guard was enough for the overnight shift - as well as when they fall asleep or wake up while at their post (your puzzle input).
6+
7+
For example, consider the following records, which have already been organized into chronological order:
8+
9+
```
10+
[1518-11-01 00:00] Guard #10 begins shift
11+
[1518-11-01 00:05] falls asleep
12+
[1518-11-01 00:25] wakes up
13+
[1518-11-01 00:30] falls asleep
14+
[1518-11-01 00:55] wakes up
15+
[1518-11-01 23:58] Guard #99 begins shift
16+
[1518-11-02 00:40] falls asleep
17+
[1518-11-02 00:50] wakes up
18+
[1518-11-03 00:05] Guard #10 begins shift
19+
[1518-11-03 00:24] falls asleep
20+
[1518-11-03 00:29] wakes up
21+
[1518-11-04 00:02] Guard #99 begins shift
22+
[1518-11-04 00:36] falls asleep
23+
[1518-11-04 00:46] wakes up
24+
[1518-11-05 00:03] Guard #99 begins shift
25+
[1518-11-05 00:45] falls asleep
26+
[1518-11-05 00:55] wakes up
27+
```
28+
29+
Timestamps are written using `year-month-day hour:minute` format. The guard falling asleep or waking up is always the one whose shift most recently started. Because all asleep/awake times are during the midnight hour (`00:00` - `00:59`), only the minute portion (`00` - `59`) is relevant for those events.
30+
31+
Visually, these records show that the guards are asleep at these times:
32+
33+
```
34+
Date ID Minute
35+
000000000011111111112222222222333333333344444444445555555555
36+
012345678901234567890123456789012345678901234567890123456789
37+
11-01 #10 .....####################.....#########################.....
38+
11-02 #99 ........................................##########..........
39+
11-03 #10 ........................#####...............................
40+
11-04 #99 ....................................##########..............
41+
11-05 #99 .............................................##########.....
42+
```
43+
44+
The columns are Date, which shows the month-day portion of the relevant day; ID, which shows the guard on duty that day; and Minute, which shows the minutes during which the guard was asleep within the midnight hour. (The Minute column's header shows the minute's ten's digit in the first row and the one's digit in the second row.) Awake is shown as `.`, and asleep is shown as `#`.
45+
46+
Note that guards count as asleep on the minute they fall asleep, and they count as awake on the minute they wake up. For example, because Guard #10 wakes up at 00:25 on 1518-11-01, minute 25 is marked as awake.
47+
48+
If you can figure out the guard most likely to be asleep at a specific time, you might be able to trick that guard into working tonight so you can have the best chance of sneaking in. You have two strategies for choosing the best guard/minute combination.
49+
50+
**Strategy 1**: Find the guard that has the most minutes asleep. What minute does that guard spend asleep the most?
51+
52+
In the example above, Guard #10 spent the most minutes asleep, a total of 50 minutes (20+25+5), while Guard #99 only slept for a total of 30 minutes (10+10+10). Guard #**10** was asleep most during minute **24** (on two days, whereas any other minute the guard was asleep was only seen on one day).
53+
54+
While this example listed the entries in chronological order, your entries are in the order you found them. You'll need to organize them before they can be analyzed.
55+
56+
**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`.)
57+
58+
## References
59+
- https://adventofcode.com/2018/day/4

day-04-repose-record/sleep.js

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

day-04-repose-record/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const assert = require('assert');
2+
3+
const sleep = require('./sleep');
4+
5+
describe('Day 4: Repose Record', () => {
6+
it('should determine sleepiest guard', () => {
7+
const schedule =
8+
`[1518-11-01 00:00] Guard #10 begins shift
9+
[1518-11-01 00:05] falls asleep
10+
[1518-11-01 00:25] wakes up
11+
[1518-11-01 00:30] falls asleep
12+
[1518-11-01 00:55] wakes up
13+
[1518-11-01 23:58] Guard #99 begins shift
14+
[1518-11-02 00:40] falls asleep
15+
[1518-11-02 00:50] wakes up
16+
[1518-11-03 00:05] Guard #10 begins shift
17+
[1518-11-03 00:24] falls asleep
18+
[1518-11-03 00:29] wakes up
19+
[1518-11-04 00:02] Guard #99 begins shift
20+
[1518-11-04 00:36] falls asleep
21+
[1518-11-04 00:46] wakes up
22+
[1518-11-05 00:03] Guard #99 begins shift
23+
[1518-11-05 00:45] falls asleep
24+
[1518-11-05 00:55] wakes up`;
25+
26+
assert.strictEqual(sleep(schedule), 240);
27+
});
28+
});

0 commit comments

Comments
 (0)