forked from trainline-eu/csa-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsa.js
executable file
·79 lines (66 loc) · 1.97 KB
/
csa.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
72
73
74
75
76
77
78
79
#!/usr/bin/env node
'use strict'
const DEP = 0, ARR = 1, DEP_TS = 2, ARR_TS = 3
const MAX_STATION = 100000
const INFINITY = Math.pow(2, 32) - 1
let timetable = new Uint32Array(4)
let timetableLength = 0 // Actual number of elements in 'timetable'
const inConn = new Uint32Array(MAX_STATION)
const earliestArr = new Uint32Array(MAX_STATION)
function compute(trip) { // Crankshaft-optimizable (Node v5.0.0)
let route = []
let station
inConn.fill(INFINITY)
earliestArr.fill(INFINITY)
earliestArr[trip[DEP]] = trip[DEP_TS]
for (let i = 0 ; i < timetableLength ; i = i + 4 /* [1] */) {
// [1]: Crankshaft doesn't support let compound assignements ('i += 4').
if (
timetable[i + DEP_TS] >= earliestArr[timetable[i + DEP]] &&
timetable[i + ARR_TS] < earliestArr[timetable[i + ARR]]
) {
inConn[timetable[i + ARR]] = i
earliestArr[timetable[i + ARR]] = timetable[i + ARR_TS]
} else if (timetable[i + DEP_TS] > earliestArr[trip[ARR]]) {
break
}
}
if (inConn[trip[ARR]] === INFINITY) {
return null
}
station = trip[ARR]
while (station !== trip[DEP]) {
route.unshift(timetable.slice(inConn[station], inConn[station] + 4))
station = route[0][DEP]
}
return route
}
let initializing = true
require('readline')
.createInterface({ input: process.stdin })
.on('line', function (line) {
if (!line) {
if (!initializing) {
this.close()
}
initializing = false
return
}
let connOrTrip = Uint32Array.from(line.split(' '))
if (initializing) {
if (timetable.length === timetableLength) {
let aux = new Uint32Array(timetable.length << 1)
aux.set(timetable)
timetable = aux
}
timetable.set(connOrTrip, timetableLength)
timetableLength += 4
} else {
let route = compute(connOrTrip)
if (route) {
console.log(route.map((c) => c.join(' ')).join('\n'), '\n')
} else {
console.log('NO_SOLUTION')
}
}
})