-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
85 lines (73 loc) · 2.24 KB
/
parser.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
80
81
82
83
84
85
/**
* Created by nastia on 27/02/2019.
*/
"use strict";
function readFile(file)
{
return new Promise(function (fulfill, reject) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
var data = rawFile.responseText.split('\n'),
input = data[0].split(' '),
inputObject = {
rows: input[0],
columns: input[1],
minIngradients: input[2],
maxPeaceSize: input[3]
},
pizza = data.splice(1, data.length - 2);
pizza.map((el, index) => {
pizza[index] = el.split('');
});
fulfill( {inputObject, pizza} );
}
}
};
rawFile.send(null);
});
}
function parsePizza({ inputObject, pizza })
{
//todo parse pizza
let {rows, columns, minIngradients, maxPeaceSize} = inputObject,
results = [];
for (let i in pizza) {
let start = 0, end = 0, m = 0, t = 0;
while ( end < columns ) {
if (pizza[i][end] == 'M') {
m++;
} else if (pizza[i][end] == 'T') {
t++;
}
end++;
if(end - start > maxPeaceSize) {
if (pizza[i][start] == 'M') {
m--;
} else if (pizza[i][start] == 'T') {
t--;
}
start++;
}
if(end - start <= maxPeaceSize && m >= minIngradients && t >= minIngradients) {
results.push([i, start, i, end - 1]);
start = end;
m = 0;
t = 0;
}
}
}
console.log(results);
}
function getChunks(fileName) {
readFile(fileName)
.then(function(result) {
parsePizza(result);
});
}
getChunks('a_example.in');
getChunks('b_small.in');
getChunks('c_medium.in');
getChunks('d_big.in');