IDENTIFICATION DIVISION. PROGRAM-ID. D4. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO "IN/4.TXT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 STR PIC X(140). WORKING-STORAGE SECTION. 78 ROW-SIZE VALUE 140. 01 TBL. 05 ROW OCCURS ROW-SIZE TIMES. 10 ELT PIC X(ROW-SIZE). 01 IDX PIC 9(3). 01 JDX PIC 9(3). 01 TMP-STR PIC X(3). 01 FILE-POS PIC X. 88 EOF VALUE 'Y'. 88 NOT-EOF VALUE 'N'. 01 BKSLSH PIC X. 01 FWSLSH PIC X. 01 ANS-1 PIC 9(9). 01 ANS-2 PIC 9(9). PROCEDURE DIVISION. OPEN INPUT INPUT-FILE. PERFORM PARSE-FILE. PERFORM SOLVE OF P1. PERFORM SOLVE OF P2. DISPLAY 'ANS-1: ' ANS-1. DISPLAY 'ANS-2: ' ANS-2. CLOSE INPUT-FILE. STOP RUN. PARSE-FILE. PERFORM VARYING IDX FROM 1 BY 1 UNTIL EOF READ INPUT-FILE AT END SET EOF TO TRUE NOT AT END MOVE STR TO ROW(IDX) END-READ END-PERFORM. P1 SECTION. SOLVE. PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > ROW-SIZE AFTER JDX FROM 1 BY 1 UNTIL JDX > ROW-SIZE IF ELT(IDX)(JDX:1) = 'X' PERFORM START-COUNT THRU END-COUNT END-IF END-PERFORM. START-COUNT. COUNT-FORWARD. IF JDX + 3 <= ROW-SIZE IF ELT(IDX)(JDX:4) = 'XMAS' ADD 1 TO ANS-1 END-IF END-IF. COUNT-BACKWARD. IF JDX >= 3 IF ELT(IDX)(JDX - 3:4) = 'SAMX' ADD 1 TO ANS-1 END-IF END-IF. COUNT-DOWNWARD. IF IDX + 3 <= ROW-SIZE IF ELT(IDX)(JDX:1) = 'X' AND ELT(IDX + 1)(JDX:1) = 'M' AND ELT(IDX + 2)(JDX:1) = 'A' AND ELT(IDX + 3)(JDX:1) = 'S' ADD 1 TO ANS-1 END-IF END-IF. COUNT-UPWARD. IF IDX > 3 IF ELT(IDX - 3)(JDX:1) = 'S' AND ELT(IDX - 2)(JDX:1) = 'A' AND ELT(IDX - 1)(JDX:1) = 'M' AND ELT(IDX)(JDX:1) = 'X' ADD 1 TO ANS-1 END-IF END-IF. COUNT-FORwARD-DOWN. IF IDX + 3 <= ROW-SIZE AND JDX + 3 <= ROW-SIZE IF ELT(IDX)(JDX:1) = 'X' AND ELT(IDX + 1)(JDX + 1:1) = 'M' AND ELT(IDX + 2)(JDX + 2:1) = 'A' AND ELT(IDX + 3)(JDX + 3:1) = 'S' ADD 1 TO ANS-1 END-IF END-IF. COUNT-FORwARD-UP. IF IDX > 3 AND JDX + 3 <= ROW-SIZE IF ELT(IDX)(JDX:1) = 'X' AND ELT(IDX - 1)(JDX + 1:1) = 'M' AND ELT(IDX - 2)(JDX + 2:1) = 'A' AND ELT(IDX - 3)(JDX + 3:1) = 'S' ADD 1 TO ANS-1 END-IF END-IF. COUNT-BACKWARD-UP. IF IDX > 3 AND JDX > 3 IF ELT(IDX)(JDX:1) = 'X' AND ELT(IDX - 1)(JDX - 1:1) = 'M' AND ELT(IDX - 2)(JDX - 2:1) = 'A' AND ELT(IDX - 3)(JDX - 3:1) = 'S' ADD 1 TO ANS-1 END-IF END-IF. COUNT-BACKWARD-DOWN. IF IDX + 3 <= ROW-SIZE AND JDX > 3 IF ELT(IDX)(JDX:1) = 'X' AND ELT(IDX + 1)(JDX - 1:1) = 'M' AND ELT(IDX + 2)(JDX - 2:1) = 'A' AND ELT(IDX + 3)(JDX - 3:1) = 'S' ADD 1 TO ANS-1 END-IF END-IF. END-COUNT. P2 SECTION. SOLVE. PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > ROW-SIZE AFTER JDX FROM 1 BY 1 UNTIL JDX > ROW-SIZE IF ELT(IDX)(JDX:1) = 'A' MOVE 'N' TO BKSLSH FWSLSH PERFORM START-COUNT THRU END-COUNT END-IF END-PERFORM. START-COUNT. COUNT-BACKSLASH. IF IDX > 1 AND IDX < ROW-SIZE AND JDX > 1 AND JDX < ROW-SIZE STRING ELT(IDX - 1)(JDX - 1:1) ELT(IDX )(JDX :1) ELT(IDX + 1)(JDX + 1:1) INTO TMP-STR IF TMP-STR = 'MAS' OR TMP-STR = 'SAM' MOVE 'Y' TO BKSLSH END-IF END-IF. COUNT-FORWARDSLASH. IF IDX > 1 AND IDX < ROW-SIZE AND JDX > 1 AND JDX < ROW-SIZE STRING ELT(IDX - 1)(JDX + 1:1) ELT(IDX )(JDX :1) ELT(IDX + 1)(JDX - 1:1) INTO TMP-STR IF TMP-STR = 'MAS' OR TMP-STR = 'SAM' MOVE 'Y' TO FWSLSH END-IF END-IF. BOTH. IF BKSLSH = 'Y' And FWSLSH = 'Y' ADD 1 to ANS-2 END-IF. END-COUNT.