-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-a.js
71 lines (65 loc) · 2.02 KB
/
04-a.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const stack = []
const sleepSums = {}
let maxSleeper = {
guard: null,
totalMinutes: 0,
distribution: {}
}
require('fs')
.readFileSync('inputData/04-sleep.txt', 'utf-8')
.split(require('os').EOL)
.map(line => {
const match = line.match(
/^\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}).*\] (Guard #\d+|falls|wakes)/
)
return {
date: new Date(match[1], match[2] - 1, match[3], match[4], match[5]),
note: match[6]
}
})
.sort((a, b) => {
return a.date - b.date
})
.forEach(line => {
const { date, note } = line
console.log(`${date}, ${note}`)
switch (note) {
case 'wakes':
const sleepStart = stack.pop()
const guard = stack[stack.length - 1] // peek
sleepSums[guard] = sleepSums[guard] || {
guard: guard,
totalMinutes: 0,
distribution: {}
}
sleepSums[guard].totalMinutes =
sleepSums[guard].totalMinutes + (date - sleepStart) / 1000 / 60
// we know the minutes are always during midnigt
const startMinute = sleepStart.getMinutes()
const lastMinute = date.getMinutes()
for (let minute = startMinute; minute < lastMinute; minute++) {
sleepSums[guard].distribution[minute] =
(sleepSums[guard].distribution[minute] || 0) + 1
}
console.log(` ${guard} slept for ${sleepSums[guard].totalMinutes}`)
if (sleepSums[guard].totalMinutes > maxSleeper.totalMinutes) {
maxSleeper = sleepSums[guard]
}
break
case 'falls':
stack.push(date)
break
default:
// clear the old guard if necessary
if (stack.length) {
stack.length = 0
}
console.log(` Pushing ${note}`)
stack.push(note) // guard
}
})
console.log(maxSleeper)
const maxMinute = Object.keys(maxSleeper.distribution).sort((a, b) => {
return maxSleeper.distribution[b] - maxSleeper.distribution[a]
})[0]
console.log(`Result: ${parseInt(maxSleeper.guard.split('#')[1]) * maxMinute}`)