aboutsummaryrefslogtreecommitdiff
path: root/challenge-128/abigail/node/ch-2.js
blob: 0f0987c00c09b18aa0a3881f7525d68e25c77dc9 (plain)
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
#!/usr/local/bin/node

//
// See ../README.md
//

//
// Run as: node ch-2.js < input-file
//

let first_line = 1
let arrivals   = []
let departures = []

  require         ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', line => {
    let times = line . match (/[0-9][0-9]:[0-9][0-9]/g)
                     . map (_ => _ . split (/:/) . map (_ => +_))
                     . map (_ => 60 * _ [0] + _ [1])
    if (first_line) {
        arrivals   = times
        first_line = 0
    }
    else {
        departures = times
    }
})
. on              ('close', () => {
    let trains = []
    for (i = 0; i < 24 * 60; i ++) {
        trains [i] = 0
    }
    arrivals . forEach ((arrival, index) => {
        let departure = departures [index]
        for (i = arrival; i <= departure; i ++) {
            trains [i] ++
        }
        if (arrival > departure) {
            for (i = 0; i <= departure; i ++) {
                trains [i] ++
            }
            for (i = arrival; i < 24 * 60; i ++) {
                trains [i] ++
            }
        }
    })
    console . log (Math . max (... trains))
})