-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (158 loc) · 3.16 KB
/
Copy pathindex.js
File metadata and controls
171 lines (158 loc) · 3.16 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const fs = require("node:fs");
const readline = require("readline");
const { OUTPUT_PATH, MEASUREMENTS_PATH, Timer } = require("../common");
const cityMap = new Map(
[
"Adenarith",
"Amsterdam",
"Anápolis",
"Aparecida de Goiânia",
"Athens",
"Austin",
"Bahrain",
"Bangalore",
"Barcelona",
"Belo Horizonte",
"Belém",
"Boa Vista",
"Boston",
"Brasília",
"Brussels",
"Bucharest",
"Campinas",
"Canada",
"Central",
"Chennai",
"Chongqing",
"Copenhagen",
"Cuiabá",
"Curitiba",
"Dallas",
"Dublin",
"Duque de Caxias",
"Feira de Santana",
"Fortaleza",
"Frankfurt",
"Gaaphis",
"Goiania",
"Guadalajara",
"Guarulhos",
"Helsinki",
"Hong Kong",
"Hyderabad",
"Indianapolis",
"Ireland",
"Istanbul",
"Juiz de Fora",
"Kiev",
"Kolkata",
"Krofast",
"Krore",
"Larfast",
"London",
"Londrina",
"Los Angeles",
"Macapá",
"Madrid",
"Manaus",
"Mexico City",
"Miami",
"Milan",
"Montreal",
"Moscow",
"Mumbai",
"N. California",
"N. Virginia",
"New Delhi",
"New York",
"Niterói",
"Nova Iguaçu",
"Ohio",
"Oregon",
"Osaka",
"Osasco",
"Oslo",
"Palmas",
"Paris",
"Porto Alegre",
"Porto Velho",
"Prico",
"Prover",
"Pune",
"Qreigh",
"Qrokwood",
"Recife",
"Ribeirão Preto",
"Rio de Janeiro",
"Salvador",
"Santo André",
"Sao Paulo",
"Seoul",
"Singapore",
"St. Petersburg",
"Stockholm",
"Sydney",
"São Bernardo do Campo",
"São Gonçalo",
"São José dos Campos",
"São Paulo",
"Tokyo",
"Toronto",
"Urgtin",
"Vancouver",
"Vienna",
"Warsaw",
"Zurich",
].map((v) => [
v,
{
min: 9007199254740991,
max: 0,
total: BigInt(0),
count: 0,
},
])
);
async function solution() {
const instream = fs.createReadStream(MEASUREMENTS_PATH);
const rl = readline.createInterface({
input: instream,
crlfDelay: Infinity,
});
rl.addListener("line", (line) => {
let [cityName, measurement] = line.split(";");
measurement = Number(measurement);
const city = cityMap.get(cityName);
city.min = Math.min(city.min, measurement);
city.max = Math.max(city.max, measurement);
city.total += BigInt(measurement);
city.count++;
});
await new Promise((resolve, _) => {
rl.addListener("close", () => resolve());
});
return Array.from(cityMap)
.map(
([city_name, status]) =>
`${city_name}=${status.min};${status.max};${~~(
status.total / BigInt(status.count)
)}(${status.total}/${status.count})\n`
)
.join("");
}
async function main() {
const expectOutput = String(fs.readFileSync(OUTPUT_PATH));
const timer = new Timer();
const got = await solution();
console.log(`Elapsed: ${timer.elapsedAsMilliSeconds()}ms`);
if (expectOutput === got) {
console.log("Test passed");
} else {
console.log("Test failed");
console.log("Expected:");
console.log(expectOutput);
console.log("Actual:");
console.log(got);
}
}
main();