11'use strict'
22
3- //////////////
4-
5- class CaseParser {
6- constructor ( ) {
7- this . damage = 0
8- this . sequence = [ ]
9- this . state = '1'
10- }
11-
12- readline ( line ) {
13- let values = line . split ( ' ' )
14- this . damage = parseInt ( values [ 0 ] )
15- this . sequence = Array . from ( values [ 1 ] )
16-
17- this . state = 'done'
18- }
19-
20- isComplete ( ) {
21- return ( this . state === 'done' )
22- }
23-
24- getCase ( ) {
25- return {
26- damage : this . damage ,
27- sequence : this . sequence
28- }
29- }
30- }
31-
32- function parse ( ) {
33- const readline = require ( 'readline' ) ;
34- let t = 0 ;
35- let currentT = 0 ;
36- let readState = 't'
37- let probs = [ ]
38- let caseParser = new CaseParser ( )
39-
40- const rl = readline . createInterface ( {
41- input : process . stdin ,
42- output : process . stdout
43- } ) ;
44-
45- rl . on ( 'line' , ( line ) => {
46- switch ( readState ) {
47- case 't' : {
48- t = parseInt ( line )
49- readState = 'case'
50- break
51- }
52- case 'case' : {
53- caseParser . readline ( line )
54-
55- if ( caseParser . isComplete ( ) ) {
56- probs . push ( caseParser . getCase ( ) )
57- currentT += 1
58- caseParser = new CaseParser ( )
59- }
60-
61- break
62- }
63- }
64-
65- if ( currentT === t ) {
66- rl . close ( )
67- }
68- } )
69- . on ( 'close' , ( ) => {
70- proc ( probs )
71- } )
72- }
73-
74- function proc ( probs ) {
75- for ( let index = 0 ; index < probs . length ; index ++ ) {
76- const result = solve ( probs [ index ] ) ;
77- console . log ( `Case #${ index + 1 } : ${ result } ` )
78- }
79- }
80-
81- //////// Solve /////////
3+ //
4+ // solve
5+ //
826function solve ( prob ) {
837 if ( isImposible ( prob ) ) {
848 return 'IMPOSSIBLE'
@@ -91,7 +15,7 @@ function solve(prob) {
9115function calculateMinSwap ( prob ) {
9216 let strengths = [ ]
9317 for ( let index = 0 ; index < prob . sequence . length ; index ++ ) {
94- const element = prob . sequence [ index ] ;
18+ const element = prob . sequence [ index ]
9519
9620 if ( index === 0 ) {
9721 if ( element === 'S' ) {
@@ -172,17 +96,115 @@ function isImposible(prob) {
17296 return false
17397}
17498
175- ////////////////////////
99+ //
100+ // processCases
101+ //
102+ function processCases ( probs ) {
103+ for ( let index = 0 ; index < probs . length ; index ++ ) {
104+ const result = solve ( probs [ index ] )
105+ console . log ( `Case #${ index + 1 } : ${ result } ` )
106+ }
107+ }
176108
177- function Problem ( ) {
178- this . damage = 0
179- this . sequence = [ ]
109+ //
110+ // CaseParser
111+ //
112+ class CaseParser {
113+ constructor ( ) {
114+ this . damage = 0
115+ this . sequence = [ ]
116+ this . state = '1'
117+ }
118+
119+ readline ( line ) {
120+ let values = line . split ( ' ' )
121+ this . damage = parseInt ( values [ 0 ] )
122+ this . sequence = Array . from ( values [ 1 ] )
123+
124+ this . state = 'done'
125+ }
126+
127+ isComplete ( ) {
128+ return ( this . state === 'done' )
129+ }
130+
131+ getCase ( ) {
132+ return {
133+ damage : this . damage ,
134+ sequence : this . sequence
135+ }
136+ }
180137}
181138
182- //////////////
139+ //
140+ // ProblemParser
141+ //
142+ class ProblemParser {
143+ constructor ( ) {
144+ this . t = 0
145+ this . currentT = 0
146+ this . cases = [ ]
147+ this . caseParser = new CaseParser ( )
148+ this . state = 't'
149+ }
183150
151+ readline ( line ) {
152+ switch ( this . state ) {
153+ case 't' : {
154+ this . t = parseInt ( line )
155+ this . state = 'case'
156+ break
157+ }
158+
159+ case 'case' : {
160+ this . caseParser . readline ( line )
161+
162+ if ( this . caseParser . isComplete ( ) ) {
163+ this . cases . push ( this . caseParser . getCase ( ) )
164+ this . currentT += 1
165+ this . caseParser = new CaseParser ( )
166+ }
167+
168+ break
169+ }
170+ }
171+
172+ if ( this . currentT === this . t ) {
173+ this . state = 'done'
174+ }
175+ }
176+
177+ isComplete ( ) {
178+ return ( this . state === 'done' )
179+ }
180+
181+ getCases ( ) {
182+ return this . cases
183+ }
184+ }
185+
186+ //
187+ // Main
188+ //
184189function main ( ) {
185- parse ( )
190+ const readline = require ( 'readline' )
191+ const problemParser = new ProblemParser ( )
192+
193+ const rl = readline . createInterface ( {
194+ input : process . stdin ,
195+ output : process . stdout
196+ } )
197+
198+ rl . on ( 'line' , ( line ) => {
199+ problemParser . readline ( line )
200+
201+ if ( problemParser . isComplete ( ) ) {
202+ rl . close ( )
203+ }
204+ } ) . on ( 'close' , ( ) => {
205+ processCases ( problemParser . getCases ( ) )
206+ }
207+ )
186208}
187209
188210if ( ! module . parent ) {
0 commit comments