|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -////////////// |
4 | | - |
5 | | -// call parse() to read the inputs and return list of problems / test cases. |
6 | | -function parse() { |
7 | | - const readline = require('readline'); |
8 | | - let t = 0; |
9 | | - let currentT = 0; |
10 | | - let readState = 't' |
11 | | - let probs = [] |
12 | | - |
13 | | - const rl = readline.createInterface({ |
14 | | - input: process.stdin, |
15 | | - output: process.stdout |
16 | | - }); |
17 | | - |
18 | | - rl.on('line', (line) => { |
19 | | - switch (readState) { |
20 | | - case 't': { |
21 | | - t = parseInt(line) |
22 | | - readState = 'num' |
23 | | - break |
24 | | - } |
25 | | - case 'num': { |
26 | | - readState = 'list' |
27 | | - break |
28 | | - } |
29 | | - case 'list': { |
30 | | - let prob = new Problem() |
31 | | - prob.numbers = line.split(' ').map(num => parseInt(num)) |
32 | | - probs.push(prob) |
33 | | - currentT += 1 |
34 | | - readState = 'num' |
35 | | - break; |
36 | | - } |
37 | | - } |
38 | | - |
39 | | - if (currentT === t) { |
40 | | - rl.close() |
41 | | - } |
42 | | - }) |
43 | | - .on('close', () => { |
44 | | - proc(probs) |
45 | | - }) |
46 | | -} |
47 | | - |
48 | | -function proc(probs) { |
49 | | - for (let index = 0; index < probs.length; index++) { |
50 | | - const result = solve(probs[index]); |
51 | | - console.log(`Case #${index + 1}: ${result}`) |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -//////// Solve ///////// |
| 3 | +// |
| 4 | +// solve |
| 5 | +// |
56 | 6 | function solve(prob) { |
57 | 7 | let firstArr = prob.numbers.reduce((acc, val, index, array) => { |
58 | 8 | if (index % 2 === 0) { |
@@ -113,16 +63,121 @@ function gotProblem(array) { |
113 | 63 | return false |
114 | 64 | } |
115 | 65 |
|
116 | | -//////////////////////// |
| 66 | +// |
| 67 | +// processCases |
| 68 | +// |
| 69 | +function processCases(probs) { |
| 70 | + for (let index = 0; index < probs.length; index++) { |
| 71 | + const result = solve(probs[index]); |
| 72 | + console.log(`Case #${index + 1}: ${result}`) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// |
| 77 | +// CaseParser |
| 78 | +// |
| 79 | +class CaseParser { |
| 80 | + constructor() { |
| 81 | + this.numbers = [] |
| 82 | + this.state = 'num' |
| 83 | + } |
| 84 | + |
| 85 | + readline(line) { |
| 86 | + switch(this.state) { |
| 87 | + case 'num': { |
| 88 | + // the line is useless. |
| 89 | + this.state = 'list' |
| 90 | + break |
| 91 | + } |
117 | 92 |
|
118 | | -function Problem() { |
119 | | - this.numbers = [] |
| 93 | + case 'list': { |
| 94 | + this.numbers = line.split(' ').map(num => parseInt(num)) |
| 95 | + this.state = 'done' |
| 96 | + break |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + isComplete() { |
| 102 | + return (this.state === 'done') |
| 103 | + } |
| 104 | + |
| 105 | + getCase() { |
| 106 | + return { |
| 107 | + numbers: this.numbers |
| 108 | + } |
| 109 | + } |
120 | 110 | } |
121 | 111 |
|
122 | | -////////////// |
| 112 | +// |
| 113 | +// ProblemParser |
| 114 | +// |
| 115 | +class ProblemParser { |
| 116 | + constructor() { |
| 117 | + this.t = 0 |
| 118 | + this.currentT = 0 |
| 119 | + this.cases = [] |
| 120 | + this.caseParser = new CaseParser() |
| 121 | + this.state = 't' |
| 122 | + } |
| 123 | + |
| 124 | + readline(line) { |
| 125 | + switch (this.state) { |
| 126 | + case 't': { |
| 127 | + this.t = parseInt(line) |
| 128 | + this.state = 'case' |
| 129 | + break |
| 130 | + } |
| 131 | + |
| 132 | + case 'case': { |
| 133 | + this.caseParser.readline(line) |
| 134 | + |
| 135 | + if (this.caseParser.isComplete()) { |
| 136 | + this.cases.push(this.caseParser.getCase()) |
| 137 | + this.currentT += 1 |
| 138 | + this.caseParser = new CaseParser() |
| 139 | + } |
| 140 | + |
| 141 | + break |
| 142 | + } |
| 143 | + } |
123 | 144 |
|
| 145 | + if (this.currentT === this.t) { |
| 146 | + this.state = 'done' |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + isComplete() { |
| 151 | + return (this.state === 'done') |
| 152 | + } |
| 153 | + |
| 154 | + getCases() { |
| 155 | + return this.cases |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// |
| 160 | +// Main |
| 161 | +// |
124 | 162 | function main() { |
125 | | - parse() |
| 163 | + const readline = require('readline') |
| 164 | + const problemParser = new ProblemParser() |
| 165 | + |
| 166 | + const rl = readline.createInterface({ |
| 167 | + input: process.stdin, |
| 168 | + output: process.stdout |
| 169 | + }) |
| 170 | + |
| 171 | + rl.on('line', (line) => { |
| 172 | + problemParser.readline(line) |
| 173 | + |
| 174 | + if (problemParser.isComplete()) { |
| 175 | + rl.close() |
| 176 | + } |
| 177 | + }).on('close', () => { |
| 178 | + processCases(problemParser.getCases()) |
| 179 | + } |
| 180 | + ) |
126 | 181 | } |
127 | 182 |
|
128 | 183 | if (!module.parent) { |
|
0 commit comments